From 78d9cbc573d4e911ee5bc3a5d865bf24fa6d5333 Mon Sep 17 00:00:00 2001 From: Maciej Nasinski Date: Mon, 23 Sep 2024 23:09:30 +0200 Subject: [PATCH] improve solution --- NEWS.md | 4 ++-- R/comapre.R | 10 +++++++--- R/description.R | 3 +-- R/namespace.R | 2 +- R/news.R | 5 +++-- R/{read_file_cran.R => read_cran_file.R} | 11 +++++----- R/{read_file_github.R => read_github_file.R} | 11 +++++----- R/template_args.R | 1 + man/cran_archive_file.Rd | 21 -------------------- man/read_cran_file.Rd | 21 ++++++++++++++++++++ man/read_github_file.Rd | 10 +++++----- man/standard_args.Rd | 2 ++ tests/testthat/test-compare.R | 15 ++++++++++++++ tests/testthat/test-description.R | 9 --------- tests/testthat/test-namespace.R | 4 ++-- tests/testthat/test-read_cran.R | 15 ++++++++++++++ tests/testthat/test-read_github.R | 13 ++++++++++++ tests/testthat/test-validate_input.R | 20 +++++++++++++++++++ tests/testthat/test-validate_pac_input.R | 9 --------- 19 files changed, 118 insertions(+), 68 deletions(-) rename R/{read_file_cran.R => read_cran_file.R} (75%) rename R/{read_file_github.R => read_github_file.R} (63%) delete mode 100644 man/cran_archive_file.Rd create mode 100644 man/read_cran_file.Rd create mode 100644 tests/testthat/test-read_cran.R create mode 100644 tests/testthat/test-read_github.R create mode 100644 tests/testthat/test-validate_input.R delete mode 100644 tests/testthat/test-validate_pac_input.R diff --git a/NEWS.md b/NEWS.md index 1afd020..a716517 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,9 @@ # pacs 0.5.1.9000 -* add NEWS file related functions, pac_news and pac_compare_news. -* improve code base. +* add new functions: pac_news and pac_compare_news. Functions are NEWS file related. * update the tinyverse vignette with a new badge url. * fix a problem with app_deps on R 3.6. +* improve code base. # pacs 0.5.1 diff --git a/R/comapre.R b/R/comapre.R index a526d33..4c5a58d 100644 --- a/R/comapre.R +++ b/R/comapre.R @@ -194,6 +194,10 @@ pac_compare_news <- function(pac, stopifnot(utils::compareVersion(new, old) >= 0) + if (utils::compareVersion(new, old) == 0) { + return(NA) + } + version_pattern <- function(version) { paste0("#.*", version) } @@ -204,7 +208,7 @@ pac_compare_news <- function(pac, old_version_reg <- regexpr(version_pattern(old), pac_news) which_matched_old <- which(old_version_reg > 0)[1] - old_version_pos <- if (which_matched_old > 0) { + old_version_pos <- if (isTRUE(which_matched_old > 0)) { which_matched_old } else { NA @@ -212,7 +216,7 @@ pac_compare_news <- function(pac, new_version_reg <- regexpr(version_pattern(new), pac_news) which_matched_new <- which(new_version_reg > 0)[1] - new_version_pos <- if (which_matched_new > 0) { + new_version_pos <- if (isTRUE(which_matched_new > 0)) { which_matched_new } else { NA @@ -222,7 +226,7 @@ pac_compare_news <- function(pac, return(NA) } - result <- pac_news[new_version_pos:(old_version_pos - 1)] + result <- pac_news[new_version_pos:max(c(old_version_pos - 1, 1))] result } diff --git a/R/description.R b/R/description.R index 665f525..fda66c5 100644 --- a/R/description.R +++ b/R/description.R @@ -78,12 +78,11 @@ pac_description_dcf_raw <- function(pac, version, repos = "https://cran.rstudio. silent = TRUE ) if (inherits(tt, "try-error")) { - result <- cran_archive_file(pac, version, "DESCRIPTION", repos) + result <- read_cran_file(pac, version, "DESCRIPTION", repos) } else { result <- as.list(read.dcf(ee)[1, ]) } unlink(ee) - structure(result, package = pac, version = version) } diff --git a/R/namespace.R b/R/namespace.R index a938225..a2d8120 100644 --- a/R/namespace.R +++ b/R/namespace.R @@ -80,7 +80,7 @@ pac_readnamespace_raw <- function(pac, version, repos = "https://cran.rstudio.co silent = TRUE ) if (inherits(tt, "try-error")) { - result <- cran_archive_file(pac, version, "NAMESPACE", repos) + result <- read_cran_file(pac, version, "NAMESPACE", repos) } else { result <- readLines(ee, warn = FALSE) unlink(ee) diff --git a/R/news.R b/R/news.R index e5efa92..9488d3a 100644 --- a/R/news.R +++ b/R/news.R @@ -83,11 +83,12 @@ pac_readnews_raw <- function(pac, version, repos = "https://cran.rstudio.com/") silent = TRUE ) if (inherits(tt, "try-error")) { - result <- cran_archive_file(pac, version, "NEWS", repos) + result <- read_cran_file(pac, version, "NEWS", repos) } else { - res <- readLines(ee) + result <- readLines(ee) unlink(ee) } + result } pac_readnews <- memoise::memoise(pac_readnews_raw, cache = cachem::cache_mem(max_age = 30 * 60)) diff --git a/R/read_file_cran.R b/R/read_cran_file.R similarity index 75% rename from R/read_file_cran.R rename to R/read_cran_file.R index f21a647..8b545cd 100644 --- a/R/read_file_cran.R +++ b/R/read_cran_file.R @@ -1,11 +1,10 @@ #' Read a file from CRAN #' @description Read a file from CRAN package source. -#' @param pac `character` package name. -#' @param version `character` package version. -#' @param repos `character` vector repositories URLs to use. Used only for the validation. Default `https://cran.rstudio.com/` -#' @param file `character` file name to read. Possible values are `DESCRIPTION` and `NAMESPACE`. +#' @inheritParams standard_args #' @keywords internal -cran_archive_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") { +read_cran_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") { + stopifnot(is_online()) + last_version <- pac_last(pac, repos) if (isTRUE(!is.null(version) && version != last_version)) { @@ -35,7 +34,7 @@ cran_archive_file <- function(pac, version, file, repos = "https://cran.rstudio. ) if (inherits(download, "try-error")) { - result <- structure(list(), package = pac, version = version) + result <- NA } else { temp_dir <- tempdir() utils::untar(temp_tar, exdir = temp_dir) diff --git a/R/read_file_github.R b/R/read_github_file.R similarity index 63% rename from R/read_file_github.R rename to R/read_github_file.R index 4eaba6c..b30c69e 100644 --- a/R/read_file_github.R +++ b/R/read_github_file.R @@ -1,12 +1,10 @@ #' Read a file from a GitHub CRAN repository #' @description Read a file from a GitHub CRAN repository. -#' @param pac `character` package name. -#' @param version `character` package version. -#' @param file `character` file name to read. Possible values are `DESCRIPTION` and `NAMESPACE`. -#' @param repos `character` vector repositories URLs to use. Used only for the validation. Default `https://cran.rstudio.com/` +#' @inheritParams standard_args #' @note if the file is not found in the GitHub repository, it will try to find it in the CRAN archive. #' @keywords internal read_github_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") { + stopifnot(is_online()) ee <- tempfile() d_url <- sprintf( "https://raw.githubusercontent.com/cran/%s/%s/%s", @@ -24,9 +22,10 @@ read_github_file <- function(pac, version, file, repos = "https://cran.rstudio.c silent = TRUE ) if (inherits(tt, "try-error")) { - result <- cran_archive_file(pac, version, repos, file) + result <- read_cran_file(pac, version, file, repos) } else { - res <- readLines(ee) + result <- readLines(ee) unlink(ee) } + result } diff --git a/R/template_args.R b/R/template_args.R index 45b7cb9..65ce256 100644 --- a/R/template_args.R +++ b/R/template_args.R @@ -12,6 +12,7 @@ #' Default: `list(scope = character(0), flavor = NULL)` #' @param description_v `logical` if the dependencies version should be taken from description files, minimal required. By default installed versions are taken. Default: `FALSE` #' @param exclude_joint `integer` exclude packages which are dependencies of at least N other packages, not count main package dependencies. Default: `0` +#' @param file `character` file name to read. Possible values are `DESCRIPTION`, `NEWS` and `NAMESPACE`. #' @param fields `character` vector listing the types of dependencies, a subset of `c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances")`. #' Character string "all" is shorthand for that vector, character string "most" for the same vector without "Enhances", character string "strong" (default) for the first three elements of that vector. #' Default: `c("Depends", "Imports", "LinkingTo")` diff --git a/man/cran_archive_file.Rd b/man/cran_archive_file.Rd deleted file mode 100644 index 85ecf44..0000000 --- a/man/cran_archive_file.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/read_file_cran.R -\name{cran_archive_file} -\alias{cran_archive_file} -\title{Read a file from CRAN} -\usage{ -cran_archive_file(pac, version, file, repos = "https://cran.rstudio.com/") -} -\arguments{ -\item{pac}{\code{character} package name.} - -\item{version}{\code{character} package version.} - -\item{file}{\code{character} file name to read. Possible values are \code{DESCRIPTION} and \code{NAMESPACE}.} - -\item{repos}{\code{character} vector repositories URLs to use. Used only for the validation. Default \verb{https://cran.rstudio.com/}} -} -\description{ -Read a file from CRAN package source. -} -\keyword{internal} diff --git a/man/read_cran_file.Rd b/man/read_cran_file.Rd new file mode 100644 index 0000000..b4c6b89 --- /dev/null +++ b/man/read_cran_file.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_cran_file.R +\name{read_cran_file} +\alias{read_cran_file} +\title{Read a file from CRAN} +\usage{ +read_cran_file(pac, version, file, repos = "https://cran.rstudio.com/") +} +\arguments{ +\item{pac}{\code{character} a package name.} + +\item{version}{\code{character} version of a package. Default: \code{NULL}} + +\item{file}{\code{character} file name to read. Possible values are \code{DESCRIPTION}, \code{NEWS} and \code{NAMESPACE}.} + +\item{repos}{\code{character} vector of repositories URLs to use. By default checking CRAN and newest Bioconductor per R version. Default \code{pacs::biocran_repos()}} +} +\description{ +Read a file from CRAN package source. +} +\keyword{internal} diff --git a/man/read_github_file.Rd b/man/read_github_file.Rd index aa21dbd..e7c4c2c 100644 --- a/man/read_github_file.Rd +++ b/man/read_github_file.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/read_file_github.R +% Please edit documentation in R/read_github_file.R \name{read_github_file} \alias{read_github_file} \title{Read a file from a GitHub CRAN repository} @@ -7,13 +7,13 @@ read_github_file(pac, version, file, repos = "https://cran.rstudio.com/") } \arguments{ -\item{pac}{\code{character} package name.} +\item{pac}{\code{character} a package name.} -\item{version}{\code{character} package version.} +\item{version}{\code{character} version of a package. Default: \code{NULL}} -\item{file}{\code{character} file name to read. Possible values are \code{DESCRIPTION} and \code{NAMESPACE}.} +\item{file}{\code{character} file name to read. Possible values are \code{DESCRIPTION}, \code{NEWS} and \code{NAMESPACE}.} -\item{repos}{\code{character} vector repositories URLs to use. Used only for the validation. Default \verb{https://cran.rstudio.com/}} +\item{repos}{\code{character} vector of repositories URLs to use. By default checking CRAN and newest Bioconductor per R version. Default \code{pacs::biocran_repos()}} } \description{ Read a file from a GitHub CRAN repository. diff --git a/man/standard_args.Rd b/man/standard_args.Rd index bd93dc7..779950c 100644 --- a/man/standard_args.Rd +++ b/man/standard_args.Rd @@ -23,6 +23,8 @@ Default: \code{list(scope = character(0), flavor = NULL)}} \item{exclude_joint}{\code{integer} exclude packages which are dependencies of at least N other packages, not count main package dependencies. Default: \code{0}} +\item{file}{\code{character} file name to read. Possible values are \code{DESCRIPTION}, \code{NEWS} and \code{NAMESPACE}.} + \item{fields}{\code{character} vector listing the types of dependencies, a subset of \code{c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances")}. Character string "all" is shorthand for that vector, character string "most" for the same vector without "Enhances", character string "strong" (default) for the first three elements of that vector. Default: \code{c("Depends", "Imports", "LinkingTo")}} diff --git a/tests/testthat/test-compare.R b/tests/testthat/test-compare.R index 3664783..067ab28 100644 --- a/tests/testthat/test-compare.R +++ b/tests/testthat/test-compare.R @@ -41,3 +41,18 @@ test_that("pacs::pac_comapre_namespace offline", { mockery::stub(pac_compare_namespace_offline, "is_online", FALSE) expect_error(pac_compare_namespace_offline("memoise", "0.2.1", "2.0.0"), "is_online\\(\\) is not TRUE") }) + + +test_that("pacs::pac_compare_news", { + skip_if_offline() + expect_identical(pac_compare_news("memoise", "2.0.0", "22.4.0"), NA) + expect_error(pac_compare_news("memoise", "22.8.0", "22.4.0"), "compareVersion") + expect_identical(pac_compare_news("WRONG"), NA) +}) + +test_that("pacs::pac_compare_news online", { + skip_if_offline() + expect_true(length(pac_compare_news("dplyr", "0.7.1", "1.0.0")) == 947) + expect_true(length(pac_compare_news("memoise", old = "1.0.0")) > 0) + expect_true(length(pac_compare_news("memoise", "1.0.0", "2.0.0")) > 0) +}) diff --git a/tests/testthat/test-description.R b/tests/testthat/test-description.R index 08f0163..26d0956 100644 --- a/tests/testthat/test-description.R +++ b/tests/testthat/test-description.R @@ -1,12 +1,3 @@ -test_that("cran_archive_file", { - skip_if_offline() - expect_true(length(cran_archive_file("dplyr", "1.0.0", "DESCRIPTION", "https://cran.rstudio.com/")) == 22) - expect_identical( - cran_archive_file("dplyr", "0.0.0.1", "DESCRIPTION", "https://cran.rstudio.com/"), - structure(list(), package = "dplyr", version = "0.0.0.1") - ) -}) - test_that("pacs::pac_description", { skip_if_offline() expect_true(length(pac_description("dplyr", version = "0.8.0")) == 23) diff --git a/tests/testthat/test-namespace.R b/tests/testthat/test-namespace.R index 380d85e..2d094da 100644 --- a/tests/testthat/test-namespace.R +++ b/tests/testthat/test-namespace.R @@ -8,8 +8,8 @@ test_that("pac_parse_namespace", { test_that("pacs::pac_namespace", { skip_if_offline() - expect_identical(pac_readnamespace_raw("dplyr", "0.0.0.0.1"), structure(list(), package = "dplyr", version = "0.0.0.0.1")) - expect_true(length(cran_archive_file("dplyr", "0.8.0", repos = "https://cran.rstudio.com/", "NAMESPACE")) == 498) + expect_identical(pac_readnamespace_raw("dplyr", "0.0.0.0.1"), structure(NA, package = "dplyr", version = "0.0.0.0.1")) + expect_true(length(read_cran_file("dplyr", "0.8.0", repos = "https://cran.rstudio.com/", "NAMESPACE")) == 498) expect_true(length(pac_namespace("dplyr", version = "0.8.0")) == 10) expect_identical(sort(pac_namespace("memoise", local = TRUE)$exports), sort(base::getNamespaceExports("memoise"))) expect_identical(pac_namespace("dplyr", "1.1.1.1"), NA) diff --git a/tests/testthat/test-read_cran.R b/tests/testthat/test-read_cran.R new file mode 100644 index 0000000..b8b3441 --- /dev/null +++ b/tests/testthat/test-read_cran.R @@ -0,0 +1,15 @@ +test_that("pacs:::read_cran_file valid input", { + skip_if_offline() + expect_silent(pacs:::read_cran_file("memoise", "1.1.0", "DESCRIPTION", repos = "https://cran.rstudio.com/")) + expect_silent(pacs:::read_cran_file("memoise", "1.1.0", "NAMESPACE", repos = "https://cran.rstudio.com/")) + expect_silent(pacs:::read_cran_file("memoise", "1.1.0", "NEWS")) +}) + +test_that("pacs::read_cran_file expected output", { + skip_if_offline() + expect_true(length(read_cran_file("dplyr", "1.0.0", "DESCRIPTION", "https://cran.rstudio.com/")) == 22) + expect_identical( + read_cran_file("dplyr", "0.0.0.1", "DESCRIPTION", "https://cran.rstudio.com/"), + NA + ) +}) diff --git a/tests/testthat/test-read_github.R b/tests/testthat/test-read_github.R new file mode 100644 index 0000000..2a76a44 --- /dev/null +++ b/tests/testthat/test-read_github.R @@ -0,0 +1,13 @@ + +test_that("pacs:::read_github_file valid input", { + skip_if_offline() + expect_silent(pacs:::read_github_file("memoise", "1.1.0", "DESCRIPTION", repos = "https://cran.rstudio.com/")) + expect_silent(pacs:::read_github_file("memoise", "1.1.0", "NAMESPACE", repos = "https://cran.rstudio.com/")) + expect_silent(pacs:::read_github_file("memoise", "1.1.0", "NEWS")) +}) + +test_that("pacs:::read_github_file invalid input", { + skip_if_offline() + expect_identical(pacs:::read_github_file("dplyr", "0.8.0.2", "DESCRIPTION", repos = "https://cran.rstudio.com/"),NA) + expect_error(pacs:::read_github_file("dplyr2", "0.8.0", "NAMESPACE", repos = "https://cran.rstudio.com/"), NA) +}) diff --git a/tests/testthat/test-validate_input.R b/tests/testthat/test-validate_input.R new file mode 100644 index 0000000..12ca302 --- /dev/null +++ b/tests/testthat/test-validate_input.R @@ -0,0 +1,20 @@ +test_that("test valid validate_pac_input input", { + expect_silent(validate_pac_input("memoise", version = NULL, at = NULL, local = TRUE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) + expect_silent(validate_pac_input("memoise", version = "1.1.0", at = NULL, local = FALSE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) +}) + +test_that("test invalid validate_pac_input input", { + expect_error(validate_pac_input("memoise", version = "1.1.0", at = NULL, local = TRUE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) + expect_error(validate_pac_input("memoise", version = "1.1.0", at = as.Date("2019-02-01"), local = FALSE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) +}) + +test_that("test valid validate_compare_input input", { + expect_silent(validate_compare_input("memoise", old = NULL, new = NULL, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) + expect_silent(validate_compare_input("memoise", old = "1.1.0", new = "1.1.1", lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) +}) + +test_that("test invalid validate_compare_input input", { + expect_error(validate_compare_input(c("memoise", "sth"), old = "1.1.0", new = "1.1.1", lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) + expect_error(validate_compare_input("memoise", old = c("1.1.0", "1"), new = "1.1.1", lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) + expect_error(validate_compare_input("memoise", old = "1.1.0", new = "1.1.1", lib.loc = .libPaths(), repos = 2)) +}) diff --git a/tests/testthat/test-validate_pac_input.R b/tests/testthat/test-validate_pac_input.R deleted file mode 100644 index cc7b832..0000000 --- a/tests/testthat/test-validate_pac_input.R +++ /dev/null @@ -1,9 +0,0 @@ -test_that("test valid validate_pac_input input", { - expect_silent(validate_pac_input("memoise", version = NULL, at = NULL, local = TRUE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) - expect_silent(validate_pac_input("memoise", version = "1.1.0", at = NULL, local = FALSE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) -}) - -test_that("test invalid validate_pac_input input", { - expect_error(validate_pac_input("memoise", version = "1.1.0", at = NULL, local = TRUE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) - expect_error(validate_pac_input("memoise", version = "1.1.0", at = as.Date("2019-02-01"), local = FALSE, lib.loc = .libPaths(), repos = "https://cran.rstudio.com/")) -})