Skip to content

Commit e989416

Browse files
Merge pull request #516 from kcphila/revised-add-spaces
Revised add spaces
2 parents 98cb63a + 44ed0e2 commit e989416

21 files changed

+121
-9
lines changed

API

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open_config(root = here::here())
1111
open_wordlist(root = here::here())
1212
path_pre_commit_exec(check_if_exists = TRUE)
1313
path_precommit_exec(check_if_exists = TRUE)
14+
precommit_docopt(doc, args = commandArgs(trailingOnly = TRUE), ...)
1415
robust_purl(path)
1516
roxygen_assert_additional_dependencies()
1617
roxygenize_with_cache(key, dirs)

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(open_config)
99
export(open_wordlist)
1010
export(path_pre_commit_exec)
1111
export(path_precommit_exec)
12+
export(precommit_docopt)
1213
export(robust_purl)
1314
export(roxygen_assert_additional_dependencies)
1415
export(roxygenize_with_cache)

R/testing.R

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ hook_state_create <- function(tempdir,
146146
withr::local_dir(tempdir)
147147
files <- fs::path_rel(path_candidate_temp, tempdir)
148148
# https://stat.ethz.ch/pipermail/r-devel/2018-February/075507.html
149+
150+
# quote any individual filenames with spaces so the shell identifies them
151+
# each as a single term
152+
files <- shQuote(files)
153+
149154
system2(paste0(Sys.getenv("R_HOME"), "/bin/Rscript"),
150155
args = as.character(c(path_executable, cmd_args, files)),
151156
stderr = path_stderr, stdout = path_stdout, env = env

R/utils.R

+21
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ git_init <- function(path = ".") {
8383
)
8484
}
8585

86+
#' Provide a singular interface for hook calls to docopt
87+
#'
88+
#' docopt provides different processing for a single string
89+
#' than an array/vector. As `"string"`` and `c("string")`
90+
#' are semantically equivalent in R, this can create problems
91+
#' when a single parameter is provided. Thus, this function
92+
#' wraps docopt to ensure that the args will always be
93+
#' interpreted as a vector.
94+
#'
95+
#' This function is only exported for use in hook scripts, but it's not intended
96+
#' to be called by the end-user directly.
97+
#' @param doc `character` vector with command line specification.
98+
#' @param args `character` vector of command line arguments.
99+
#' Defaults to `commandArgs(trailingOnly=TRUE)`.
100+
#' @param ... Additional parameters passed to `docopt`.
101+
#' @family hook script helpers
102+
#' @keywords internal
103+
#' @export
104+
precommit_docopt <- function(doc, args = commandArgs(trailingOnly = TRUE), ...) {
105+
docopt::docopt(doc, c(args, ""), ...)
106+
}
86107

87108
#' Read the refs corresponding to a hooks repo
88109
#' @keywords internal

inst/hooks/exported/codemeta-description-updated.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Options:
1313
" -> doc
1414

1515

16-
arguments <- docopt::docopt(doc)
16+
arguments <- precommit::precommit_docopt(doc)
1717
setwd(arguments$root)
1818

1919
# adapted from https://github.com/lorenzwalthert/precommit/blob/f4413cfe6282c84f7176160d06e1560860c8bd3d/inst/hooks/exported/readme-rmd-rendered

inst/hooks/exported/deps-in-desc.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pre_installed <- c(
1717
"survival", "tcltk", "tools", "utils"
1818
)
1919

20-
arguments <- docopt::docopt(doc)
20+
arguments <- precommit::precommit_docopt(doc)
2121
arguments$files <- normalizePath(arguments$files) # because working directory changes to root
2222
setwd(normalizePath(arguments$root))
2323
deps_in_desc <- function(file, arguments) {

inst/hooks/exported/lintr.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Options:
1010
Otherwise, lints will never be shown to the user.
1111
" -> doc
1212

13-
arguments <- docopt::docopt(doc)
13+
arguments <- precommit::precommit_docopt(doc)
1414

1515
lintr_staged <- grepl(
1616
"modified:.*\\.lintr", system2("git", "status", stdout = TRUE)

inst/hooks/exported/roxygenize.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Options:
2121
--root=<root_> Path relative to the git root that contains the R package root [default: .].
2222
2323
" -> doc
24-
arguments <- docopt::docopt(doc)
24+
arguments <- precommit::precommit_docopt(doc)
2525
arguments$files <- normalizePath(arguments$files) # because working directory changes to root
2626
setwd(arguments$root)
2727

inst/hooks/exported/spell-check.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Options:
99
1010
" -> doc
1111

12-
arguments <- docopt::docopt(doc)
12+
arguments <- precommit::precommit_docopt(doc)
1313
path_wordlist <- file.path("inst", "WORDLIST")
1414
files <- arguments$files
1515
if (file.exists(path_wordlist)) {

inst/hooks/exported/style-files.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if (length(keys) > 0) {
3838
doc <- gsub("<files>...", insert, paste0(doc, paste(key_value_pairs, collapse = "\n")))
3939
}
4040

41-
arguments <- docopt::docopt(doc, args)
41+
arguments <- precommit::precommit_docopt(doc, args)
4242
if (packageVersion("styler") < "1.3.2") {
4343
stop(
4444
"Your styler version is outdated. ",

inst/hooks/exported/use-tidy-description.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Options:
1212
" -> doc
1313

1414

15-
arguments <- docopt::docopt(doc)
15+
arguments <- precommit::precommit_docopt(doc)
1616
setwd(arguments$root)
1717

1818
if (!file.exists("DESCRIPTION")) {

inst/hooks/local/consistent-release-tag.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Usage:
66
consistent-release-tag [--release-mode] [<files>...]
77
88
" -> doc
9-
arguments <- docopt::docopt(doc)
9+
arguments <- precommit::precommit_docopt(doc)
1010

1111

1212
# This hook checks that all versions in config files and the git tag that

man/diff_requires_run_roxygenize.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dirs_R.cache.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/may_require_permanent_cache.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/precommit_docopt.Rd

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/robust_purl.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/roxygen_assert_additional_dependencies.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/roxygenize_with_cache.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-docopt.R

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
test_that("custom docopt interface parses as expected", {
2+
args_variants <- list(
3+
"file a.R",
4+
c("file a.R", "file-B.R"),
5+
c("File A.R", "File c.R"),
6+
c("Another file with spaces.R", "--warn_only"),
7+
c("Another file with spaces.R", "Yet another file with spaces (YAFWS).R", "--warn_only"),
8+
c("--warn_only", "Another file with spaces.R"),
9+
c("--warn_only", "Another file with spaces.R", "Yet another file with spaces (YAFWS).R"),
10+
c("Another file with spaces.R", "--warn_only", "Yet another file with spaces (YAFWS).R")
11+
)
12+
13+
"Run lintr on R files during a precommit.
14+
Usage:
15+
cmdtest [--warn_only] <files>...
16+
Options:
17+
--warn_only Placeholder for test.
18+
" -> doc
19+
20+
for (args in args_variants) {
21+
new_args <- precommit_docopt(doc, args)
22+
23+
# to show failures in vanilla docopt, use this:
24+
# new_args <- docopt::docopt(doc, args)
25+
26+
expect_equal(length(new_args), 4)
27+
if ("--warn_only" %in% args) {
28+
expect_equal(length(new_args$files), length(args) - 1)
29+
expect_equal(length(new_args$`<files>`), length(args) - 1)
30+
expect_true(new_args$warn_only)
31+
expect_true(new_args$`--warn_only`)
32+
} else {
33+
expect_equal(length(new_args$files), length(args))
34+
expect_equal(length(new_args$`<files>`), length(args))
35+
expect_false(new_args$warn_only)
36+
expect_false(new_args$`--warn_only`)
37+
}
38+
}
39+
})

tests/testthat/test-hooks.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ run_test("style-files",
140140
)
141141
)
142142

143-
144143
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
145144
### no-browser-statement ####
146145
# success
@@ -214,6 +213,7 @@ run_test(
214213
std_err = "1 1"
215214
)
216215

216+
217217
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
218218
### spell-check ####
219219
# success

0 commit comments

Comments
 (0)