diff --git a/NEWS.md b/NEWS.md index b05ca039..8536d9ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,10 @@ - When restoring GitHub-hosted packages, packrat will now look for both `Github*` and `Remote*` fields to determine where to install from. (#740) +- When restoring packages from CRAN-like repositories, names are no + longer used to detect if these are actually git-like. This prevents + issues if you name a CRAN-like repository something like "GitHub". (#747) + # packrat 0.9.3 - Update vendored `renv` with support for additional Linux distributions when diff --git a/R/restore.R b/R/restore.R index e4b27ab1..5be4caab 100644 --- a/R/restore.R +++ b/R/restore.R @@ -25,12 +25,17 @@ isFromCranlikeRepo <- function(pkgRecord, repos) { return(TRUE) } - # for records that do declare a source, ensure it's not 'source', 'github', 'bitbucket', or 'gitlab'. + # check if there's a remote host and repo for github, bitbucket, or gitlab + if (!is.null(pkgRecord$remote_host) && !is.null(pkgRecord$remote_repo)) { + return(FALSE) + } + + # for records that do declare a source, ensure it's not 'source'. # in previous releases of packrat, we attempted to match the repository name # with one of the existing repositories; however, this caused issues in # certain environments (the names declared repositories in the lockfile, and # the the names of the active repositories in the R session, may not match) - !tolower(source) %in% c("source", "github", "bitbucket", "gitlab") + !tolower(source) %in% c("source") } # Given a package record and a database of packages, check to see if diff --git a/tests/testthat/test-restore.R b/tests/testthat/test-restore.R index d8c49dd3..656c8ebb 100644 --- a/tests/testthat/test-restore.R +++ b/tests/testthat/test-restore.R @@ -150,3 +150,92 @@ test_that("appendRemoteInfoToDescription uses RemoteSubdir", { desc <- readLines(file.path(dest_dir, "toast", "DESCRIPTION")) expect_true("RemoteSubdir: toast" %in% desc) }) + +test_that("isFromCranlikeRepo returns TRUE for CRAN source", { + pkgRecord <- list( + name = "ggplot2", + source = "CRAN", + version = "3.4.0" + ) + + repos <- c(CRAN = "https://cran.r-project.org") + + expect_true(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns TRUE for CustomCRANLikeRepository class", { + pkgRecord <- structure( + list( + name = "ggplot2", + source = "CRAN", + version = "3.4.0" + ), + class = c("packageRecord", "CustomCRANLikeRepository") + ) + + repos <- c(CRAN = "https://cran.r-project.org") + + expect_true(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns FALSE for source package", { + pkgRecord <- list( + name = "mypackage", + source = "source", + version = "1.0.0" + ) + + repos <- c(CRAN = "https://cran.r-project.org") + + expect_false(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns FALSE if remote_repo/remote_host are present", { + pkgRecord <- list( + name = "mypackage", + source = "GitHub", + version = "1.0.0", + remote_host = "github.com", + remote_repo = "mypackage" + ) + + repos <- c(GitHub = "https://github.com/me/mypackage") + + expect_false(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns TRUE for BioConductor source", { + pkgRecord <- list( + name = "GenomicRanges", + source = "Bioconductor", + version = "1.50.0" + ) + + repos <- c(BioCsoft = "https://bioconductor.org/packages/3.16/bioc") + + expect_true(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns TRUE for custom repository source", { + pkgRecord <- list( + name = "mypackage", + source = "MyRepo", + version = "1.0.0" + ) + + repos <- c(MyRepo = "https://example.com/repo") + + expect_true(isFromCranlikeRepo(pkgRecord, repos)) +}) + +test_that("isFromCranlikeRepo returns TRUE for a CRAN-like source named Github", { + pkgRecord <- list( + name = "mypackage", + source = "GitHub", + version = "1.0.0" + ) + + repos <- c(GitHub = "https://example.com/repo") + + expect_true(isFromCranlikeRepo(pkgRecord, repos)) +})