Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix delint conflicts #687

Merged
merged 21 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters: with_defaults( # The following TODOs are part of an effort to have {lintr} lint-free (#584)
line_length_linter = line_length_linter(120),
cyclocomp_linter = cyclocomp_linter(29), # TODO reduce to 15
cyclocomp_linter = cyclocomp_linter(30), # TODO reduce to 15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#673 added 1 to the cyclocomp of lint().
To keep this separate from removing ~ 200 lints from tests/, I temporarily bump the limit here.

paren_brace_linter = NULL # TODO enable (#603)
)
exclusions: list(
Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/test-T_and_F_symbol_linter.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
context("T_and_F_symbol_linter")

test_that("returns the correct linting", {
linter <- T_and_F_symbol_linter
msgT <- "Use TRUE instead of the symbol T."
msgF <- "Use FALSE instead of the symbol F."
msg_true <- "Use TRUE instead of the symbol T."
msg_false <- "Use FALSE instead of the symbol F."
expect_lint("FALSE", NULL, linter)
expect_lint("TRUE", NULL, linter)
expect_lint("x <- \"TRUE master vs FALSE slave\"", NULL, linter)
expect_lint("T", list(message=msgT, line_number=1L, column_number=2L), linter)
expect_lint("F", list(message=msgF, line_number=1L, column_number=2L), linter)
expect_lint("T", list(message = msg_true, line_number = 1L, column_number = 2L), linter)
expect_lint("F", list(message = msg_false, line_number = 1L, column_number = 2L), linter)
expect_lint("for (i in 1:10) {x <- c(T, TRUE, F, FALSE)}",
list(
list(message=msgT, line_number=1L, column_number=26L),
list(message=msgF, line_number=1L, column_number=35L)
list(message = msg_true, line_number = 1L, column_number = 26L),
list(message = msg_false, line_number = 1L, column_number = 35L)
),
linter)
})
86 changes: 42 additions & 44 deletions tests/testthat/test-absolute_path_linter.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
context("absolute_path_linter")

test_that("unquote", {
f <- lintr:::unquote
expect_equal(f(character()), character())
expect_equal(f("foo"), "foo")
expect_equal(
f(c("'f", "\"f'", "\"f\""), q="\""),
c("'f", "\"f'", "f" ))
f(c("'f", "\"f'", "\"f\""), q = "\""),
c("'f", "\"f'", "f"))
expect_equal(
f(c("\"f\"", "'f'", "`f`", "`'f'`"), q="'"),
f(c("\"f\"", "'f'", "`f`", "`'f'`"), q = "'"),
c("\"f\"", "f", "`f`", "`'f'`"))
expect_equal(f("`a\\`b`", q=c("`")), "a`b")
expect_equal(f("`a\\`b`", q = c("`")), "a`b")
x <- c("\"x\"", "\"\\n\"", "\"\\\\\"", "\"\\\\y\"", "\"\\ny\"", "\"\\\\ny\"", "\"\\\\\\ny\"",
"\"\\\\\\\\ny\"", "\"'\"", "\"\\\"\"", "\"`\"")
y <- c("x", "\n", "\\", "\\y", "\ny", "\\ny", "\\\ny", "\\\\ny", "'", "\"", "`")
expect_equal(f(x, q="\""), y)
expect_equal(f(x, q = "\""), y)
})

