Skip to content

Commit

Permalink
Test record_versions()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Mar 5, 2024
1 parent fa0b67c commit 9f7aae7
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 8 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Imports:
jsonlite,
nanonext,
pkgsearch,
utils,
vctrs
Encoding: UTF-8
Language: en-US
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(assert_cran_url)
export(assert_package)
export(assert_release_exists)
export(build_universe)
export(get_current_versions)
export(record_versions)
export(review_pull_request)
export(review_pull_requests)
Expand All @@ -16,4 +17,6 @@ importFrom(nanonext,ncurl)
importFrom(nanonext,parse_url)
importFrom(nanonext,status_code)
importFrom(pkgsearch,cran_package)
importFrom(utils,available.packages)
importFrom(utils,compareVersion)
importFrom(vctrs,vec_rbind)
1 change: 1 addition & 0 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
#' @importFrom jsonlite parse_json read_json write_json
#' @importFrom nanonext ncurl parse_url status_code
#' @importFrom pkgsearch cran_package
#' @importFrom utils available.packages compareVersion
#' @importFrom vctrs vec_rbind
NULL
22 changes: 16 additions & 6 deletions R/record_versions.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#' @title Record the manifest of package versions.
#' @export
#' @keywords internal
#' @description Record the manifest of versions of packages
#' and their MD5 hashes.
#' @details This function tracks a manifest containing the current version,
Expand All @@ -20,26 +19,37 @@
#' @param issues Character of length 1, file path to a JSON file
#' which records packages with version issues.
#' @param repos Character string of package repositories to track.
#' @param current A data frame of current versions and hashes of packages
#' in `repos`. This argument is exposed for testing only.
record_versions <- function(
manifest = "versions.json",
issues = "version_issues.json",
repos = "https://r-releases.r-universe.dev"
repos = "https://r-releases.r-universe.dev",
current = r.releases.utils::get_current_versions(repos = repos)
) {
current <- get_current_versions(repos = repos)
if (!file.exists(manifest)) {
jsonlite::write_json(x = current, path = manifest, pretty = TRUE)
return(invisible())
}
previous <- read_versions_previous(manifest = manifest)
new <- update_version_manifest(current = current, previous = previous)
jsonlite::write_json(x = new, path = manifest, pretty = TRUE)
new_issues <- new[!versions_aligned(new = new),, drop = FALSE] # nolint
new_issues <- new[!versions_aligned(manifest = new),, drop = FALSE] # nolint
jsonlite::write_json(x = new_issues, path = issues, pretty = TRUE)
invisible()
}

get_current_versions <- function(repos) {
out <- available.packages(repos = "https://r-releases.r-universe.dev")
#' @title Get the current versions of packages
#' @export
#' @keywords internal
#' @description Get the current versions of packages in the repos.
#' @return A data frame of packages with their current versions and MD5
#' hashes.
#' @inheritParams record_versions
get_current_versions <- function(
repos = "https://r-releases.r-universe.dev"
) {
out <- utils::available.packages(repos = repos)
out <- as.data.frame(out)
out <- out[, c("Package", "Version", "MD5sum")]
colnames(out) <- c("package", "version_current", "md5_current")
Expand Down
19 changes: 19 additions & 0 deletions man/get_current_versions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/record_versions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

169 changes: 169 additions & 0 deletions tests/test-record_versions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Temporary files used in the mock test.
manifest <- tempfile()
issues <- tempfile()

# First update to the manifest.
contents <- data.frame(
package = c(
"package_unmodified",
"version_decremented",
"version_incremented",
"version_unmodified"
),
version_current = rep("1.0.0", 4L),
md5_current = rep("md5_1.0.0", 4L)
)
r.releases.utils::record_versions(
manifest = manifest,
issues = issues,
current = contents
)
written <- jsonlite::read_json(manifest)
expected <- list(
list(
package = "package_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0"
),
list(
package = "version_decremented",
version_current = "1.0.0",
md5_current = "md5_1.0.0"
),
list(
package = "version_incremented",
version_current = "1.0.0",
md5_current = "md5_1.0.0"
),
list(
package = "version_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0"
)
)
stopifnot(identical(written, expected))
stopifnot(!file.exists(issues))

# Update the manifest after no changes to packages or versions.
r.releases.utils::record_versions(
manifest = manifest,
issues = issues,
current = contents
)
written <- jsonlite::read_json(manifest)
expected <- list(
list(
package = "package_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_decremented",
version_current = "1.0.0",
md5_current = "md5_1.0.0",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_incremented",
version_current = "1.0.0",
md5_current = "md5_1.0.0",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
)
)
stopifnot(identical(written, expected))
stopifnot(file.exists(issues))
stopifnot(identical(jsonlite::read_json(issues), list()))

# Update the packages in all the ways indicated above.
index <- contents$package == "version_decremented"
contents$version_current[index] <- "0.0.1"
contents$md5_current[index] <- "md5_0.0.1"
index <- contents$package == "version_incremented"
contents$version_current[index] <- "2.0.0"
contents$md5_current[index] <- "md5_2.0.0"
index <- contents$package == "version_unmodified"
contents$version_current[index] <- "1.0.0"
contents$md5_current[index] <- "md5_1.0.0-modified"
r.releases.utils::record_versions(
manifest = manifest,
issues = issues,
current = contents
)
written <- jsonlite::read_json(manifest)
expected <- list(
list(
package = "package_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_decremented",
version_current = "0.0.1",
md5_current = "md5_0.0.1",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_incremented",
version_current = "2.0.0",
md5_current = "md5_2.0.0",
version_highest = "2.0.0",
md5_highest = "md5_2.0.0"
),
list(
package = "version_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0-modified",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
)
)
stopifnot(identical(written, expected))
stopifnot(file.exists(issues))
written_issues <- jsonlite::read_json(issues)
expected_issues <- list(
list(
package = "version_decremented",
version_current = "0.0.1",
md5_current = "md5_0.0.1",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
),
list(
package = "version_unmodified",
version_current = "1.0.0",
md5_current = "md5_1.0.0-modified",
version_highest = "1.0.0",
md5_highest = "md5_1.0.0"
)
)
stopifnot(identical(written_issues, expected_issues))

# Remove temporary files
unlink(c(manifest, issues))

# The manifest can be created and updated from the actual repo.
manifest <- tempfile()
issues <- tempfile()
r.releases.utils::record_versions(manifest = manifest, issues = issues)
stopifnot(file.exists(manifest))
r.releases.utils::record_versions(manifest = manifest, issues = issues)
contents <- jsonlite::read_json(manifest)
stopifnot(is.character(contents[[1L]]$package))
stopifnot(length(contents[[1L]]$package) == 1L)
stopifnot(file.exists(manifest))
stopifnot(file.exists(issues))
unlink(c(manifest, issues))

0 comments on commit 9f7aae7

Please sign in to comment.