Skip to content

Commit e3c21d1

Browse files
committed
refactor: changed the way check_name_consistency detect package name
1 parent 0b93ccd commit e3c21d1

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: golem
22
Title: A Framework for Robust Shiny Applications
3-
Version: 0.5.1.9007
3+
Version: 0.5.1.9008
44
Authors@R: c(
55
person("Colin", "Fay", , "[email protected]", role = c("cre", "aut"),
66
comment = c(ORCID = "0000-0001-7343-1846")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
- Full refactoring of the `add_*_files` and `use_*_files` functions that now all share the same behavior
2727

28+
- The internal `check_name_consistency()` now parses the code of `app_config.R` and get the `package` arg of `system.file`, instead of doing a text based search. This allows the function to detect several calls to `system.file` and fixes the bug from #1179
29+
2830
## Doc
2931

3032
- Vignettes have been renamed

R/reload.R

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,76 @@ detach_all_attached <- function() {
3434
check_name_consistency <- function(pkg) {
3535
old_dir <- setwd(pkg)
3636

37-
package_name <- desc_get(keys = "Package")
37+
package_name_from_desc <- desc_get(keys = "Package")
38+
package_name_from_config <- c()
39+
3840
pth <- fs_path(
3941
pkg,
4042
"R",
4143
"app_config.R"
4244
)
43-
app_config <- readLines(pth)
45+
app_config <- parse(file = pth)
4446

45-
where_system.file <- app_config[
46-
grep(
47-
"system.file",
48-
app_config
49-
)
50-
]
47+
lapply(
48+
app_config,
49+
function(expr) {
50+
codetools::walkCode(
51+
expr,
52+
codetools::makeCodeWalker(
53+
handler = function(e, w) return(NULL),
54+
call = function(expr, w) {
55+
fn <- expr[[1]]
56+
if (
57+
is.symbol(fn) &&
58+
as.character(fn) == "system.file"
59+
) {
60+
args <- as.list(expr)[-1]
61+
package_name_from_config <<- c(
62+
package_name_from_config,
63+
args$package
64+
)
65+
}
66+
for (e in as.list(expr)[-1L]) {
67+
codetools::walkCode(e, w)
68+
}
69+
},
70+
leaf = function(e, w) return(NULL)
71+
)
72+
)
73+
}
74+
)
5175

5276
setwd(old_dir)
5377

54-
if (grepl(
55-
package_name,
56-
where_system.file
57-
)) {
78+
if (
79+
length(
80+
unique(
81+
c(
82+
package_name_from_config,
83+
package_name_from_desc
84+
)
85+
)
86+
) ==
87+
1
88+
) {
5889
return(invisible(TRUE))
5990
} else {
6091
stop(
6192
call. = FALSE,
6293
"Package name does not match in DESCRIPTION and `app_sys()`.\n",
6394
"\n",
6495
sprintf(
65-
"DESCRIPTION: '%s'\n",
66-
package_name
96+
"In DESCRIPTION: '%s'\n",
97+
package_name_from_desc
6798
),
6899
sprintf(
69-
"R/app_config.R - app_sys(): '%s'\n",
70-
where_system.file
100+
"R/app_config.R : '%s'\n",
101+
paste(package_name_from_config, collapse = ", ")
71102
),
72103
"\n",
73104
sprintf(
74105
"Please make both these names match before continuing, for example using golem::set_golem_name('%s')",
75-
package_name
106+
package_name_from_desc
76107
)
77108
)
78109
}

tests/testthat/test-reload.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ test_that(
2424
)
2525
expect_error(check_name_consistency("."))
2626
})
27+
run_quietly_in_a_dummy_golem({
28+
res <- check_name_consistency(".")
29+
expect_true(res)
30+
write(
31+
"app_sys_2 <- function(...){system.file(..., package = 'blabla')}",
32+
"R/app_config.R",
33+
append = TRUE
34+
)
35+
expect_error(check_name_consistency("."))
36+
})
2737
}
2838
)
2939

0 commit comments

Comments
 (0)