Skip to content

Commit 52a809d

Browse files
authored
Use both Remote* and Github* fields for GitHub remotes (#746)
* prioritize RemoteSubDir; fall back to GithubSubDir * add test * fall back to Remote* fields in getRemoteInfo() * Add NEWS.md bullet
1 parent b3127db commit 52a809d

File tree

6 files changed

+90
-9
lines changed

6 files changed

+90
-9
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# packrat (development version)
22

3+
- When restoring GitHub-hosted packages, packrat will now look for both
4+
`Github*` and `Remote*` fields to determine where to install from. (#740)
5+
36
# packrat 0.9.3
47

58
- Update vendored `renv` with support for additional Linux distributions when

R/remote-info.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ getRemoteInfo <- function(pkgRecord) {
1313
as.data.frame(
1414
as.list(c(
1515
RemoteType = pkgRecord$source,
16-
GithubRepo = pkgRecord$gh_repo,
17-
GithubUsername = pkgRecord$gh_username,
18-
GithubRef = pkgRecord$gh_ref,
19-
GithubSHA1 = pkgRecord$gh_sha1,
20-
GithubSubdir = pkgRecord$gh_subdir
16+
GithubRepo = pkgRecord$gh_repo %||% pkgRecord$remote_repo,
17+
GithubUsername = pkgRecord$gh_username %||% pkgRecord$remote_username,
18+
GithubRef = pkgRecord$gh_ref %||% pkgRecord$remote_ref,
19+
GithubSHA1 = pkgRecord$gh_sha1 %||% pkgRecord$remote_sha,
20+
GithubSubdir = pkgRecord$gh_subdir %||% pkgRecord$remote_subdir
2121
)),
2222
stringsAsFactors = FALSE
2323
)

R/restore.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,12 +1152,15 @@ appendRemoteInfoToDescription <- function(src, dest, remote_info) {
11521152
basedir <- scratchDir
11531153
}
11541154

1155-
# Determine the true package root
1156-
if (remote_info$RemoteType == "github") {
1157-
remote_subdir <- remote_info$GithubSubdir
1158-
} else {
1155+
# Determine the true package root. RemoteSubDir is the more modern way of
1156+
# recording this info, but in some cases GithubSubDir may be used instead (or
1157+
# in addition). If package isn't in a subdirectory, then both should be NULL.
1158+
if (!is.null(remote_info$RemoteSubdir)) {
11591159
remote_subdir <- remote_info$RemoteSubdir
1160+
} else {
1161+
remote_subdir <- remote_info$GithubSubdir
11601162
}
1163+
11611164
if (length(remote_subdir) > 0) {
11621165
basedir <- file.path(basedir, remote_subdir)
11631166
}

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,7 @@ verboseLogger <- function(verbose) {
812812
function(...) {}
813813
}
814814
}
815+
816+
`%||%` <- function(x, y) {
817+
if (is.null(x)) y else x
818+
}

tests/testthat/test-remote-info.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ test_that("remote_info is correctly generated from a GitLab pkgRecord with a sub
9090
)
9191
expect_identical(getRemoteInfo(pkgRecordGitlabSubdir), expected)
9292
})
93+
94+
test_that("remote_info for a GitHub source falls back to Remote* fields if GIthub* ones are missing (but prioritize Github* ones)", {
95+
pkgRecordGithubSubdir <- list(
96+
name = "subadder",
97+
source = "github",
98+
version = "0.9.3.1",
99+
gh_repo = "sub_adder",
100+
remote_repo = "something_else",
101+
remote_username = "my-username",
102+
remote_ref = "HEAD",
103+
remote_sha1 = "abc123",
104+
remote_subdir = "subadder"
105+
)
106+
expected <- data.frame(
107+
RemoteType = "github",
108+
GithubRepo = "sub_adder",
109+
GithubUsername = "my-username",
110+
GithubRef = "HEAD",
111+
GithubSHA1 = "abc123",
112+
GithubSubdir = "subadder",
113+
stringsAsFactors = FALSE
114+
)
115+
expect_identical(getRemoteInfo(pkgRecordGithubSubdir), expected)
116+
})

tests/testthat/test-restore.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,50 @@ test_that("annotatePkgDesc annotates a package description", {
103103
expect_equal(result$InstallAgent, paste('packrat', packageVersion('packrat')))
104104
expect_equal(result$InstallSource, "CRAN")
105105
})
106+
107+
test_that("appendRemoteInfoToDescription uses RemoteSubdir", {
108+
parent_dir <- withr::local_tempdir()
109+
dest_dir <- withr::local_tempdir()
110+
# create package dir with subdirectory
111+
src_dir <- file.path(parent_dir, "bread", "toast")
112+
dir.create(src_dir, recursive = TRUE)
113+
114+
writeLines(
115+
readLines(test_path("packages/toast/DESCRIPTION")),
116+
file.path(src_dir, "DESCRIPTION")
117+
)
118+
119+
parent_tarball <- paste0(parent_dir, ".tar.gz")
120+
dest_tarball <- paste0(dest_dir, ".tar.gz")
121+
122+
in_dir(
123+
parent_dir,
124+
tar(
125+
tarfile = parent_tarball,
126+
files = "bread",
127+
compression = "gzip",
128+
tar = tar_binary()
129+
)
130+
)
131+
132+
remote_info <- data.frame(
133+
RemoteType = "github",
134+
RemoteHost = "github.com",
135+
RemoteRepo = "bread",
136+
RemoteUsername = "breakfaster",
137+
RemoteRef = "HEAD",
138+
RemoteSha = "abc123",
139+
RemoteSubdir = "toast",
140+
stringsAsFactors = FALSE
141+
)
142+
143+
appendRemoteInfoToDescription(
144+
src = parent_tarball,
145+
dest = dest_tarball,
146+
remote_info = remote_info
147+
)
148+
149+
untar(dest_tarball, exdir = dest_dir, tar = tar_binary())
150+
desc <- readLines(file.path(dest_dir, "toast", "DESCRIPTION"))
151+
expect_true("RemoteSubdir: toast" %in% desc)
152+
})

0 commit comments

Comments
 (0)