Skip to content

Commit

Permalink
Merge pull request #72 from ropensci-review-tools/tests
Browse files Browse the repository at this point in the history
improve tests
  • Loading branch information
mpadge authored Oct 24, 2024
2 parents b70e6d9 + 1554ffa commit 3f68766
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 80 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pkgstats
Title: Metrics of R Packages
Version: 0.2.0.039
Version: 0.2.0.041
Authors@R:
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
51 changes: 1 addition & 50 deletions R/cran-data-archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ one_summary_from_archive <- function (path, save_full,

if (save_full || save_ex_calls) {
pkg <- utils::tail (decompose_path (path) [[1]], 1L)
pkg <- gsub ("\\.tar\\.gz$", "", pkg)
pkg <- gsub ("\\.tar\\.gz$", ".Rds", pkg)
if (save_full) {
saveRDS (s, fs::path (results_path, pkg))
} else if (save_ex_calls) {
Expand Down Expand Up @@ -452,52 +452,3 @@ get_pkg_version <- function (path) {

c (pkg, v)
}

#' Internal function to convert columns in result of archive trawl to expected,
#' generally numeric, types.
#' @noRd
convert_trawl_columns <- function (x) {

int_cols <- c (
"^files\\_",
"^loc\\_",
"^blank\\_",
"^comment\\_",
"^indentation$",
"^rel\\_space$",
"^num\\_",
"^desc\\_n\\_",
"^n\\_fns\\_(r|s)",
"^n\\_edges",
"^n\\_clusters"
)
num_cols <- c (
"^rel\\_space\\_",
"^nexpr$",
"^rel\\_space\\_",
"^data\\_size\\_",
"^n\\_fns\\_per",
"^npars\\_",
"^doclines\\_per\\_",
"^docchars\\_per\\_",
"^centrality\\_",
"^node\\_degree\\_",
"^cpl\\_instability"
)

cols <- names (x)

int_cols <- lapply (int_cols, function (i) grep (i, cols))
int_cols <- sort (unlist (int_cols))
for (i in int_cols) {
storage.mode (x [[i]]) <- "integer"
}

num_cols <- lapply (num_cols, function (i) grep (i, cols))
num_cols <- sort (unlist (num_cols))
for (i in num_cols) {
storage.mode (x [[i]]) <- "numeric"
}

return (x)
}
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/pkgstats",
"issueTracker": "https://github.com/ropensci-review-tools/pkgstats/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.2.0.039",
"version": "0.2.0.041",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/helper-archive-trawl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
setup_test_archive <- function () {
f <- system.file ("extdata", "pkgstats_9.9.tar.gz", package = "pkgstats")
tarball <- basename (f)

archive_path <- file.path (tempdir (), "archive")
if (!dir.exists (archive_path)) {
dir.create (archive_path)
}
path <- file.path (archive_path, tarball)
if (!file.exists (path)) {
file.copy (f, path)
}

list (tarball = tarball, path = path, archive_path = archive_path)
}

setup_test_tarball <- function (archive) {
tarball_path <- file.path (archive$archive_path, "tarballs")
if (!dir.exists (tarball_path)) {
dir.create (tarball_path, recursive = TRUE)
}
if (!file.exists (file.path (tarball_path, archive$tarball))) {
file.copy (archive$path, file.path (tarball_path, archive$tarball))
}

return (tarball_path)
}
98 changes: 70 additions & 28 deletions tests/testthat/test-archive-trawl.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
test_all <- (identical (Sys.getenv ("MPADGE_LOCAL"), "true") |
identical (Sys.getenv ("GITHUB_WORKFLOW"), "test-coverage"))

setup_test_archive <- function () {
f <- system.file ("extdata", "pkgstats_9.9.tar.gz", package = "pkgstats")
tarball <- basename (f)

archive_path <- file.path (tempdir (), "archive")
if (!dir.exists (archive_path)) {
dir.create (archive_path)
}
path <- file.path (archive_path, tarball)
if (!file.exists (path)) {
file.copy (f, path)
}

list (tarball = tarball, path = path, archive_path = archive_path)
}

setup_test_tarball <- function (archive) {
tarball_path <- file.path (archive$archive_path, "tarballs")
if (!dir.exists (tarball_path)) {
dir.create (tarball_path, recursive = TRUE)
}
if (!file.exists (file.path (tarball_path, archive$tarball))) {
file.copy (archive$path, file.path (tarball_path, archive$tarball))
}

return (tarball_path)
}

test_that ("archive trawl", {

if (!test_all) {
Expand All @@ -37,6 +9,19 @@ test_that ("archive trawl", {

archive <- setup_test_archive ()

expect_type (archive, "list")
expect_length (archive, 3L)
expect_identical (names (archive), c ("tarball", "path", "archive_path"))

v <- get_pkg_version (archive$path)
expect_type (v, "character")
expect_length (v, 2L)
expect_identical (v, c ("pkgstats", "9.9"))

f <- archive_results_file_name ("aaa.txt")
expect_equal (basename (f), "aaa.Rds")
expect_equal (f, fs::path (fs::path_wd (), "aaa.Rds"))

expect_error (
pkgstats_from_archive (archive$path),
"Assertion on 'path' failed"
Expand Down Expand Up @@ -64,3 +49,60 @@ test_that ("archive trawl", {
Sys.unsetenv ("PKGSTATS_CRAN_TESTS")
}
})

test_that ("archive_trawl with save options", {

if (!test_all) {
Sys.setenv ("PKGSTATS_CRAN_TESTS" = "true")
}

archive <- setup_test_archive ()
rds_name <- gsub ("\\.tar\\.gz$", ".Rds", archive$tarball)
tarball_path <- setup_test_tarball (archive)

tempfiles <- basename (fs::dir_ls (fs::path_temp (), type = "file"))
expect_false (rds_name %in% tempfiles)

out <- pkgstats_from_archive (tarball_path, save_full = TRUE)
tempfiles <- basename (fs::dir_ls (fs::path_temp (), type = "file"))
expect_true (rds_name %in% tempfiles)

rds_path <- fs::path (fs::path_temp (), rds_name)
dat <- readRDS (rds_path)
expect_type (dat, "list")
expect_length (dat, 8L)
nms <- c (
"loc",
"vignettes",
"data_stats",
"desc",
"translations",
"objects",
"network",
"external_calls"
)
expect_identical (names (dat), nms)
fs::file_delete (rds_path)

out <- pkgstats_from_archive (tarball_path, save_ex_calls = TRUE)
tempfiles <- basename (fs::dir_ls (fs::path_temp (), type = "file"))
expect_true (rds_name %in% tempfiles)

dat <- readRDS (rds_path)
expect_s3_class (dat, "data.frame")
nms <- c (
"tags_line", "call", "tag", "file", "kind", "start", "end", "package"
)
expect_equal (names (dat), nms)
expect_equal (ncol (dat), 8L)
if (test_all) {
expect_true (nrow (dat) > 500L)
}
fs::file_delete (rds_path)

unlink (archive$archive_path, recursive = TRUE)
if (!test_all) {
Sys.unsetenv ("PKGSTATS_CRAN_TESTS")
}

})

0 comments on commit 3f68766

Please sign in to comment.