Skip to content

Commit 4f1a7e5

Browse files
Merge pull request #529 from TymekDev/run-test-update
Add read_only flag to run_test
2 parents 0a0f75c + 9a99319 commit 4f1a7e5

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

R/testing.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ run_test <- function(hook_name,
4343
artifacts = NULL,
4444
file_transformer = function(files) files,
4545
env = character(),
46-
expect_success = is.null(std_err)) {
46+
expect_success = is.null(std_err),
47+
read_only = FALSE) {
4748
withr::local_envvar(list(R_PRECOMMIT_HOOK_ENV = "1"))
4849
path_executable <- fs::dir_ls(system.file(
4950
fs::path("hooks", "exported"),
@@ -62,7 +63,8 @@ run_test <- function(hook_name,
6263
artifacts = ensure_named(artifacts),
6364
file_transformer = file_transformer,
6465
env = env,
65-
expect_success = expect_success
66+
expect_success = expect_success,
67+
read_only = read_only
6668
)
6769
}
6870

@@ -84,6 +86,8 @@ run_test <- function(hook_name,
8486
#' @param expect_success Whether or not an exit code 0 is expected. This can
8587
#' be derived from `std_err`, but sometimes, non-empty stderr does not mean
8688
#' 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.
8791
#' @keywords internal
8892
run_test_impl <- function(path_executable,
8993
path_candidate,
@@ -93,7 +97,8 @@ run_test_impl <- function(path_executable,
9397
artifacts,
9498
file_transformer,
9599
env,
96-
expect_success) {
100+
expect_success,
101+
read_only) {
97102
# ensure cannonical /private/var/... not /var/... on macOS
98103
tempdir <- fs::path(normalizePath((fs::dir_create(fs::file_temp()))))
99104
copy_artifacts(artifacts, tempdir)
@@ -128,6 +133,13 @@ run_test_impl <- function(path_executable,
128133
std_out,
129134
exit_status
130135
)
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+
})
142+
}
131143
}
132144

133145

man/run_test.Rd

Lines changed: 5 additions & 1 deletion
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: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/hook-order.Rmd

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@ Read only hooks should generally run only after write hooks.
2121
- Only add a dependency once, i.e. if styler must run before roxygen, add the requirement to styler or roxygen, not both. This makes keeping track easier.
2222
- The hooks must appear in an order that meets all constraints, not just randomly order constraints.
2323

24-
## Hooks with dependencies:
24+
## Hooks with dependencies
2525

26-
**Read and write**
26+
### Read and write
2727

28-
- styler: should run before roxygen because of caching. Caches.
29-
- roxygen. Caches.
30-
- codemeta: must be before tidy description.
31-
- use-tidy-description.
28+
- style-files - caches; should run before roxygenize because of the caching
29+
- roxygenize - caches
30+
- codemeta-description-updated - must be before use-tidy-description
31+
- use-tidy-description
32+
- spell-check - updates `inst/WORDLIST`; should run after roxygenize
3233

33-
**Just read:**
34+
### Read only
3435

35-
- spell check; run after roxygen
36-
- lintr: should run after styler.
37-
- readme-rmd-rendered: Must run after styler.
36+
- lintr - should run after styler
37+
- readme-rmd-rendered - must run after styler
3838
- parsable-R
3939
- no-browser-statement
4040
- no-print-statement
41-
- deps in desc.
41+
- no-debug-statement
42+
- deps-in-desc
43+
- pkgdown

0 commit comments

Comments
 (0)