From 730bad139b99d757c432154d7d47a66526c45803 Mon Sep 17 00:00:00 2001 From: "Richard J. Acton" Date: Sun, 13 Oct 2024 19:00:14 +0100 Subject: [PATCH] styler styling --- R/renv_helpers.R | 161 ++++++++++++----------- tests/testthat/test-renv_helpers.R | 198 ++++++++++++++--------------- 2 files changed, 178 insertions(+), 181 deletions(-) diff --git a/R/renv_helpers.R b/R/renv_helpers.R index 654f27d1..260e6227 100644 --- a/R/renv_helpers.R +++ b/R/renv_helpers.R @@ -8,16 +8,16 @@ #' #' @importFrom jsonlite read_json read_renv_lock <- function(renv_lock_path = "renv.lock") { - if (!file.exists(renv_lock_path)) { - stop(renv_lock_path," does not exist!") + if (!file.exists(renv_lock_path)) { + stop(renv_lock_path, " does not exist!") + } + tryCatch( + renv_lock <- jsonlite::read_json(renv_lock_path), + error = function(e) { + stop("Error reading renv.lock file\n", e) } - tryCatch( - renv_lock <- jsonlite::read_json(renv_lock_path), - error = function(e) { - stop("Error reading renv.lock file\n", e) - } - ) - renv_lock + ) + renv_lock } #' renv_remote_pkg @@ -42,35 +42,34 @@ read_renv_lock <- function(renv_lock_path = "renv.lock") { #' } renv_remote_pkg <- function( renv_lock_pkg_info, - type = renv_lock_pkg_info$RemoteType -) { - type <- match.arg(type, c( - "github", "gitlab" - # , "bitbucket", "git", "local", "svn", "url", "version", "cran", "bioc" - )) - pkg_info <- vector(mode = "list", length = 3) - names(pkg_info) <- c("package_name", "repo_url", "commit") - switch (type, - "github" = { - pkg_info[[1]] <- renv_lock_pkg_info$Package - pkg_info[[2]] <- paste0( - # RemoteHost is listed as api.github.com for some reason - "https://github.com/", renv_lock_pkg_info$RemoteUser, "/", - renv_lock_pkg_info$RemoteRepo - ) - pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha - }, - "gitlab" = { - pkg_info[[1]] <- renv_lock_pkg_info$Package - pkg_info[[2]] <- paste0( - "https://", renv_lock_pkg_info$RemoteHost, "/", - renv_lock_pkg_info$RemoteUser, "/", - renv_lock_pkg_info$RemoteRepo - ) - pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha - } - ) - pkg_info + type = renv_lock_pkg_info$RemoteType) { + type <- match.arg(type, c( + "github", "gitlab" + # , "bitbucket", "git", "local", "svn", "url", "version", "cran", "bioc" + )) + pkg_info <- vector(mode = "list", length = 3) + names(pkg_info) <- c("package_name", "repo_url", "commit") + switch(type, + "github" = { + pkg_info[[1]] <- renv_lock_pkg_info$Package + pkg_info[[2]] <- paste0( + # RemoteHost is listed as api.github.com for some reason + "https://github.com/", renv_lock_pkg_info$RemoteUser, "/", + renv_lock_pkg_info$RemoteRepo + ) + pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha + }, + "gitlab" = { + pkg_info[[1]] <- renv_lock_pkg_info$Package + pkg_info[[2]] <- paste0( + "https://", renv_lock_pkg_info$RemoteHost, "/", + renv_lock_pkg_info$RemoteUser, "/", + renv_lock_pkg_info$RemoteRepo + ) + pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha + } + ) + pkg_info } #' renv2nix @@ -87,49 +86,48 @@ renv2nix <- function( renv_lock_path = "renv.lock", return_rix_call = FALSE, method = "fast", - ... -) { - method <- match.arg(method, c("fast", "accurate")) - renv_lock <- read_renv_lock(renv_lock_path = renv_lock_path) - if (method == "fast") { - repo_pkgs_lgl <- logical(length = length(renv_lock$Packages)) - for (i in seq_along(renv_lock$Packages)) { - if (renv_lock$Packages[[i]]$Source == "Repository") { - repo_pkgs_lgl[i] <- TRUE - } else { - repo_pkgs_lgl[i] <- FALSE - } - } - git_pkgs <- NULL - # as local_r_pkgs expects an archive not sure how to set type here.. - # local_r_pkgs <- NULL - if (any(!repo_pkgs_lgl)) { - for (x in renv_lock$Packages[!repo_pkgs_lgl]) { - git_pkgs[[x$Package]] <- renv_remote_pkg(x) - } - } - rix_call <- call("rix", - r_ver = renv_lock$R$Version, - r_pkgs = names(renv_lock$Packages[repo_pkgs_lgl]), - git_pkgs = git_pkgs#, - #local_r_pkgs = local_r_pkgs - ) - dots <- list(...) - for(arg in names(dots)) { - rix_call[[arg]] <- dots[[arg]] - } + ...) { + method <- match.arg(method, c("fast", "accurate")) + renv_lock <- read_renv_lock(renv_lock_path = renv_lock_path) + if (method == "fast") { + repo_pkgs_lgl <- logical(length = length(renv_lock$Packages)) + for (i in seq_along(renv_lock$Packages)) { + if (renv_lock$Packages[[i]]$Source == "Repository") { + repo_pkgs_lgl[i] <- TRUE + } else { + repo_pkgs_lgl[i] <- FALSE + } + } + git_pkgs <- NULL + # as local_r_pkgs expects an archive not sure how to set type here.. + # local_r_pkgs <- NULL + if (any(!repo_pkgs_lgl)) { + for (x in renv_lock$Packages[!repo_pkgs_lgl]) { + git_pkgs[[x$Package]] <- renv_remote_pkg(x) + } + } + rix_call <- call("rix", + r_ver = renv_lock$R$Version, + r_pkgs = names(renv_lock$Packages[repo_pkgs_lgl]), + git_pkgs = git_pkgs # , + # local_r_pkgs = local_r_pkgs + ) + dots <- list(...) + for (arg in names(dots)) { + rix_call[[arg]] <- dots[[arg]] + } - if (return_rix_call) { - # print(rix_call) - return(rix_call) - } - eval(rix_call) - } else { - stop( - "'accurate' renv based environments with package version matching", - " is not yet implemented :(" - ) + if (return_rix_call) { + # print(rix_call) + return(rix_call) } + eval(rix_call) + } else { + stop( + "'accurate' renv based environments with package version matching", + " is not yet implemented :(" + ) + } } #' renv_lock_r_ver @@ -147,7 +145,6 @@ renv2nix <- function( #' rix(r_ver = renv_lock_r_ver()) #' renv_lock_r_ver <- function(renv_lock_path = "renv.lock") { - renv_lock <- read_renv_lock(renv_lock_path = renv_lock_path) - renv_lock$R$Version + renv_lock <- read_renv_lock(renv_lock_path = renv_lock_path) + renv_lock$R$Version } - diff --git a/tests/testthat/test-renv_helpers.R b/tests/testthat/test-renv_helpers.R index bb83e424..a868656c 100644 --- a/tests/testthat/test-renv_helpers.R +++ b/tests/testthat/test-renv_helpers.R @@ -1,116 +1,116 @@ testthat::test_that("Testing `read_renv_lock()`", { - testthat::expect_error(read_renv_lock("nosuchfile"), "nosuchfile does not exist") - tmpf <- tempfile() - cat("not json", file = tmpf) - testthat::expect_error(read_renv_lock(tmpf), "Error reading renv\\.lock file") - unlink(tmpf) + testthat::expect_error(read_renv_lock("nosuchfile"), "nosuchfile does not exist") + tmpf <- tempfile() + cat("not json", file = tmpf) + testthat::expect_error(read_renv_lock(tmpf), "Error reading renv\\.lock file") + unlink(tmpf) }) testthat::test_that("Testing `renv_remote_pkg()`", { - synthetic_renv_lock_example <- list( - githubpkg = list( - Package = "githubpkg", - RemoteType = "github", - RemoteUser = "user", - RemoteRepo = "repo", - RemoteSha = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz", - RemoteHost = "api.github.com" - ), - gitlabpkg = list( - Package = "gitlabpkg", - RemoteType = "gitlab", - RemoteUser = "user", - RemoteRepo = "repo", - RemoteSha = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh", - RemoteHost = "gitlab.com" - ) + synthetic_renv_lock_example <- list( + githubpkg = list( + Package = "githubpkg", + RemoteType = "github", + RemoteUser = "user", + RemoteRepo = "repo", + RemoteSha = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz", + RemoteHost = "api.github.com" + ), + gitlabpkg = list( + Package = "gitlabpkg", + RemoteType = "gitlab", + RemoteUser = "user", + RemoteRepo = "repo", + RemoteSha = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh", + RemoteHost = "gitlab.com" ) + ) - expected_git_pkg <- list( - githubpkg = list( - package_name = "githubpkg", - repo_url = "https://github.com/user/repo", - commit = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz" - ), - gitlabpkg = list( - package_name = "gitlabpkg", - repo_url = "https://gitlab.com/user/repo", - commit = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh" - ) + expected_git_pkg <- list( + githubpkg = list( + package_name = "githubpkg", + repo_url = "https://github.com/user/repo", + commit = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz" + ), + gitlabpkg = list( + package_name = "gitlabpkg", + repo_url = "https://gitlab.com/user/repo", + commit = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh" ) + ) - testthat::expect_equal( - renv_remote_pkg(synthetic_renv_lock_example$githubpkg), expected_git_pkg$githubpkg - ) - testthat::expect_equal( - renv_remote_pkg(synthetic_renv_lock_example$gitlabpkg), expected_git_pkg$gitlabpkg - ) + testthat::expect_equal( + renv_remote_pkg(synthetic_renv_lock_example$githubpkg), expected_git_pkg$githubpkg + ) + testthat::expect_equal( + renv_remote_pkg(synthetic_renv_lock_example$gitlabpkg), expected_git_pkg$gitlabpkg + ) }) testthat::test_that("Testing `renv2nix()`", { - synthetic_renv_lock_example <- list( - R = list(Version = "4.4.1"), - Packages = list( - MASS = list( - Package = "MASS", - Version = "7.3-56", - Source = "Repository", - Repository = "CRAN", - Hash = "af0e1955cb80bb36b7988cc657db261e", - Requirements = c() - ), - R6 = list( - Package = "R6", - Version = "2.5.1", - Source = "Repository", - Repository = "RSPM", - Hash = "470851b6d5d0ac559e9d01bb352b4021", - Requirements = c() - ), - githubpkg = list( - Package = "githubpkg", - Source = "GitHub", - RemoteType = "github", - RemoteUser = "user", - RemoteRepo = "repo", - RemoteSha = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz", - RemoteHost = "api.github.com" - ), - gitlabpkg = list( - Package = "gitlabpkg", - Source = "GitLab", - RemoteType = "gitlab", - RemoteUser = "user", - RemoteRepo = "repo", - RemoteSha = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh", - RemoteHost = "gitlab.com" - ) - ) + synthetic_renv_lock_example <- list( + R = list(Version = "4.4.1"), + Packages = list( + MASS = list( + Package = "MASS", + Version = "7.3-56", + Source = "Repository", + Repository = "CRAN", + Hash = "af0e1955cb80bb36b7988cc657db261e", + Requirements = c() + ), + R6 = list( + Package = "R6", + Version = "2.5.1", + Source = "Repository", + Repository = "RSPM", + Hash = "470851b6d5d0ac559e9d01bb352b4021", + Requirements = c() + ), + githubpkg = list( + Package = "githubpkg", + Source = "GitHub", + RemoteType = "github", + RemoteUser = "user", + RemoteRepo = "repo", + RemoteSha = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz", + RemoteHost = "api.github.com" + ), + gitlabpkg = list( + Package = "gitlabpkg", + Source = "GitLab", + RemoteType = "gitlab", + RemoteUser = "user", + RemoteRepo = "repo", + RemoteSha = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh", + RemoteHost = "gitlab.com" + ) ) - tmpf <- tempfile() - jsonlite::write_json(synthetic_renv_lock_example, tmpf, auto_unbox = TRUE) - test_call <- call( - "rix", r_ver = "4.4.1", r_pkgs = c("MASS", "R6"), git_pkgs = list( - githubpkg = list( - package_name = "githubpkg", - repo_url = "https://github.com/user/repo", - commit = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz" - ), - gitlabpkg = list( - package_name = "gitlabpkg", - repo_url = "https://gitlab.com/user/repo", - commit = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh" - ) - ) + ) + tmpf <- tempfile() + jsonlite::write_json(synthetic_renv_lock_example, tmpf, auto_unbox = TRUE) + test_call <- call( + "rix", + r_ver = "4.4.1", r_pkgs = c("MASS", "R6"), git_pkgs = list( + githubpkg = list( + package_name = "githubpkg", + repo_url = "https://github.com/user/repo", + commit = "yki8snny7wgpjolz5cq0bwxjshxdd0xv0mcyygoz" + ), + gitlabpkg = list( + package_name = "gitlabpkg", + repo_url = "https://gitlab.com/user/repo", + commit = "45p9megdp0i5230rtw1lisy6rquc466zb9yxn7eh" + ) ) - testthat::expect_equal(test_call, renv2nix(tmpf, return_rix_call = TRUE)) - unlink(tmpf) + ) + testthat::expect_equal(test_call, renv2nix(tmpf, return_rix_call = TRUE)) + unlink(tmpf) }) testthat::test_that("Testing `renv_lock_r_ver()`", { - tmpf <- tempfile() - jsonlite::write_json(list(R = list(Version = "4.4.1")), tmpf, auto_unbox = TRUE) - testthat::expect_equal(renv_lock_r_ver(renv_lock_path = tmpf), "4.4.1") - unlink(tmpf) + tmpf <- tempfile() + jsonlite::write_json(list(R = list(Version = "4.4.1")), tmpf, auto_unbox = TRUE) + testthat::expect_equal(renv_lock_r_ver(renv_lock_path = tmpf), "4.4.1") + unlink(tmpf) }) -