|
1 | 1 | # 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 | +) { |
3 | 9 | list( |
4 | 10 | pkg_path = pkg, |
5 | 11 | is_installed = FALSE, |
6 | 12 | pkg_name = "testpkg", |
7 | 13 | tar_path = NULL, |
| 14 | + version = NULL, |
8 | 15 | extracted_path = NULL |
9 | 16 | ) |
10 | 17 | } |
@@ -262,3 +269,186 @@ test_that("rdd_to_txt keeps both tar.gz archive and extracted files when keep_fi |
262 | 269 | # Clean up the cache directory. |
263 | 270 | unlink(cache_dir, recursive = TRUE) |
264 | 271 | }) |
| 272 | + |
| 273 | +test_that("rdd_to_txt fetches a specific package version when 'version' is provided", { |
| 274 | + skip_on_cran() |
| 275 | + skip_if_offline() |
| 276 | + |
| 277 | + cache_dir <- tempfile("cache_version_test") |
| 278 | + dir.create(cache_dir) |
| 279 | + |
| 280 | + # Use a package with a known version history, e.g., "jsonlite" |
| 281 | + # and a version that is not the latest. |
| 282 | + pkg_name <- "ini" |
| 283 | + pkg_version <- "0.1" |
| 284 | + |
| 285 | + old_repos <- getOption("repos") |
| 286 | + options(repos = c(CRAN = "https://cloud.r-project.org")) |
| 287 | + |
| 288 | + # Fetch the package with the specified version. |
| 289 | + out <- suppressWarnings(rdd_to_txt( |
| 290 | + pkg_name, |
| 291 | + version = pkg_version, |
| 292 | + force_fetch = TRUE, |
| 293 | + keep_files = "extracted", |
| 294 | + cache_path = cache_dir |
| 295 | + )) |
| 296 | + |
| 297 | + options(repos = old_repos) |
| 298 | + |
| 299 | + # Check that the extracted directory for the correct version exists. |
| 300 | + expected_dir <- file.path(cache_dir, pkg_name, pkg_version) |
| 301 | + expect_true(dir.exists(expected_dir)) |
| 302 | + |
| 303 | + # Verify the version from the DESCRIPTION file. |
| 304 | + desc_file <- file.path(expected_dir, "DESCRIPTION") |
| 305 | + expect_true(file.exists(desc_file)) |
| 306 | + |
| 307 | + desc_content <- readLines(desc_file) |
| 308 | + version_line <- grep("Version:", desc_content, value = TRUE) |
| 309 | + expect_true(grepl(pkg_version, version_line)) |
| 310 | + |
| 311 | + # Clean up the cache directory. |
| 312 | + unlink(cache_dir, recursive = TRUE) |
| 313 | +}) |
| 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