Skip to content

Commit

Permalink
[ci] [R-package] Add string_boundary_linter (#5324)
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousCorrelation authored Jun 23, 2022
1 parent eb13f39 commit 1b43214
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions .ci/lint_r_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ LINTERS_TO_USE <- list(
, "spaces_inside" = lintr::spaces_inside_linter()
, "spaces_left_parens" = lintr::spaces_left_parentheses_linter()
, "sprintf" = lintr::sprintf_linter()
, "string_boundary" = lintr::string_boundary_linter()
, "todo_comments" = lintr::todo_comment_linter(c("todo", "fixme", "to-do"))
, "trailing_blank" = lintr::trailing_blank_lines_linter()
, "trailing_white" = lintr::trailing_whitespace_linter()
Expand Down
6 changes: 3 additions & 3 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ VERBOSITY <- as.integer(

ON_WINDOWS <- .Platform$OS.type == "windows"

UTF8_LOCALE <- all(grepl(
pattern = "UTF-8$"
, x = Sys.getlocale(category = "LC_CTYPE")
UTF8_LOCALE <- all(endsWith(
Sys.getlocale(category = "LC_CTYPE")
, "UTF-8"
))

data(agaricus.train, package = "lightgbm")
Expand Down
34 changes: 17 additions & 17 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -787,20 +787,20 @@ test_that("all parameters are stored correctly with save_model_to_string()", {
params_in_file <- .params_from_model_string(model_str = model_str)

# parameters should match what was passed from the R package
expect_equal(sum(grepl(pattern = "^\\[metric\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[metric:")), 1L)
expect_equal(sum(params_in_file == "[metric: l2]"), 1L)

expect_equal(sum(grepl(pattern = "^\\[num_iterations\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[num_iterations:")), 1L)
expect_equal(sum(params_in_file == "[num_iterations: 4]"), 1L)

expect_equal(sum(grepl(pattern = "^\\[objective\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[objective:")), 1L)
expect_equal(sum(params_in_file == "[objective: regression]"), 1L)

expect_equal(sum(grepl(pattern = "^\\[verbosity\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[verbosity:")), 1L)
expect_equal(sum(params_in_file == sprintf("[verbosity: %i]", VERBOSITY)), 1L)

# early stopping should be off by default
expect_equal(sum(grepl(pattern = "^\\[early_stopping_round\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[early_stopping_round:")), 1L)
expect_equal(sum(params_in_file == "[early_stopping_round: 0]"), 1L)
})

Expand Down Expand Up @@ -851,15 +851,15 @@ test_that("early_stopping, num_iterations are stored correctly in model string e

# parameters should match what was passed from the R package, and the "main" (non-alias)
# params values in `params` should be preferred to keyword argumentts or aliases
expect_equal(sum(grepl(pattern = "^\\[num_iterations\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[num_iterations:")), 1L)
expect_equal(sum(params_in_file == sprintf("[num_iterations: %s]", num_iterations)), 1L)
expect_equal(sum(grepl(pattern = "^\\[early_stopping_round\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[early_stopping_round:")), 1L)
expect_equal(sum(params_in_file == sprintf("[early_stopping_round: %s]", early_stopping_round)), 1L)

# none of the aliases shouold have been written to the model file
expect_equal(sum(grepl(pattern = "^\\[num_boost_round\\:", x = params_in_file)), 0L)
expect_equal(sum(grepl(pattern = "^\\[n_iter\\:", x = params_in_file)), 0L)
expect_equal(sum(grepl(pattern = "^\\[n_iter_no_change\\:", x = params_in_file)), 0L)
expect_equal(sum(startsWith(params_in_file, "[num_boost_round:")), 0L)
expect_equal(sum(startsWith(params_in_file, "[n_iter:")), 0L)
expect_equal(sum(startsWith(params_in_file, "[n_iter_no_change:")), 0L)

})

Expand Down Expand Up @@ -1079,15 +1079,15 @@ test_that("lgb.cv() correctly handles passing through params to the model file",

# parameters should match what was passed from the R package, and the "main" (non-alias)
# params values in `params` should be preferred to keyword argumentts or aliases
expect_equal(sum(grepl(pattern = "^\\[num_iterations\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[num_iterations:")), 1L)
expect_equal(sum(params_in_file == sprintf("[num_iterations: %s]", num_iterations)), 1L)
expect_equal(sum(grepl(pattern = "^\\[early_stopping_round\\:", x = params_in_file)), 1L)
expect_equal(sum(startsWith(params_in_file, "[early_stopping_round:")), 1L)
expect_equal(sum(params_in_file == sprintf("[early_stopping_round: %s]", early_stopping_round)), 1L)

# none of the aliases shouold have been written to the model file
expect_equal(sum(grepl(pattern = "^\\[num_boost_round\\:", x = params_in_file)), 0L)
expect_equal(sum(grepl(pattern = "^\\[n_iter\\:", x = params_in_file)), 0L)
expect_equal(sum(grepl(pattern = "^\\[n_iter_no_change\\:", x = params_in_file)), 0L)
expect_equal(sum(startsWith(params_in_file, "[num_boost_round:")), 0L)
expect_equal(sum(startsWith(params_in_file, "[n_iter:")), 0L)
expect_equal(sum(startsWith(params_in_file, "[n_iter_no_change:")), 0L)
}

})
Expand Down Expand Up @@ -1268,8 +1268,8 @@ test_that("Booster's print, show, and summary work correctly", {
}

.has_expected_content_for_fitted_model <- function(printed_txt) {
expect_true(any(grepl("^LightGBM Model", printed_txt)))
expect_true(any(grepl("^Fitted to dataset", printed_txt)))
expect_true(any(startsWith(printed_txt, "LightGBM Model")))
expect_true(any(startsWith(printed_txt, "Fitted to dataset")))
}

.has_expected_content_for_finalized_model <- function(printed_txt) {
Expand Down
2 changes: 1 addition & 1 deletion build_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ dynlib_line <- grep(
)

c_api_contents <- readLines(file.path(TEMP_SOURCE_DIR, "src", "lightgbm_R.h"))
c_api_contents <- c_api_contents[grepl("^LIGHTGBM_C_EXPORT", c_api_contents)]
c_api_contents <- c_api_contents[startsWith(c_api_contents, "LIGHTGBM_C_EXPORT")]
c_api_contents <- gsub(
pattern = "LIGHTGBM_C_EXPORT SEXP "
, replacement = ""
Expand Down

0 comments on commit 1b43214

Please sign in to comment.