Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'test-cran-update.R' #73

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.041
Version: 0.2.0.046
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand Down
3 changes: 3 additions & 0 deletions R/cran-data-fn-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ pkgstats_fns_from_archive <- function (path,
#'
#' @family archive
#' @export

# nocov start
pkgstats_fns_update <- function (prev_results = NULL,
results_file = NULL,
chunk_size = 1000L,
Expand Down Expand Up @@ -253,3 +255,4 @@ pkgstats_fns_update <- function (prev_results = NULL,

invisible (out)
}
# nocov end
3 changes: 3 additions & 0 deletions R/cran-data-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ RELEASE_TAG <- "v0.1.6"
#' @return Local path to directory containing updated results.
#' @family archive
#' @export

# nocov start
pkgstats_update <- function (upload = TRUE) {

requireNamespace ("callr")
Expand Down Expand Up @@ -102,6 +104,7 @@ pkgstats_update <- function (upload = TRUE) {
)
}
}
# nocov end

dl_prev_data <- function (results_path, what = "all") {
what <- match.arg (what, c ("all", "current", "fn_names"))
Expand Down
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.041",
"version": "0.2.0.046",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
115 changes: 115 additions & 0 deletions tests/testthat/test-cran-update.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
test_all <- (identical (Sys.getenv ("MPADGE_LOCAL"), "true") |
identical (Sys.getenv ("GITHUB_WORKFLOW"), "test-coverage"))

skip_if (!test_all)

test_that ("cran data update functions", {

results_path <- fs::path (fs::path_temp (), "pkgstats-data")
fs::dir_create (results_path)

f <- dl_prev_data (results_path, what = "current")
expect_type (f, "character")
expect_length (f, 1L)
expect_true (fs::file_exists (f))

x <- readRDS (f)
expect_s3_class (x, "data.frame")
expect_equal (ncol (x), 95L)
expect_true (nrow (x) > 20000L)

expect_silent (
check_prev_results (x)
)

new <- list_new_cran_updates (x)
expect_type (new, "character")
if (length (new) > 0L) {
expect_true (all (grepl ("\\_[0-9]", new)))
}

fs::dir_delete (results_path)
})

test_that ("download tarball", {

pkg <- "odbc"
u <- paste0 ("https://cran.r-project.org/package=", pkg)
qry <- httr2::request (u)
resp <- httr2::req_perform (qry)
# No 'xml' or 'rvest' here, so process as raw text:
b <- httr2::resp_body_string (resp)
b <- strsplit (b, ",\\s") [[1]]
tarball <- grep ("\\.tar\\.gz", b, value = TRUE)
ptn <- "odbc\\_[0-9]\\.[0-9]\\.[0-9]\\.tar\\.gz"
tarball <- regmatches (tarball, regexpr (ptn, tarball))
tarball <- gsub ("\\.tar\\.gz$", "", tarball)

results_path <- fs::path (fs::path_temp (), "pkgstats-data")
fs::dir_create (results_path)

path <- dl_one_tarball (results_path, tarball)
expect_true (fs::file_exists (path))

fs::dir_delete (results_path)
})

# Some pkgs define an "exportPattern" in the namespace, and use that to define
# exported functions. These call the separate `aliases_from_rd()` function,
# which is covered in these tests. The selected package was identified using
# the following code:
#
# flist <- fs::dir_ls (path_to_tarballs, regexp = "\\.tar\\.gz$")
# dat <- pbapply::pblapply (flist, function (f) {
#
# tb <- tryCatch (extract_tarball (f), error = function (e) NULL)
# ret <- NULL
# if (!is.null (tb)) {
# nmsp <- get_namespace_contents (tb)
# if (any (grepl ("exportPattern", nmsp))) {
# ret <- c (unname (f), as.character (file.size (f)))
# }
# fs::dir_delete (tb)
# }
#
# return (ret)
# })
# dat <- do.call (rbind, dat)
# rownames (dat) <- NULL
# dat <- data.frame (pkg = basename (dat [, 1]), size = as.numeric (dat [, 2]))
# dat <- dat [order (dat$size), ]
#
test_that ("fn_names_from_export_ptn", {

pkg <- "abodOutlier"
u <- paste0 ("https://cran.r-project.org/package=", pkg)
qry <- httr2::request (u)
resp <- httr2::req_perform (qry)
# No 'xml' or 'rvest' here, so process as raw text:
b <- httr2::resp_body_string (resp)
b <- strsplit (b, ",\\s") [[1]]
tarball <- grep ("\\.tar\\.gz", b, value = TRUE)
ptn <- "abodOutlier\\_[0-9]\\.[0-9]\\.tar\\.gz"
tarball <- regmatches (tarball, regexpr (ptn, tarball))
tarball <- gsub ("\\.tar\\.gz$", "", tarball)

results_path <- fs::path (fs::path_temp (), "pkgstats-data")
fs::dir_create (results_path)

path <- dl_one_tarball (results_path, tarball)
expect_true (fs::file_exists (path))

fn_nms <- pkgstats_fn_names (path)
expect_s3_class (fn_nms, "data.frame")
expect_equal (ncol (fn_nms), 3L)
expect_equal (names (fn_nms), c ("package", "version", "fn_name"))
expect_true (nrow (fn_nms) > 0L)

fs::dir_delete (results_path)

pkg <- fs::path (fs::path_temp (), gsub ("\\_.*$", "", tarball))
if (fs::dir_exists (pkg)) {
fs::dir_delete (pkg)
}

})
33 changes: 33 additions & 0 deletions tests/testthat/test-pkgstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,36 @@ test_that ("pkgstats", {
expect_true ("stats" %in% ext$package)
expect_true (length (unique (ext$package)) > 15L)
})

skip_if (!test_all)

test_that ("pkgstats with tabs", {

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

path <- system.file ("extdata", "pkgstats_9.9.tar.gz", package = "pkgstats")
path <- extract_tarball (path)
flist <- fs::dir_ls (fs::path (path, "R"), type = "file", recurse = TRUE)
f <- flist [which (basename (flist) == "loc.R")]
x <- readLines (f)
x <- gsub ("^\\s{12}", "\\\t\\\t\\\t", x)
x <- gsub ("^\\s{8}", "\\\t\\\t", x)
x <- gsub ("^\\s{4}", "\\\t", x)
writeLines (x, f)

# message is now produced once per session by readr, but can only be
# suppressed by Suggesting yet another package, `tidyselect`.
# expect_message (
s <- pkgstats (path)

if (!test_all) {
Sys.unsetenv ("PKGSTATS_CRAN_TESTS")
}

expect_s3_class (s$loc, "tbl_df")
expect_true (s$loc$ntabs [s$loc$dir == "R"] > 0L)

fs::dir_delete (path)
})
Loading