Skip to content

Commit 4763c82

Browse files
committed
Add root to remaining hooks. Issues with roxygenize tests
1 parent 83914bd commit 4763c82

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#!/usr/bin/env Rscript
22

3+
"A hook to make sure DESCRIPTION hasn’t been edited more recently than
4+
codemeta.json.
5+
6+
Usage:
7+
codemeta-description-updated [--root=<root_>] <_ignored>...
8+
9+
Options:
10+
--root=<root_> Path relative to the git root that contains the R package
11+
root [default: .].
12+
13+
" -> doc
14+
15+
16+
arguments <- docopt::docopt(doc)
17+
setwd(arguments$root)
18+
319
# adapted from https://github.com/lorenzwalthert/precommit/blob/f4413cfe6282c84f7176160d06e1560860c8bd3d/inst/hooks/exported/readme-rmd-rendered
420
if (!file.exists("DESCRIPTION")) {
521
rlang::abort("No `DESCRIPTION` found in repository.")

inst/hooks/exported/roxygenize.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ This check should run *after* check that modify the files that are passed to
1414
them (like styler) because they will never modify their input .R files.
1515
1616
Usage:
17-
roxygenize [--no-warn-cache] <files>...
17+
roxygenize [--no-warn-cache] [--root=<root_>] <files>...
1818
1919
Options:
2020
--no-warn-cache Suppress the warning about a missing permanent cache.
21+
--root=<root_> Path relative to the git root that contains the R package root [default: .].
2122
2223
" -> doc
2324
arguments <- docopt::docopt(doc)
25+
arguments$files <- normalizePath(arguments$files) # because working directory changes to root
26+
setwd(arguments$root)
27+
2428
if (packageVersion("precommit") < "0.1.3.9010") {
2529
rlang::abort(paste(
2630
"This hooks only works with the R package {precommit} >= 0.1.3.9010",

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
#!/usr/bin/env Rscript
2+
3+
"A hook to run usethis::use_tidy_description() to ensure dependencies are
4+
ordered alphabetically and fields are in standard order.
5+
6+
Usage:
7+
use-tidy-description [--root=<root_>] <_ignored>...
8+
9+
Options:
10+
--root=<root_> Path relative to the git root that contains the R package root [default: .].
11+
12+
" -> doc
13+
14+
15+
arguments <- docopt::docopt(doc)
16+
setwd(arguments$root)
17+
18+
if (!file.exists("DESCRIPTION")) {
19+
rlang::abort("No `DESCRIPTION` found in repository.")
20+
}
21+
222
description <- desc::description$new()
323
description_old <- description$clone(deep = TRUE)
424
deps <- description$get_deps()

tests/testthat/test-hooks.R

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
# success
55
run_test("use-tidy-description", "DESCRIPTION", suffix = "")
66

7+
# in sub directory with correct root
8+
run_test("use-tidy-description",
9+
"DESCRIPTION",
10+
suffix = "",
11+
cmd_args="--root=rpkg",
12+
artifacts = c("rpkg/DESCRIPTION" = test_path("in/DESCRIPTION")))
13+
14+
15+
16+
# in sub directory with incorrect root
17+
# Need to generate the directoy `rpkg` but without DESCRIPTION file.
18+
run_test("use-tidy-description",
19+
"DESCRIPTION",
20+
suffix = "",
21+
cmd_args="--root=rpkg",
22+
std_err="No `DESCRIPTION` found in repository.",
23+
artifacts = c("rpkg/README.md" = test_path("in/README.md")))
24+
725
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
826
### style-files ####
927

@@ -373,20 +391,58 @@ run_test("roxygenize",
373391
}
374392
)
375393

394+
# with outdated Rd present in correct root
395+
run_test("roxygenize",
396+
file_name = c("rpkg/man/flie.Rd" = "flie.Rd"),
397+
suffix = "",
398+
std_err = NA,
399+
cmd_args="--root=rpkg",
400+
std_out = "Writing NAMESPACE",
401+
artifacts = c(
402+
"rpkg/DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"),
403+
"rpkg/R/roxygenize.R" = test_path("in/roxygenize.R")
404+
),
405+
file_transformer = function(files) {
406+
git_init()
407+
git2r::add(path = files)
408+
# hack to add artifact to trigger diff_requires_roxygenize()
409+
git2r::add(path = fs::path(fs::path_dir(fs::path_dir(files[1])), "rpkg/R"))
410+
files
411+
}
412+
)
413+
376414

377415
# without Rd present
378416
run_test("roxygenize",
379-
file_name = c("R/roxygenize.R" = "roxygenize.R"),
417+
file_name = c("rpkg1/R/roxygenize.R" = "roxygenize.R"),
380418
suffix = "",
419+
cmd_args="--root=rpkg1",
381420
std_err = "Please commit the new `.Rd` files",
382421
artifacts = c(
383-
"DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf")
422+
"rpkg1/DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"),
423+
"rpkg2/R/roxygenize.R" = test_path("in/roxygenize.R")
384424
),
385425
file_transformer = function(files) {
386426
git_init()
387427
git2r::add(path = files)
388428
files
389429
}
430+
431+
)
432+
433+
# with Rd present in wrong root
434+
run_test("roxygenize",
435+
file_name = c("R/roxygenize.R" = "roxygenize.R"),
436+
suffix = "",
437+
std_err = "Please commit the new `.Rd` files",
438+
artifacts = c(
439+
"DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf")
440+
),
441+
file_transformer = function(files) {
442+
git_init()
443+
git2r::add(path = files)
444+
files
445+
}
390446
)
391447

392448

@@ -458,6 +514,42 @@ run_test("codemeta-description-update",
458514
}
459515
)
460516

517+
# succeed in correct root
518+
run_test("codemeta-description-update",
519+
file_name = c("rpkg/DESCRIPTION" = "DESCRIPTION",
520+
"rpkg/codemeta.json" = "codemeta.json"),
521+
cmd_args="--root=rpkg",
522+
suffix = "",
523+
file_transformer = function(files) {
524+
if (length(files) > 1) {
525+
# transformer is called once on all files and once per file
526+
content_2 <- readLines(files[2])
527+
Sys.sleep(2)
528+
writeLines(content_2, files[2])
529+
}
530+
files
531+
}
532+
)
533+
534+
# # fail in wrong root
535+
run_test("codemeta-description-update",
536+
file_name = c("rpkg/DESCRIPTION" = "DESCRIPTION",
537+
"rpkg/codemeta.json" = "codemeta.json",
538+
"rpkg2/codemeta.json" = "README.md"),
539+
cmd_args="--root=rpkg2",
540+
std_err="No `DESCRIPTION` found in repository.",
541+
suffix = "",
542+
file_transformer = function(files) {
543+
if (length(files) > 1) {
544+
# transformer is called once on all files and once per file
545+
content_2 <- readLines(files[2])
546+
Sys.sleep(2)
547+
writeLines(content_2, files[2])
548+
}
549+
files
550+
}
551+
)
552+
461553
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
462554
### readme-rmd-rendered ####
463555
if (has_git()) {

0 commit comments

Comments
 (0)