Skip to content

Commit 418bc89

Browse files
maelleateucher
andauthored
fix: make use_testthat() work for brand-new package (#1843)
* fix: make use_testthat() work for brand-new package * check value is character in proj_desc_field_update() * Also allow vectors > length 1 since desc() allows it * union old and new when appending to avoid duplicates --------- Co-authored-by: Andy Teucher <[email protected]>
1 parent e05947f commit 418bc89

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

R/proj-desc.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ proj_desc_create <- function(name, fields = list(), roxygen = TRUE) {
3131
# including appending".
3232
proj_desc_field_update <- function(key, value, overwrite = TRUE, append = FALSE) {
3333
check_string(key)
34-
stopifnot(is.character(value) || is.numeric(value), length(value) == 1)
34+
check_character(value)
3535
check_bool(overwrite)
3636

3737
desc <- proj_desc()
3838

3939
old <- desc$get_list(key, default = "")
40-
if (value %in% old) {
40+
if (all(value %in% old)) {
4141
return(invisible())
4242
}
4343

@@ -51,7 +51,7 @@ proj_desc_field_update <- function(key, value, overwrite = TRUE, append = FALSE)
5151
ui_done("Adding {ui_value(value)} to {ui_field(key)}")
5252

5353
if (append) {
54-
value <- c(old, value)
54+
value <- union(old, value)
5555
}
5656

5757
# https://github.com/r-lib/desc/issues/117

R/test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use_testthat_impl <- function(edition = NULL, parallel = FALSE) {
3737
edition <- check_edition(edition)
3838

3939
use_dependency("testthat", "Suggests", paste0(edition, ".0.0"))
40-
proj_desc_field_update("Config/testthat/edition", edition, overwrite = TRUE)
40+
proj_desc_field_update("Config/testthat/edition", as.character(edition), overwrite = TRUE)
4141

4242
if (parallel) {
4343
proj_desc_field_update("Config/testthat/parallel", "true", overwrite = TRUE)

tests/testthat/_snaps/proj-desc.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# proj_desc_field_append() only messages when adding
1+
# proj_desc_field_update() only messages when adding
22

33
Code
44
proj_desc_field_update("Config/Needs/foofy", "alfa", append = TRUE)
@@ -10,3 +10,10 @@
1010
Message
1111
v Adding 'bravo' to Config/Needs/foofy
1212

13+
# proj_desc_field_update() works with multiple values
14+
15+
Code
16+
proj_desc_field_update("Config/Needs/foofy", c("alfa", "bravo"), append = TRUE)
17+
Message
18+
v Adding 'alfa', 'bravo' to Config/Needs/foofy
19+

tests/testthat/test-proj-desc.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that("proj_desc_field_append() only messages when adding", {
1+
test_that("proj_desc_field_update() only messages when adding", {
22
create_local_package()
33
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))
44

@@ -9,3 +9,15 @@ test_that("proj_desc_field_append() only messages when adding", {
99
})
1010
expect_equal(proj_desc()$get_list("Config/Needs/foofy"), c("alfa", "bravo"))
1111
})
12+
13+
test_that("proj_desc_field_update() works with multiple values", {
14+
create_local_package()
15+
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))
16+
17+
proj_desc_field_update("Config/Needs/foofy", "alfa", append = TRUE)
18+
expect_snapshot({
19+
proj_desc_field_update("Config/Needs/foofy", c("alfa", "bravo"),
20+
append = TRUE)
21+
})
22+
expect_equal(proj_desc()$get_list("Config/Needs/foofy"), c("alfa", "bravo"))
23+
})

0 commit comments

Comments
 (0)