Skip to content

Commit c6099d5

Browse files
Avoid implicit assignments in expectations (#1879)
* getting started * use magrittr pipe instead * handle a few more * a few more * and some more * Update test-xml_nodes_to_lints.R * fix indentation lints * remove magrittr brace syntax in favour of tempvar * don't gain dependency on magrittr
1 parent 020c4b6 commit c6099d5

12 files changed

+126
-59
lines changed

tests/testthat/test-backport_linter.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ test_that("backport_linter detects backwards-incompatibility", {
1212
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter("release"))
1313
expect_lint(".getNamespaceInfo(dir.exists(lapply(x, toTitleCase)))", NULL, backport_linter("devel"))
1414

15-
# don't allow dependencies older than we've recorded
16-
tmp <- withr::local_tempfile(lines = "x <- x + 1")
17-
18-
expect_warning(l <- lint(tmp, backport_linter("2.0.0")), "version older than 3.0.0", fixed = TRUE)
19-
expect_identical(l, lint(tmp, backport_linter("3.0.0")))
20-
2115
expect_lint(
2216
"numToBits(2)",
2317
rex::rex("numToBits (R 4.1.0) is not available for dependency R >= 4.0.0."),
@@ -64,3 +58,16 @@ test_that("backport_linter detects backwards-incompatibility", {
6458
backport_linter("3.0.0", except = c("numToBits", "R_user_dir"))
6559
)
6660
})
61+
62+
test_that("backport_linter generates expected warnings", {
63+
tmp <- withr::local_tempfile(lines = "x <- x + 1")
64+
65+
expect_warning(
66+
{
67+
l <- lint(tmp, backport_linter("2.0.0"))
68+
},
69+
"version older than 3.0.0",
70+
fixed = TRUE
71+
)
72+
expect_identical(l, lint(tmp, backport_linter("3.0.0")))
73+
})

tests/testthat/test-cache.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,19 @@ test_that("load_cache returns an empty environment if reading cache file fails",
9292
cache_f1 <- file.path(d1, fhash(f1))
9393
writeLines(character(), cache_f1)
9494

95-
expect_warning(e2 <- lintr:::load_cache(file = f1, path = d1), "Could not load cache file")
95+
expect_warning(
96+
{
97+
e2 <- lintr:::load_cache(file = f1, path = d1)
98+
},
99+
"Could not load cache file"
100+
)
96101
saveRDS(e1, cache_f1)
97-
expect_warning(e3 <- lintr:::load_cache(file = f1, path = d1), "Could not load cache file")
98-
102+
expect_warning(
103+
{
104+
e3 <- lintr:::load_cache(file = f1, path = d1)
105+
},
106+
"Could not load cache file"
107+
)
99108
expect_identical(ls(e2), character())
100109
expect_identical(ls(e3), character())
101110
})

tests/testthat/test-closed_curly_linter.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ test_that("returns the correct linting", {
77
)
88

99
expect_warning(
10-
linter <- closed_curly_linter(),
10+
{
11+
linter <- closed_curly_linter()
12+
},
1113
"Linter closed_curly_linter was deprecated",
1214
fixed = TRUE
1315
)

tests/testthat/test-dir_linters.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ test_that("lint_dir works with specific linters without specifying other argumen
9595

9696
test_that("lint_dir continues to accept relative_path= in 2nd positional argument, with a warning", {
9797
the_dir <- test_path("dummy_packages", "package", "vignettes")
98+
9899
expect_warning(
99-
positional_lints <- lint_dir(the_dir, FALSE),
100+
{
101+
positional_lints <- lint_dir(the_dir, FALSE)
102+
},
100103
"'relative_path' is no longer available as a positional argument",
101104
fixed = TRUE
102105
)

tests/testthat/test-get_source_expressions.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,16 @@ param_df <- expand.grid(
336336
expression_idx = seq_along(expressions),
337337
stringsAsFactors = FALSE
338338
)
339-
param_df$.test_name <-
340-
with(param_df, sprintf("%s on expression %d", linter, expression_idx))
339+
param_df$.test_name <- with(param_df, sprintf("%s on expression %d", linter, expression_idx))
341340

342341
patrick::with_parameters_test_that(
343342
"linters pass with xml_missing() content",
344343
{
345344
linter <- eval(call(linter))
346345
expression <- expressions[[expression_idx]]
347-
expect_no_warning(lints <- linter(expression))
346+
expect_no_warning({
347+
lints <- linter(expression)
348+
})
348349
expect_length(lints, 0L)
349350
},
350351
.test_name = param_df$.test_name,

tests/testthat/test-ids_with_token.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ test_that("ids_with_token works as expected", {
66

77
# deprecated argument
88
expect_warning(
9-
old_arg <- ids_with_token(source_file = source_expression, value = "expr"),
9+
{
10+
old_arg <- ids_with_token(source_file = source_expression, value = "expr")
11+
},
1012
"Argument source_file was deprecated"
1113
)
1214
expect_identical(old_arg, ref)

tests/testthat/test-lint_file.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ test_that("compatibility warnings work", {
221221

222222
test_that("Deprecated positional usage of cache= works, with warning", {
223223
expect_warning(
224-
l <- lint("a = 2\n", FALSE, linters = assignment_linter()),
224+
{
225+
l <- lint("a = 2\n", FALSE, linters = assignment_linter())
226+
},
225227
"'cache' is no longer available as a positional argument",
226228
fixed = TRUE
227229
)

tests/testthat/test-lint_package.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ test_that(
132132
test_that("lint_package returns early if no package is found", {
133133
temp_pkg <- withr::local_tempdir("dir")
134134

135-
expect_warning(l <- lint_package(temp_pkg), "Didn't find any R package", fixed = TRUE)
135+
expect_warning(
136+
{
137+
l <- lint_package(temp_pkg)
138+
},
139+
"Didn't find any R package",
140+
fixed = TRUE
141+
)
136142
expect_null(l)
137143

138144
# ignore a folder named DESCRIPTION, #702

tests/testthat/test-methods.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ test_that("split.lint works as intended", {
143143

144144
test_that("within.list is dispatched", {
145145
l <- lint(text = "a=1\nb=2", linters = infix_spaces_linter())
146-
expect_silent(l <- lapply(l, within, line_number <- line_number + 1L))
146+
expect_silent({
147+
l <- lapply(l, within, line_number <- line_number + 1L)
148+
})
147149
expect_identical(vapply(l, `[[`, integer(1L), "line_number"), 2L:3L)
148150
})

tests/testthat/test-with.R

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,19 @@ test_that("can instantiate all linters without arguments", {
6868
test_that("with_defaults is supported with a deprecation warning", {
6969
defaults <- linters_with_defaults()
7070
expect_warning(
71-
old_defaults <- with_defaults(),
71+
{
72+
old_defaults <- with_defaults()
73+
},
7274
rex::rex("Use linters_with_defaults or modify_defaults instead.")
7375
)
7476
expect_identical(defaults, old_defaults)
7577

7678
# linters_with_defaults only accepts `defaults = list()` to start from blank
7779
defaults <- linters_with_defaults(defaults = list(), no_tab_linter())
7880
expect_warning(
79-
old_defaults <- with_defaults(default = NULL, no_tab_linter()),
81+
{
82+
old_defaults <- with_defaults(default = NULL, no_tab_linter())
83+
},
8084
rex::rex("Use linters_with_defaults or modify_defaults instead.")
8185
)
8286
expect_identical(defaults, old_defaults)
@@ -93,16 +97,25 @@ test_that("modify_defaults works", {
9397
})
9498

9599
test_that("linters_with_defaults(default = .) is supported with a deprecation warning", {
96-
expect_warning(linters <- linters_with_defaults(default = list(), no_tab_linter()), "'default'")
100+
expect_warning(
101+
{
102+
linters <- linters_with_defaults(default = list(), no_tab_linter())
103+
},
104+
"'default'"
105+
)
97106
expect_named(linters, "no_tab_linter")
98107

99108
# the same warning is not triggered in modify_defaults
100-
expect_silent(linters <- modify_defaults(defaults = list(), default = list(), no_tab_linter()))
109+
expect_silent({
110+
linters <- modify_defaults(defaults = list(), default = list(), no_tab_linter())
111+
})
101112
expect_named(linters, c("default", "no_tab_linter"))
102113

103114
# if default= is explicitly provided alongside defaults=, assume that was intentional
104115
default <- Linter(function(.) list())
105-
expect_silent(linters <- linters_with_defaults(defaults = list(), default = default))
116+
expect_silent({
117+
linters <- linters_with_defaults(defaults = list(), default = default)
118+
})
106119
expect_named(linters, "default")
107120
})
108121

0 commit comments

Comments
 (0)