test_that("unescape", {
Expand All @@ -33,24 +31,24 @@ test_that("is_root_path", {
y <- logical()
expect_equal(f(x), y)

x <- c( "", "foo", "http://rseek.org/", "./", " /", "/foo", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
x <- c("", "foo", "http://rseek.org/", "./", " /", "/foo", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_equal(f(x), y)

x <- c( "/", "//")
x <- c("/", "//")
y <- c(TRUE, FALSE)
expect_equal(f(x), y)

x <- c( "~", "~/", "~//", "~bob2", "~foo_bar/")
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE)
x <- c("~", "~/", "~//", "~bob2", "~foo_bar/")
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)

x <- c("c:", "C:\\", "D:/", "C:\\\\", "D://")
y <- c(TRUE, TRUE, TRUE, FALSE, FALSE)
y <- c(TRUE, TRUE, TRUE, FALSE, FALSE)
expect_equal(f(x), y)

x <- c("\\\\", "\\\\localhost", "\\\\localhost\\")
y <- c( TRUE, TRUE, TRUE)
y <- c(TRUE, TRUE, TRUE)
expect_equal(f(x), y)
})

Expand All @@ -62,20 +60,20 @@ test_that("is_absolute_path", {
y <- logical()
expect_equal(f(x), y)

x <- c( "/", "//", "/foo", "/foo/")
y <- c(TRUE, FALSE, TRUE, TRUE)
x <- c("/", "//", "/foo", "/foo/")
y <- c(TRUE, FALSE, TRUE, TRUE)
expect_equal(f(x), y)

x <- c( "~", "~/foo", "~/foo/", "~'") #nolint
y <- c(TRUE, TRUE, TRUE, FALSE)
x <- c("~", "~/foo", "~/foo/", "~'")
y <- c(TRUE, TRUE, TRUE, FALSE)
expect_equal(f(x), y)

x <- c("c:", "C:\\foo\\", "C:/foo/") #nolint
y <- c(TRUE, TRUE, TRUE)
x <- c("c:", "C:\\foo\\", "C:/foo/")
y <- c(TRUE, TRUE, TRUE)
expect_equal(f(x), y)

x <- c("\\\\", "\\\\localhost", "\\\\localhost\\c$", "\\\\localhost\\c$\\foo") #nolint
y <- c( TRUE, TRUE, TRUE, TRUE)
x <- c("\\\\", "\\\\localhost", "\\\\localhost\\c$", "\\\\localhost\\c$\\foo")
y <- c(TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)
})

Expand All @@ -87,16 +85,16 @@ test_that("is_relative_path", {
y <- logical()
expect_equal(f(x), y)

x <- c( "/", "c:\\", "~/", "foo", "http://rseek.org/", "'./'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
x <- c("/", "c:\\", "~/", "foo", "http://rseek.org/", "'./'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_equal(f(x), y)

x <- c("/foo", "foo/", "foo/bar", "foo//bar", "./foo", "../foo") #nolint
y <- c( FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)
x <- c("/foo", "foo/", "foo/bar", "foo//bar", "./foo", "../foo")
y <- c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)

x <- c("\\\\", "\\foo", "foo\\", "foo\\bar", ".\\foo", "..\\foo", ".", "..", "../") #nolint
y <- c( FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
x <- c("\\\\", "\\foo", "foo\\", "foo\\bar", ".\\foo", "..\\foo", ".", "..", "../")
y <- c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)
})

Expand All @@ -108,12 +106,12 @@ test_that("is_path", {
y <- logical()
expect_equal(f(x), y)

x <- c( "", "foo", "http://rseek.org/", "foo\nbar", "'foo/bar'", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
x <- c("", "foo", "http://rseek.org/", "foo\nbar", "'foo/bar'", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_equal(f(x), y)

x <- c("c:", "..", "foo/bar", "foo\\bar", "~", "\\\\localhost") #nolint
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
x <- c("c:", "..", "foo/bar", "foo\\bar", "~", "\\\\localhost")
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)
})

Expand All @@ -125,21 +123,21 @@ test_that("is_valid_path", {
y <- logical()
expect_equal(f(x), y)

x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf") #nolint
y <- c( TRUE, FALSE, FALSE, FALSE)
x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf")
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_equal(f(x), y)

x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf") #nolint
y <- c( TRUE, FALSE, FALSE, FALSE)
expect_equal(f(x, lax=TRUE), y)
x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf")
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_equal(f(x, lax = TRUE), y)

x <- c("/asdf", "/asd*f", "/as:df", "/a\nsdf")
y <- c( TRUE, TRUE, TRUE, TRUE)
y <- c(TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)

x <- c("/asdf", "/asd*f", "/as:df", "/a\nsdf")
y <- c( TRUE, FALSE, FALSE, FALSE)
expect_equal(f(x, lax=TRUE), y)
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_equal(f(x, lax = TRUE), y)
})


Expand All @@ -150,8 +148,8 @@ test_that("is_long_path", {
y <- logical()
expect_equal(f(x), y)

x <- c("foo/", "/foo", "n/a", "Z:\\foo", "foo/bar", "~/foo", "../foo") #nolint
y <- c( FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
x <- c("foo/", "/foo", "n/a", "Z:\\foo", "foo/bar", "~/foo", "../foo")
y <- c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
expect_equal(f(x), y)
})

Expand All @@ -160,7 +158,7 @@ test_that("returns the correct linting", {
msg <- rex::escape("Do not use absolute paths.")

# strict mode
linter <- absolute_path_linter(lax=FALSE)
linter <- absolute_path_linter(lax = FALSE)
non_absolute_path_strings <- c(
"..",
"./blah",
Expand Down Expand Up @@ -191,7 +189,7 @@ test_that("returns the correct linting", {
}

# lax mode: no check for strings that are likely not paths (too short or with special characters)
linter <- absolute_path_linter(lax=TRUE)
linter <- absolute_path_linter(lax = TRUE)
unlikely_path_strings <- c(
"/",
encodeString("/a\nsdf/bar"),
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-error.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("error")
test_that("returns the correct linting", {
expect_lint("\"\\R\"",
rex("is an unrecognized escape in character string starting")
Expand Down Expand Up @@ -60,11 +59,6 @@ test_that("returns the correct linting", {
expected_message <- tryCatch(parse(text = "\\"), error = get_base_message)
expect_lint("\\", rex(expected_message))

# also try when LANGUAGE initially unset
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes non-english locales to fail the test.

Sys.unsetenv("LANGUAGE")
expected_message <- tryCatch(parse(text = "\\"), error = get_base_message)
expect_lint("\\", rex(expected_message))

expect_lint("``",
rex("attempt to use zero-length variable name")
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-exclusions.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ context("normalize_exclusions")

a <- tempfile()
b <- tempfile()
c <- tempfile(tmpdir=".")
c <- tempfile(tmpdir = ".")
file.create(a, b, c)
a <- normalizePath(a)
b <- normalizePath(b)
Expand Down Expand Up @@ -178,7 +178,7 @@ test_that("it normalizes file paths, removing non-existing files", {
expect_equal(normalize_exclusions(c(t1, t2, t3)), res)

res <- list(); res[[a]] <- 1:10; res[["notafile"]] <- 5:15; res[[c]] <- 5:15
expect_equal(normalize_exclusions(c(t1, t2, t3), normalize_path=FALSE), res)
expect_equal(normalize_exclusions(c(t1, t2, t3), normalize_path = FALSE), res)
})

unlink(c(a, b, c))
Expand Down
18 changes: 8 additions & 10 deletions tests/testthat/test-extraction_operator_linter.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
context("extraction_operator_linter")

test_that("linter returns the correct linting", {
linter <- extraction_operator_linter
msgB <- rex::escape("Use `[[` instead of `[` to extract an element.")
msgD <- rex::escape("Use `[[` instead of `$` to extract an element.")
msg_b <- rex::escape("Use `[[` instead of `[` to extract an element.")
msg_d <- rex::escape("Use `[[` instead of `$` to extract an element.")

expect_lint("x[[1]]", NULL, linter)
expect_lint("x[-1]", NULL, linter)
expect_lint("x[1, 'a']", NULL, linter)
expect_lint("self$a", NULL, linter)
expect_lint(".self $\na", NULL, linter)
expect_lint("x$a", list(message=msgD, line_number=1L, column_number=2L), linter)
expect_lint("x $\na", list(message=msgD, line_number=1L, column_number=3L), linter)
expect_lint("x[NULL]", list(message=msgB, line_number=1L, column_number=2L), linter)
expect_lint("x[++ + 3]", list(message=msgB, line_number=1L, column_number=2L), linter)
expect_lint("x$a", list(message = msg_d, line_number = 1L, column_number = 2L), linter)
expect_lint("x $\na", list(message = msg_d, line_number = 1L, column_number = 3L), linter)
expect_lint("x[NULL]", list(message = msg_b, line_number = 1L, column_number = 2L), linter)
expect_lint("x[++ + 3]", list(message = msg_b, line_number = 1L, column_number = 2L), linter)
expect_lint("c(x['a'], x [ 1 ])",
list(
list(message=msgB, line_number=1L, column_number=4L),
list(message=msgB, line_number=1L, column_number=13L)
list(message = msg_b, line_number = 1L, column_number = 4L),
list(message = msg_b, line_number = 1L, column_number = 13L)
),
linter)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ test_that("tab positions have been corrected", {

test_that("Terminal newlines are detected correctly", {
writeLines("lm(y ~ x)", tmp <- tempfile())
on.exit(unlink(tmp), add=TRUE)
on.exit(unlink(tmp), add = TRUE)
writeBin(
# strip the last (two) element(s) (\r\n or \n)
head(readBin(tmp, raw(), file.size(tmp)), if (.Platform$OS.type == "windows") -2L else -1L),
tmp2 <- tempfile()
)
on.exit(unlink(tmp2), add=TRUE)
on.exit(unlink(tmp2), add = TRUE)

expect_true(get_source_expressions(tmp)$expressions[[2L]]$terminal_newline)
expect_false(get_source_expressions(tmp2)$expressions[[2L]]$terminal_newline)
Expand Down
32 changes: 15 additions & 17 deletions tests/testthat/test-implicit_integer_linter.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
context("implicit_integer_linter")

test_that("single numerical constants are properly identified ", {
# Test single numerical constants
is_implicit <- lintr:::is_implicit_integer

x <- c("Inf", "NaN", "TRUE", "FALSE", "NA", "NA_character")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
x <- c("Inf", "NaN", "TRUE", "FALSE", "NA", "NA_character")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_equal(is_implicit(x), y)

x <- c("2.000", "2.", "2L", "2.0", "2.1", "2")
y <- c( FALSE, FALSE, FALSE, FALSE, FALSE, TRUE)
x <- c("2.000", "2.", "2L", "2.0", "2.1", "2")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE)
expect_equal(is_implicit(x), y)

# 1000 1000L 1000L 1200* 0.0012 0.001 0.0... 1.2
x <- c("1e3", "1e3L", "1.0e3L", "1.2e3", "1.2e-3", "1e-3", "1e-33", "1.2e0")
y <- c( TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
y <- c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_equal(is_implicit(x), y)

# 1* 123L 123* 123.1
x <- c("0x1p+0", "0x1.ecp+6L", "0x1.ecp+6", "0x1.ec66666666666p+6")
y <- c( FALSE, FALSE, FALSE, FALSE)
y <- c(FALSE, FALSE, FALSE, FALSE)
expect_equal(is_implicit(x), y)

x <- c( "8i", "8.0i")
y <- c( FALSE, FALSE)
x <- c("8i", "8.0i")
y <- c(FALSE, FALSE)
expect_equal(is_implicit(x), y)

max <- .Machine[["integer.max"]] # largest number that R can represent as an integer
x <- as.character(c(-max - 1.0, -max, max, max + 1.0))
y <- c( FALSE, TRUE, TRUE, FALSE)
x <- as.character(c(-max - 1.0, -max, max, max + 1.0))
y <- c(FALSE, TRUE, TRUE, FALSE)
expect_equal(is_implicit(x), y)

# Note: cases indicated by "*" should be TRUE but they are complicated to handle, and it is not
Expand All @@ -41,13 +39,13 @@ test_that("linter returns the correct linting", {

expect_lint("x <<- 1L", NULL, linter)
expect_lint("1.0/-Inf -> y", NULL, linter)
expect_lint("y <- 1+i", list(message=msg, line_number=1L, column_number=7L), linter)
expect_lint("z <- 1e5", list(message=msg, line_number=1L, column_number=9L), linter)
expect_lint("cat(1:n)", list(message=msg, line_number=1L, column_number=6L), linter)
expect_lint("y <- 1+i", list(message = msg, line_number = 1L, column_number = 7L), linter)
expect_lint("z <- 1e5", list(message = msg, line_number = 1L, column_number = 9L), linter)
expect_lint("cat(1:n)", list(message = msg, line_number = 1L, column_number = 6L), linter)
expect_lint("552^9",
list(
list(message=msg, line_number=1L, column_number=4L),
list(message=msg, line_number=1L, column_number=6L)
list(message = msg, line_number = 1L, column_number = 4L),
list(message = msg, line_number = 1L, column_number = 6L)
),
linter)
})
7 changes: 2 additions & 5 deletions tests/testthat/test-nonportable_path_linter.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("nonportable_path_linter")


test_that("Non-portable path linter", {
linter <- nonportable_path_linter(lax=FALSE)
linter <- nonportable_path_linter(lax = FALSE)
msg <- rex::escape("Use file.path() to construct portable file paths.")

# various strings
Expand Down Expand Up @@ -44,7 +41,7 @@ test_that("Non-portable path linter", {
}

# lax mode: no check for strings that are likely not paths (too short or with special characters)
linter <- nonportable_path_linter(lax=TRUE)
linter <- nonportable_path_linter(lax = TRUE)

unlikely_path_strings <- c(
"/foo", encodeString("/a\nsdf/bar"), "/as:df/bar"
Expand Down
Loading