Skip to content

Commit abc4005

Browse files
committed
fix tests and docs
1 parent 8ca56cf commit abc4005

6 files changed

Lines changed: 188 additions & 22 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
^man/figures/logo\.png$
1515
^man/figures/card\.png$
1616
^CRAN-SUBMISSION$
17+
^.gemini$

R/to_txt.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#' \item a package name not installed (which will then be downloaded from CRAN).
1212
#' }
1313
#' @param file Optional. Save path for the output text file. If set, the function will return the path to the file instead of the combined text. Defaults to `NULL`.
14-
#' @param force_fetch `logical`. If `TRUE`, the package source will be fetched from CRAN as a tar.gz archive even if the package is already installed locally. Default is `FALSE`.
14+
#' @param force_fetch `logical`. If `TRUE`, the package source will be fetched from CRAN as a tar.gz archive even if the package is already installed locally. Default is `FALSE`, but when `version` is specified, it will be set to `TRUE`.
15+
#' @param version Optional. A `character` string specifying the package version to fetch from CRAN. If not provided, the latest version will be used.
1516
#' @param content A character vector specifying which components to include in the output.
1617
#' Possible values are:
1718
#' \itemize{
@@ -102,7 +103,7 @@ rdd_to_txt <- function(
102103
pkg_info <- resolve_pkg_path(
103104
pkg,
104105
cache_path,
105-
force_fetch = force_fetch,
106+
force_fetch = force_fetch || !is.null(version),
106107
version = version,
107108
repos = repos
108109
)
@@ -129,9 +130,9 @@ rdd_to_txt <- function(
129130
file = NULL,
130131
include_tests = FALSE,
131132
include_roxygen = FALSE,
132-
force_fetch = force_fetch,
133+
force_fetch = force_fetch || !is.null(version),
133134
cache_path = cache_path,
134-
keep_files = "both", # make sure the files are not deleted prematurely, as rdd_to_txt will take care of that later
135+
keep_files = "both" # make sure the files are not deleted prematurely, as rdd_to_txt will take care of that later
135136
)
136137
}
137138

