From c698bf3445a6160fd99fff574ef61fd442a39e36 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 8 Sep 2024 22:43:30 +0300 Subject: [PATCH] Mask current rextendr version in snapshot tests (#386) --- DESCRIPTION | 2 +- tests/testthat/_snaps/use_cran_defaults.md | 2 +- tests/testthat/_snaps/use_extendr.md | 2 +- tests/testthat/helper.R | 13 +++++++++++++ tests/testthat/test-use_cran_defaults.R | 12 ++---------- tests/testthat/test-use_extendr.R | 2 +- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 84f33a87..65722059 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: rextendr Title: Call Rust Code from R using the 'extendr' Crate Version: 0.3.1.9001 -Authors@R: +Authors@R: c(person(given = "Claus O.", family = "Wilke", role = "aut", diff --git a/tests/testthat/_snaps/use_cran_defaults.md b/tests/testthat/_snaps/use_cran_defaults.md index 49ef14b8..aa89ea55 100644 --- a/tests/testthat/_snaps/use_cran_defaults.md +++ b/tests/testthat/_snaps/use_cran_defaults.md @@ -4,7 +4,7 @@ use_extendr() Message i First time using rextendr. Upgrading automatically... - i Setting `Config/rextendr/version` to "0.3.1.9001" in the 'DESCRIPTION' file. + i Setting `Config/rextendr/version` to "*.*.*" in the 'DESCRIPTION' file. i Setting `SystemRequirements` to "Cargo (Rust's package manager), rustc" in the 'DESCRIPTION' file. v Creating 'src/rust/src'. v Writing 'src/entrypoint.c' diff --git a/tests/testthat/_snaps/use_extendr.md b/tests/testthat/_snaps/use_extendr.md index 28141539..7cf6ce67 100644 --- a/tests/testthat/_snaps/use_extendr.md +++ b/tests/testthat/_snaps/use_extendr.md @@ -4,7 +4,7 @@ use_extendr() Message i First time using rextendr. Upgrading automatically... - i Setting `Config/rextendr/version` to "0.3.1.9001" in the 'DESCRIPTION' file. + i Setting `Config/rextendr/version` to "*.*.*" in the 'DESCRIPTION' file. i Setting `SystemRequirements` to "Cargo (Rust's package manager), rustc" in the 'DESCRIPTION' file. v Creating 'src/rust/src'. v Writing 'src/entrypoint.c' diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index d5f58ee6..c94d89d1 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -112,3 +112,16 @@ skip_if_opted_out_of_dev_tests <- function() { testthat::skip("Dev extendr tests disabled") } } + +#' Mask any version in snapshot files +#' @param snapshot_lines Character vector, lines of the snapshot file +#' @example +#' expect_snapshot(some_operation(), transform = mask_any_version) +#' @noRd +mask_any_version <- function(snapshot_lines) { + stringi::stri_replace_all_regex( + snapshot_lines, + "\\d+\\.\\d+\\.\\d+(?:\\.\\d+)?", + "*.*.*" + ) +} diff --git a/tests/testthat/test-use_cran_defaults.R b/tests/testthat/test-use_cran_defaults.R index b3ba8764..5b3db4b7 100644 --- a/tests/testthat/test-use_cran_defaults.R +++ b/tests/testthat/test-use_cran_defaults.R @@ -4,7 +4,7 @@ test_that("use_cran_defaults() modifies and creates files correctly", { path <- local_package("testpkg") # capture setup messages withr::local_options(usethis.quiet = FALSE) - expect_snapshot(use_extendr()) + expect_snapshot(use_extendr(), transform = mask_any_version) expect_snapshot(use_cran_defaults()) expect_snapshot(cat_file("src", "Makevars")) @@ -26,14 +26,6 @@ test_that("use_cran_defaults() quiet if quiet=TRUE", { test_that("vendor_pkgs() vendors dependencies", { - mask_version_strings <- function(snapshot_lines) { - stringi::stri_replace_all_regex( - snapshot_lines, - "\\d+\\.\\d+\\.\\d+", - "*.*.*" - ) - } - skip_if_not_installed("usethis") path <- local_package("testpkg") @@ -44,6 +36,6 @@ test_that("vendor_pkgs() vendors dependencies", { package_versions <- vendor_pkgs(path, quiet = TRUE) expect_snapshot(cat_file("src", "rust", "vendor-config.toml")) - expect_snapshot(package_versions, transform = mask_version_strings) + expect_snapshot(package_versions, transform = mask_any_version) expect_true(file.exists(file.path("src", "rust", "vendor.tar.xz"))) }) diff --git a/tests/testthat/test-use_extendr.R b/tests/testthat/test-use_extendr.R index 124cea6a..68c6571d 100644 --- a/tests/testthat/test-use_extendr.R +++ b/tests/testthat/test-use_extendr.R @@ -4,7 +4,7 @@ test_that("use_extendr() sets up extendr files correctly", { path <- local_package("testpkg") # capture setup messages withr::local_options(usethis.quiet = FALSE) - expect_snapshot(use_extendr()) + expect_snapshot(use_extendr(), transform = mask_any_version) # DESCRITION file version_in_desc <- stringi::stri_trim_both(desc::desc_get("Config/rextendr/version", path)[[1]])