Skip to content

Commit ad25fe9

Browse files
committed
Support suggests field of standalone file
1 parent b4e8477 commit ad25fe9

File tree

3 files changed

+81
-30
lines changed

3 files changed

+81
-30
lines changed

R/use_standalone.R

+24-12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#' [use_package()] to ensure they are included in the `Imports:`
3939
#' field of the `DESCRIPTION` file.
4040
#'
41+
#' - `suggests`: Similar to `imports`, but the packages are added to the
42+
#' `Suggests:` field of the `DESCRIPTION` file.
43+
#'
4144
#' Note that lists are specified with standard YAML syntax, using
4245
#' square brackets, for example: `imports: [rlang (>= 1.0.0), purrr]`.
4346
#'
@@ -84,20 +87,27 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) {
8487
}
8588

8689
imports <- dependencies$imports
87-
88-
for (i in seq_len(nrow(imports))) {
89-
import <- imports[i, , drop = FALSE]
90-
91-
if (is.na(import$ver)) {
92-
ver <- NULL
93-
} else {
94-
ver <- import$ver
90+
suggests <- dependencies$suggests
91+
92+
add_fields <- function(x, type = c("Imports", "Suggests")) {
93+
type <- arg_match(type)
94+
95+
for (i in seq_len(nrow(x))) {
96+
pkgs <- x[i, , drop = FALSE]
97+
if (is.na(pkgs$ver)) {
98+
ver <- NULL
99+
} else {
100+
ver <- pkgs$ver
101+
}
102+
ui_silence(
103+
use_package(pkgs$pkg, min_version = ver, type = type)
104+
)
95105
}
96-
ui_silence(
97-
use_package(import$pkg, min_version = ver)
98-
)
99106
}
100107

108+
add_fields(imports, "Imports")
109+
add_fields(suggests, "Suggests")
110+
101111
invisible()
102112
}
103113

@@ -206,6 +216,8 @@ standalone_dependencies <- function(lines, path, error_call = caller_env()) {
206216
deps <- as_chr_field(yaml$dependencies)
207217
imports <- as_chr_field(yaml$imports)
208218
imports <- as_version_info(imports, error_call = error_call)
219+
suggests <- as_chr_field(yaml$suggests)
220+
suggests <- as_version_info(suggests, error_call = error_call)
209221

210222
if (any(stats::na.omit(imports$cmp) != ">=")) {
211223
cli::cli_abort(
@@ -214,7 +226,7 @@ standalone_dependencies <- function(lines, path, error_call = caller_env()) {
214226
)
215227
}
216228

217-
list(deps = deps, imports = imports)
229+
list(deps = deps, imports = imports, suggests = suggests)
218230
}
219231

220232
as_version_info <- function(fields, error_call = caller_env()) {

tests/testthat/_snaps/use_standalone.md

+26-7
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,42 @@
7878
! `file` is absent, but must be supplied.
7979
i Possible options are cli, downstream-deps, lazyeval, lifecycle, linked-version, obj-type, purrr, rlang, s3-register, sizes, types-check, vctrs, or zeallot.
8080

81-
# can extract imports
81+
# can extract imports/suggests
8282

8383
Code
84-
extract_imports("# imports: rlang (== 1.0.0)")
84+
extract_pkgs("# imports: rlang (== 1.0.0)", "imports")
8585
Condition
86-
Error in `extract_imports()`:
86+
Error in `extract_pkgs()`:
8787
! Version specification must use `>=`.
8888
Code
89-
extract_imports("# imports: rlang (>= 1.0.0), purrr")
89+
extract_pkgs("# suggests: rlang (== 1.0.0)", "suggests")
90+
Output
91+
# A data frame: 1 x 3
92+
pkg cmp ver
93+
<chr> <chr> <chr>
94+
1 rlang == 1.0.0
95+
Code
96+
extract_pkgs("# imports: rlang (>= 1.0.0), purrr", "imports")
97+
Condition
98+
Error in `extract_pkgs()`:
99+
! Version field can't contain comma.
100+
i Do you need to wrap in a list?
101+
Code
102+
extract_pkgs("# suggests: rlang (>= 1.0.0), purrr", "suggests")
90103
Condition
91-
Error in `extract_imports()`:
104+
Error in `extract_pkgs()`:
92105
! Version field can't contain comma.
93106
i Do you need to wrap in a list?
94107
Code
95-
extract_imports("# imports: foo (>=0.0.0)")
108+
extract_pkgs("# imports: foo (>=0.0.0)", "imports")
109+
Condition
110+
Error in `extract_pkgs()`:
111+
! Can't parse version `foo (>=0.0.0)` in `imports:` field.
112+
i Example of expected version format: `rlang (>= 1.0.0)`.
113+
Code
114+
extract_pkgs("# suggests: foo (>=0.0.0)", "suggests")
96115
Condition
97-
Error in `extract_imports()`:
116+
Error in `extract_pkgs()`:
98117
! Can't parse version `foo (>=0.0.0)` in `imports:` field.
99118
i Example of expected version format: `rlang (>= 1.0.0)`.
100119

tests/testthat/test-use_standalone.R

+31-11
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,60 @@ test_that("can extract dependencies", {
8282
expect_equal(extract_deps("# dependencies: [a, b]"), c("a", "b"))
8383
})
8484

85-
test_that("can extract imports", {
86-
extract_imports <- function(imports) {
85+
test_that("can extract imports/suggests", {
86+
extract_pkgs <- function(pkgs, type = c("imports", "suggests")) {
87+
type <- arg_match(type)
8788
out <- standalone_dependencies(
88-
c("# ---", imports, "# ---"),
89+
c("# ---", pkgs, "# ---"),
8990
"test.R",
9091
error_call = current_env()
9192
)
92-
out$imports
93+
out[[type]]
9394
}
9495

9596
expect_equal(
96-
extract_imports(NULL),
97+
extract_pkgs(NULL, "imports"),
98+
version_info_df()
99+
)
100+
expect_equal(
101+
extract_pkgs(NULL, "suggests"),
97102
version_info_df()
98103
)
99104

100105
expect_equal(
101-
extract_imports("# imports: rlang"),
106+
extract_pkgs("# imports: rlang", "imports"),
107+
version_info_df("rlang", NA, NA)
108+
)
109+
expect_equal(
110+
extract_pkgs("# suggests: rlang", "suggests"),
102111
version_info_df("rlang", NA, NA)
103112
)
104113

105114
expect_equal(
106-
extract_imports("# imports: rlang (>= 1.0.0)"),
115+
extract_pkgs("# imports: rlang (>= 1.0.0)", "imports"),
116+
version_info_df("rlang", ">=", "1.0.0")
117+
)
118+
expect_equal(
119+
extract_pkgs("# suggests: rlang (>= 1.0.0)", "suggests"),
107120
version_info_df("rlang", ">=", "1.0.0")
108121
)
109122

110123
expect_equal(
111-
extract_imports("# imports: [rlang (>= 1.0.0), purrr]"),
124+
extract_pkgs("# imports: [rlang (>= 1.0.0), purrr]", "imports"),
125+
version_info_df(c("rlang", "purrr"), c(">=", NA), c("1.0.0", NA))
126+
)
127+
expect_equal(
128+
extract_pkgs("# suggests: [rlang (>= 1.0.0), purrr]", "suggests"),
112129
version_info_df(c("rlang", "purrr"), c(">=", NA), c("1.0.0", NA))
113130
)
114131

115132
expect_snapshot(error = TRUE, {
116-
extract_imports("# imports: rlang (== 1.0.0)")
117-
extract_imports("# imports: rlang (>= 1.0.0), purrr")
118-
extract_imports("# imports: foo (>=0.0.0)")
133+
extract_pkgs("# imports: rlang (== 1.0.0)", "imports")
134+
extract_pkgs("# suggests: rlang (== 1.0.0)", "suggests")
135+
extract_pkgs("# imports: rlang (>= 1.0.0), purrr", "imports")
136+
extract_pkgs("# suggests: rlang (>= 1.0.0), purrr", "suggests")
137+
extract_pkgs("# imports: foo (>=0.0.0)", "imports")
138+
extract_pkgs("# suggests: foo (>=0.0.0)", "suggests")
119139
})
120140
})
121141

0 commit comments

Comments
 (0)