Skip to content

Commit

Permalink
Add more missing tests (#1624)
Browse files Browse the repository at this point in the history
* Add more missing tests

* tests for lang utilities

* Update test-backport_linter.R

* Update test-with.R

* Update test-with.R

* Test `validate_linter_db()`

* Update test-utils.R

* Use `:::` for internals
  • Loading branch information
IndrajeetPatil authored Oct 6, 2022
1 parent dba72ab commit aac0d14
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/comments.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ wercker_build_info <- function() {
# nocov start
github_comment <- function(text, info = NULL, token = settings$comment_token) {
if (!requireNamespace("httr", quietly = TRUE)) {
stop("Package 'httr' is required to post comments with github_comment().") # nocov
stop("Package 'httr' is required to post comments with github_comment().")
}

if (is.null(info)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-backport_linter.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
test_that("backport_linter produces error when R version misspecified", {
expect_error(
lint(text = "numToBits(2)", linters = backport_linter(420L)),
"`r_version` must be a R version number, returned by R_system_version(), or a string.",
fixed = TRUE
)
})

test_that("backport_linter detects backwards-incompatibility", {
# default should be current R version; all of these are included on our dependency
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter())
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-comments.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ test_that("it returns NULL if GIT_URL is not on github", {
expect_false(lintr:::in_ci())
})


test_that("it returns NULL for Jenkins PR build info when git URL is missing", {
clear_ci_info()
expect_null(lintr:::jenkins_build_info())
})

test_that("it determines Jenkins PR build info", {
clear_ci_info()
Sys.setenv(
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-linter_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ test_that("input validation for available_linters works as expected", {
expect_error(available_linters(exclude_tags = 1L), "`exclude_tags` must be a character vector.")
})

test_that("validate_linter_db works as expected", {
df_empty <- data.frame()
expect_warning(
lintr:::validate_linter_db(df_empty, "mypkg"),
"`linters.csv` must contain the columns 'linter' and 'tags'.",
fixed = TRUE
)
expect_false(suppressWarnings(lintr:::validate_linter_db(df_empty, "mypkg")))

df <- data.frame(linter = "absolute_path_linter", tags = "robustness")
expect_true(lintr:::validate_linter_db(df, "mypkg"))
})

test_that("available_linters returns a data frame", {
avail <- available_linters()
avail2 <- available_linters(c("lintr", "not-a-package"))
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-semicolon_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ test_that("Trailing semicolons only", {
expect_lint("function(){\nf <-\n 1 ;f <- 1.23\n}", NULL, linter)
})


test_that("Compound semicolons only", {
expect_error(
lint(text = "a <- 1;", linters = semicolon_linter(allow_trailing = TRUE, allow_compound = TRUE)),
"At least one of `allow_compound` or `allow_trailing` must be FALSE, otherwise no lints can be generated.",
fixed = TRUE
)
})

test_that("deprecation notices for semicolon_terminator_linter succeed, and the deprecated version works", {
expect_warning(
linter <- semicolon_terminator_linter(),
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("set_lang and reset_lang work as expected", {
old_lang <- Sys.getenv("LANGUAGE")
new_lang <- "en"

lintr:::set_lang(new_lang)
expect_identical(Sys.getenv("LANGUAGE"), new_lang)

lintr:::reset_lang(old_lang)
expect_identical(Sys.getenv("LANGUAGE"), old_lang)
})
10 changes: 10 additions & 0 deletions tests/testthat/test-with.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
test_that("modify_defaults produces error with missing or incorrect defaults", {
msg <- "`defaults` must be a named list."
expect_error(modify_defaults(), msg, fixed = TRUE)
expect_error(modify_defaults("assignment_linter"), msg, fixed = TRUE)
})

test_that("linters_with_tags produces error with incorrect tags", {
expect_error(linters_with_tags(1L:4L), "`tags` must be a character vector, or NULL.", fixed = TRUE)
})

test_that("linters_with_defaults works as expected with unnamed args", {
# assignment_linter is in defaults, so output doesn't change
expect_named(linters_with_defaults(assignment_linter), names(linters_with_defaults()))
Expand Down

0 comments on commit aac0d14

Please sign in to comment.