Skip to content

Commit 726b3e9

Browse files
Merge pull request #511 from TymekDev/main
Add --no-update flag to spell-check
2 parents cc98ff4 + 4406ac2 commit 726b3e9

File tree

8 files changed

+81
-38
lines changed

8 files changed

+81
-38
lines changed

R/testing.R

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ run_test <- function(hook_name,
8686
#' @param expect_success Whether or not an exit code 0 is expected. This can
8787
#' be derived from `std_err`, but sometimes, non-empty stderr does not mean
8888
#' error, but just a message.
89-
#' @param read_only If `TRUE` and `artifacts` are not `NULL`, then assert that hook
90-
#' did not modify the artifacts.
89+
#' @param read_only If `TRUE`, then assert that no new files were created.
90+
#' Additionally, if `artifacts` are not `NULL`, then assert that hook did not
91+
#' modify the artifacts.
9192
#' @keywords internal
9293
run_test_impl <- function(path_executable,
9394
path_candidate,
@@ -112,6 +113,7 @@ run_test_impl <- function(path_executable,
112113
)
113114
path_stderr <- tempfile()
114115
path_stdout <- tempfile()
116+
files_before_hook <- fs::dir_ls(tempdir, all = TRUE, recurse = TRUE)
115117
exit_status <- hook_state_create(
116118
tempdir,
117119
path_candidate_temp,
@@ -133,12 +135,17 @@ run_test_impl <- function(path_executable,
133135
std_out,
134136
exit_status
135137
)
136-
if (isTRUE(read_only) && !is.null(artifacts)) {
137-
purrr::iwalk(artifacts, function(reference_path, temp_path) {
138-
artifact_before_hook <- readLines(testthat::test_path(reference_path))
139-
artifact_after_hook <- readLines(fs::path_join(c(tempdir, temp_path)))
140-
testthat::expect_equal(artifact_before_hook, artifact_after_hook)
141-
})
138+
if (isTRUE(read_only)) {
139+
files_after_hook <- fs::dir_ls(tempdir, all = TRUE, recurse = TRUE)
140+
testthat::expect_equal(files_before_hook, files_after_hook)
141+
142+
if (!is.null(artifacts)) {
143+
purrr::iwalk(artifacts, function(reference_path, temp_path) {
144+
artifact_before_hook <- readLines(reference_path)
145+
artifact_after_hook <- readLines(fs::path_join(c(tempdir, temp_path)))
146+
testthat::expect_equal(artifact_before_hook, artifact_after_hook)
147+
})
148+
}
142149
}
143150
}
144151

inst/hooks/exported/spell-check.R

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
"Spell check for files
44
Usage:
5-
spell-check [--lang=<language>] <files>...
5+
spell-check [--lang=<language>] [--read-only] <files>...
66
77
Options:
88
--lang=<language> Passed to `spelling::spell_check_files()` [default: en_US]
9+
--read-only Don't update inst/WORDLIST with errors (new words)
910
1011
" -> doc
1112

@@ -16,10 +17,12 @@ if (file.exists(path_wordlist)) {
1617
ignore <- readLines(path_wordlist, encoding = "UTF-8")
1718
action <- "update"
1819
} else {
19-
if (!dir.exists(dirname(path_wordlist))) {
20-
dir.create(dirname(path_wordlist))
20+
if (isFALSE(arguments$read_only)) {
21+
if (!dir.exists(dirname(path_wordlist))) {
22+
dir.create(dirname(path_wordlist))
23+
}
24+
file.create(path_wordlist)
2125
}
22-
file.create(path_wordlist)
2326
ignore <- character()
2427
action <- "create"
2528
}
@@ -34,24 +37,26 @@ spelling_errors <- spelling::spell_check_files(
3437
if (nrow(spelling_errors) > 0) {
3538
cat("The following spelling errors were found:\n")
3639
print(spelling_errors)
37-
ignore_df <- data.frame(
38-
original = unique(c(ignore, spelling_errors$word))
39-
)
40-
ignore_df$lower <- tolower(ignore_df$original)
41-
ignore_df <- ignore_df[order(ignore_df$lower, method = "radix"), ]
42-
ignore <- ignore_df$original[ignore_df$lower != ""] # drop blanks if any
43-
writeLines(ignore, path_wordlist)
44-
cat(
45-
"All spelling errors found were copied to inst/WORDLIST assuming they were",
46-
"not spelling errors and will be ignored in the future. Please ",
47-
"review the above list and for each word that is an actual typo:\n",
48-
"- fix it in the source code.\n",
49-
"- remove it again manually from inst/WORDLIST to make sure it's not\n",
50-
" ignored in the future.\n",
51-
"Then, try committing again.\n"
52-
)
40+
if (isTRUE(arguments$read_only)) {
41+
cat("Hint: you can enable an automatic updating of WORDLIST with errors (new words) by removing the --read-only flag.\n")
42+
} else {
43+
ignore_df <- data.frame(
44+
original = unique(c(ignore, spelling_errors$word))
45+
)
46+
ignore_df$lower <- tolower(ignore_df$original)
47+
ignore_df <- ignore_df[order(ignore_df$lower, method = "radix"), ]
48+
ignore <- ignore_df$original[ignore_df$lower != ""] # drop blanks if any
49+
writeLines(ignore, path_wordlist)
50+
cat(
51+
"All spelling errors found were copied to inst/WORDLIST assuming they were",
52+
"not spelling errors and will be ignored in the future. Please",
53+
"review the above list and for each word that is an actual typo:\n",
54+
"- fix it in the source code.\n",
55+
"- remove it again manually from inst/WORDLIST to make sure it's not\n",
56+
" ignored in the future.\n",
57+
"Then, try committing again.\n",
58+
"Hint: you can disable this behavior by providing a --read-only flag.\n"
59+
)
60+
}
5361
stop("Spell check failed", call. = FALSE)
54-
} else {
55-
ignore <- ignore[ignore != ""] # drop blanks if any
56-
writeLines(ignore, path_wordlist)
5762
}

man/run_test.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_test_impl.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# a ttle
2+
3+
This is a fsssile.

tests/testthat/test-hooks.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,24 @@ run_test(
219219
# success
220220
run_test("spell-check", suffix = "-success.md", std_err = NULL)
221221

222-
# basic failure
223-
run_test("spell-check", suffix = "-fail.md", std_err = "Spell check failed")
222+
# failure with --read-only does not create WORDLIST
223+
run_test(
224+
"spell-check",
225+
suffix = "-fail.md",
226+
std_err = "Spell check failed",
227+
cmd_args = "--read-only",
228+
read_only = TRUE
229+
)
230+
231+
# failure with --read-only does not update WORDLIST
232+
run_test(
233+
"spell-check",
234+
suffix = "-fail-2.md",
235+
std_err = "Spell check failed",
236+
cmd_args = "--read-only",
237+
artifacts = c("inst/WORDLIST" = test_path("in/WORDLIST")),
238+
read_only = TRUE
239+
)
224240

225241
# success with wordlist
226242
run_test("spell-check",

vignettes/available-hooks.Rmd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ The `lang` arg will be passed to `spelling::spell_check_files()`.
196196
id: spell-check
197197
args: [--lang=<language>]
198198

199+
**read only**
200+
201+
The `--read-only` flag will be passed to spell check. This flag makes
202+
this hook idempotent.
203+
204+
id: spell-check
205+
args: [--read-only]
206+
199207
This hook does not modify input files. It will add all words not found
200208
in the dictionary to `inst/WORDLIST`, assuming they were spelled
201209
correctly but were not in the dictionary. An example might be "RStudio".
@@ -205,6 +213,8 @@ them and remove them from `inst/WORDLIST`. If there were not typos, or
205213
you fixed all, stage `inst/WORDLIST` and this time, the commit should
206214
pass.
207215

216+
To opt out of updating `inst/WORDLIST` provide the `--read-only` flag.
217+
208218
## `roxygenize`
209219

210220
A hook to run `roxygen2::roxygenize()`. Makes sure you commit your `.Rd`

vignettes/hook-order.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Read only hooks should generally run only after write hooks.
2929
- roxygenize - caches
3030
- codemeta-description-updated - must be before use-tidy-description
3131
- use-tidy-description
32-
- spell-check - updates `inst/WORDLIST`; should run after roxygenize
32+
- spell-check - updates `inst/WORDLIST` (unless `--read-only` arg is specified); should run after roxygenize
3333

3434
### Read only
3535

0 commit comments

Comments
 (0)