R/util_resolve_pkg_path.R

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resolve_pkg_path <- function(
2424
# Helper function to parse tarball filename into package name and version.
2525
parse_tarball_name <- function(tar_path) {
2626
base_name <- basename(tar_path) # e.g., "rJavaEnv_0.2.2.tar.gz"
27-
folder_name <- sub("\.tar\.gz$", "", base_name)
27+
folder_name <- sub("\\.tar\\.gz$", "", base_name)
2828
parts <- strsplit(folder_name, "_")[[1]]
2929
if (length(parts) < 2) {
3030
stop(
@@ -51,7 +51,7 @@ resolve_pkg_path <- function(
5151
# Check if directory is a source package by looking for Rd files in "man/"
5252
man_dir <- file.path(pkg, "man")
5353
rd_files <- if (dir.exists(man_dir)) {
54-
list.files(man_dir, pattern = "\.Rd$", full.names = TRUE)
54+
list.files(man_dir, pattern = "\\.Rd$", full.names = TRUE)
5555
} else {
5656
character(0)
5757
}
@@ -75,7 +75,7 @@ resolve_pkg_path <- function(
7575
}
7676
} else {
7777
# pkg is a file; assume it is a tar.gz archive.
78-
if (!grepl("\.tar\.gz$", pkg)) {
78+
if (!grepl("\\.tar\\.gz$", pkg)) {
7979
stop(
8080
"The specified file is not a recognized package archive (expected extension .tar.gz)."
8181
)
@@ -129,7 +129,7 @@ resolve_pkg_path <- function(
129129
}
130130
# Warn if repos contains known problematic URLs.
131131
if (
132-
any(grepl("posit\.co|r-universe\.dev", repos, ignore.case = TRUE))
132+
any(grepl("posit\\.co|r-universe\\.dev", repos, ignore.case = TRUE))
133133
) {
134134
warning(
135135
"Using a repository URL from posit.co or r-universe.dev may result in pre-built binaries being downloaded instead of the package source."
@@ -145,7 +145,12 @@ resolve_pkg_path <- function(
145145
# Try downloading.
146146
res <- try(
147147
suppressWarnings(
148-
utils::download.file(url, destfile = file.path(dest_dir, tar_filename), mode = "wb", quiet = TRUE)
148+
utils::download.file(
149+
url,
150+
destfile = file.path(dest_dir, tar_filename),
151+
mode = "wb",
152+
quiet = TRUE
153+
)
149154
),
150155
silent = TRUE
151156
)
@@ -154,15 +159,23 @@ resolve_pkg_path <- function(
154159
url <- file.path(repo_url, "src/contrib", tar_filename)
155160
res <- try(
156161
suppressWarnings(
157-
utils::download.file(url, destfile = file.path(dest_dir, tar_filename), mode = "wb", quiet = TRUE)
162+
utils::download.file(
163+
url,
164+
destfile = file.path(dest_dir, tar_filename),
165+
mode = "wb",
166+
quiet = TRUE
167+
)
158168
),
159169
silent = TRUE
160170
)
161171
}
162172
if (inherits(res, "try-error") || res != 0) {
163173
stop(paste("Could not download package", pkg, "version", version))
164174
}
165-
dp <- matrix(c(tar_filename, file.path(dest_dir, tar_filename)), nrow = 1)
175+
dp <- matrix(
176+
c(tar_filename, file.path(dest_dir, tar_filename)),
177+
nrow = 1
178+
)
166179
} else {
167180
dp <- utils::download.packages(
168181
pkg,

man/rdd_to_txt.Rd

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/resolve_pkg_path.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-to_txt.R

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# Tests for the rdd_to_txt function in the rdocdump package
2-
fake_resolve_pkg_path <- function(pkg, cache_path, force_fetch, repos) {
2+
fake_resolve_pkg_path <- function(
3+
pkg,
4+
cache_path,
5+
force_fetch,
6+
version,
7+
repos
8+
) {
39
list(
410
pkg_path = pkg,
511
is_installed = FALSE,
612
pkg_name = "testpkg",
713
tar_path = NULL,
14+
version = NULL,
815
extracted_path = NULL
916
)
1017
}
@@ -272,8 +279,8 @@ test_that("rdd_to_txt fetches a specific package version when 'version' is provi
272279

273280
# Use a package with a known version history, e.g., "jsonlite"
274281
# and a version that is not the latest.
275-
pkg_name <- "jsonlite"
276-
pkg_version <- "1.7"
282+
pkg_name <- "ini"
283+
pkg_version <- "0.1"
277284

278285
old_repos <- getOption("repos")
279286
options(repos = c(CRAN = "https://cloud.r-project.org"))
@@ -304,3 +311,144 @@ test_that("rdd_to_txt fetches a specific package version when 'version' is provi
304311
# Clean up the cache directory.
305312
unlink(cache_dir, recursive = TRUE)
306313
})
314+
315+
test_that("rdd_to_txt sets force_fetch=TRUE internally whenever 'version' is provided", {
316+
calls <- new.env(parent = emptyenv())
317+
calls$resolve_force_fetch <- NULL
318+
calls$rdd_force_fetch <- NULL
319+
320+
fake_resolve_pkg_path_spy <- function(
321+
pkg,
322+
cache_path,
323+
force_fetch,
324+
version,
325+
repos
326+
) {
327+
calls$resolve_force_fetch <- force_fetch
328+
list(
329+
pkg_path = tempfile("pkg_"),
330+
is_installed = FALSE,
331+
pkg_name = "ini",
332+
tar_path = NULL,
333+
version = version,
334+
extracted_path = NULL
335+
)
336+
}
337+
338+
fake_rdd_extract_code_spy <- function(
339+
pkg,
340+
file,
341+
include_tests,
342+
include_roxygen,
343+
force_fetch,
344+
cache_path,
345+
keep_files
346+
) {
347+
calls$rdd_force_fetch <- force_fetch
348+
"CODE"
349+
}
350+
351+
# No-op helpers to avoid touching filesystem or requiring real pkg content
352+
fake_cleanup_files <- function(pkg_info, keep_files) invisible(NULL)
353+
fake_combine_rd <- function(...) "" # not used when content = "code"
354+
fake_combine_vignettes <- function(...) "" # not used when content = "code"
355+
356+
local_mocked_bindings(
357+
resolve_pkg_path = fake_resolve_pkg_path_spy,
358+
rdd_extract_code = fake_rdd_extract_code_spy,
359+
cleanup_files = fake_cleanup_files,
360+
combine_rd = fake_combine_rd,
361+
combine_vignettes = fake_combine_vignettes,
362+
.package = "rdocdump"
363+
)
364+
365+
out <- rdd_to_txt(
366+
"ini",
367+
version = "0.1", # << the trigger
368+
force_fetch = FALSE, # user-specified FALSE should be overridden
369+
content = "code",
370+
keep_files = "none"
371+
)
372+
373+
expect_identical(out, "CODE")
374+
expect_true(isTRUE(calls$resolve_force_fetch))
375+
expect_true(isTRUE(calls$rdd_force_fetch))
376+
})
377+
378+
test_that("rdd_to_txt passes through force_fetch unchanged when 'version' is NULL", {
379+
calls <- new.env(parent = emptyenv())
380+
calls$resolve_force_fetch <- NULL
381+
calls$rdd_force_fetch <- NULL
382+
383+
fake_resolve_pkg_path_spy <- function(
384+
pkg,
385+
cache_path,
386+
force_fetch,
387+
version,
388+
repos
389+
) {
390+
calls$resolve_force_fetch <- force_fetch
391+
list(
392+
pkg_path = tempfile("pkg_"),
393+
is_installed = FALSE,
394+
pkg_name = "ini",
395+
tar_path = NULL,
396+
version = version,
397+
extracted_path = NULL
398+
)
399+
}
400+
401+
fake_rdd_extract_code_spy <- function(
402+
pkg,
403+
file,
404+
include_tests,
405+
include_roxygen,
406+
force_fetch,
407+
cache_path,
408+
keep_files
409+
) {
410+
calls$rdd_force_fetch <- force_fetch
411+
"CODE"
412+
}
413+
414+
fake_cleanup_files <- function(pkg_info, keep_files) invisible(NULL)
415+
fake_combine_rd <- function(...) ""
416+
fake_combine_vignettes <- function(...) ""
417+
418+
local_mocked_bindings(
419+
resolve_pkg_path = fake_resolve_pkg_path_spy,
420+
rdd_extract_code = fake_rdd_extract_code_spy,
421+
cleanup_files = fake_cleanup_files,
422+
combine_rd = fake_combine_rd,
423+
combine_vignettes = fake_combine_vignettes,
424+
.package = "rdocdump"
425+
)
426+
427+
# Case A: user passes force_fetch = FALSE, version = NULL -> internal should be FALSE
428+
out_A <- rdd_to_txt(
429+
"ini",
430+
version = NULL,
431+
force_fetch = FALSE,
432+
content = "code",
433+
keep_files = "none"
434+
)
435+
expect_identical(out_A, "CODE")
436+
expect_false(isTRUE(calls$resolve_force_fetch))
437+
expect_false(isTRUE(calls$rdd_force_fetch))
438+
439+
# Reset captured values
440+
calls$resolve_force_fetch <- NULL
441+
calls$rdd_force_fetch <- NULL
442+
443+
# Case B: user passes force_fetch = TRUE, version = NULL -> internal should be TRUE
444+
out_B <- rdd_to_txt(
445+
"ini",
446+
version = NULL,
447+
force_fetch = TRUE,
448+
content = "code",
449+
keep_files = "none"
450+
)
451+
expect_identical(out_B, "CODE")
452+
expect_true(isTRUE(calls$resolve_force_fetch))
453+
expect_true(isTRUE(calls$rdd_force_fetch))
454+
})

0 commit comments

Comments
 (0)