From 490dae61fdab8cbd57e3dd4f2334f9646433552e Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Thu, 5 Sep 2024 15:11:46 -0400 Subject: [PATCH 1/6] add manifest handling --- R/lockfile.R | 14 ++++++++++---- R/paths.R | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/R/lockfile.R b/R/lockfile.R index f98513f71..a40786294 100644 --- a/R/lockfile.R +++ b/R/lockfile.R @@ -122,10 +122,9 @@ renv_lockfile_save <- function(lockfile, project) { } renv_lockfile_load <- function(project, strict = FALSE) { - - path <- renv_lockfile_path(project) - if (file.exists(path)) - return(renv_lockfile_read(path)) + lockfile_path <- renv_lockfile_path(project) + if (file.exists(lockfile_path)) + return(renv_lockfile_read(lockfile_path)) if (strict) { abort(c( @@ -134,6 +133,13 @@ renv_lockfile_load <- function(project, strict = FALSE) { )) } + manifest_path <- renv_manifest_path(project) + if (file.exists(manifest_path)) { + caution("No lockfile found; creating from `manifest.json`.") + renv_lockfile_from_manifest(manifest_path, lockfile_path) + return(renv_lockfile_read(lockfile_path)) + } + renv_lockfile_init(project = project) } diff --git a/R/paths.R b/R/paths.R index ebb353188..c7449a939 100644 --- a/R/paths.R +++ b/R/paths.R @@ -73,6 +73,15 @@ renv_paths_lockfile <- function(project = NULL) { } +renv_manifest_path <- function(project = NULL) { + + # otherwise, use default location (location location relative to renv folder) + project <- renv_project_resolve(project) + renv <- renv_paths_renv(project = project) + file.path(dirname(renv), "manifest.json") + +} + renv_paths_settings <- function(project = NULL) { renv_paths_renv("settings.json", project = project) } From ec2d797992ae9218b2dfe492f99517118ec2b916 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Thu, 5 Sep 2024 16:57:33 -0400 Subject: [PATCH 2/6] add test --- R/paths.R | 4 ++-- tests/testthat/test-lockfile.R | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/R/paths.R b/R/paths.R index c7449a939..e98fd1260 100644 --- a/R/paths.R +++ b/R/paths.R @@ -66,7 +66,7 @@ renv_paths_lockfile <- function(project = NULL) { return(override) } - # otherwise, use default location (location location relative to renv folder) + # otherwise, use default location (location relative to renv folder) project <- renv_project_resolve(project) renv <- renv_paths_renv(project = project) file.path(dirname(renv), "renv.lock") @@ -75,7 +75,7 @@ renv_paths_lockfile <- function(project = NULL) { renv_manifest_path <- function(project = NULL) { - # otherwise, use default location (location location relative to renv folder) + # otherwise, use default location (location relative to renv folder) project <- renv_project_resolve(project) renv <- renv_paths_renv(project = project) file.path(dirname(renv), "manifest.json") diff --git a/tests/testthat/test-lockfile.R b/tests/testthat/test-lockfile.R index 4e04f3d7c..d9527903f 100644 --- a/tests/testthat/test-lockfile.R +++ b/tests/testthat/test-lockfile.R @@ -49,6 +49,28 @@ test_that("we can create lockfiles from manifests", { }) +test_that("we create lockfile from a manifest automatically when no lockfile found", { + + skip_on_cran() + + project_dir <- tempfile() + dir.create(project_dir) + + manifest <- "resources/manifest.json" + expected_lock <- renv_lockfile_from_manifest("resources/manifest.json") + file.copy(manifest, file.path(project_dir, "manifest.json")) + + # when called with `strict = TRUE` does not create manifest + expect_error(renv_lockfile_load(project_dir, strict = TRUE)) + + # creates and reads lockfile + obtained_lock <- renv_lockfile_load(project_dir) + expect_identical(expected_lock, obtained_lock) + expect_true(file.exists(file.path(project_dir, "renv.lock"))) + + unlink(project_dir, recursive = TRUE) +}) + test_that("the Requirements field is read as character", { lockfile <- renv_lockfile_read(text = ' From 52291bc4d25386a54cf152bb5ede71d4cf53a6ca Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Thu, 5 Sep 2024 17:10:03 -0400 Subject: [PATCH 3/6] move function --- R/lockfile.R | 9 +++++++++ R/paths.R | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/R/lockfile.R b/R/lockfile.R index a40786294..59b56226b 100644 --- a/R/lockfile.R +++ b/R/lockfile.R @@ -116,6 +116,15 @@ renv_lockfile_path <- function(project) { renv_paths_lockfile(project = project) } +renv_manifest_path <- function(project = NULL) { + + # similar to default case of `renv_paths_lockfile()` + project <- renv_project_resolve(project) + renv <- renv_paths_renv(project = project) + file.path(dirname(renv), "manifest.json") + +} + renv_lockfile_save <- function(lockfile, project) { file <- renv_lockfile_path(project) renv_lockfile_write(lockfile, file = file) diff --git a/R/paths.R b/R/paths.R index e98fd1260..ee836e348 100644 --- a/R/paths.R +++ b/R/paths.R @@ -73,15 +73,6 @@ renv_paths_lockfile <- function(project = NULL) { } -renv_manifest_path <- function(project = NULL) { - - # otherwise, use default location (location relative to renv folder) - project <- renv_project_resolve(project) - renv <- renv_paths_renv(project = project) - file.path(dirname(renv), "manifest.json") - -} - renv_paths_settings <- function(project = NULL) { renv_paths_renv("settings.json", project = project) } From 7d823cd3b7475604f3d9e585ad9c4587435bb0c9 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Thu, 5 Sep 2024 17:23:34 -0400 Subject: [PATCH 4/6] remove duplicate log line --- R/manifest-convert.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/manifest-convert.R b/R/manifest-convert.R index da711c02e..4201585bb 100644 --- a/R/manifest-convert.R +++ b/R/manifest-convert.R @@ -73,10 +73,8 @@ renv_lockfile_from_manifest <- function(manifest = "manifest.json", if (is.na(lockfile)) return(lock) - # otherwise, write to file and report for user + # otherwise, write to file renv_lockfile_write(lock, file = lockfile) - fmt <- "- Lockfile written to %s." - writef(fmt, renv_path_pretty(lockfile)) invisible(lock) From 8dc332d88294b0a1fe762f193d3caf6fe18c6ea3 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Fri, 6 Sep 2024 13:43:34 -0400 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Kevin Ushey --- R/lockfile.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/lockfile.R b/R/lockfile.R index 59b56226b..543ce419a 100644 --- a/R/lockfile.R +++ b/R/lockfile.R @@ -131,9 +131,9 @@ renv_lockfile_save <- function(lockfile, project) { } renv_lockfile_load <- function(project, strict = FALSE) { - lockfile_path <- renv_lockfile_path(project) - if (file.exists(lockfile_path)) - return(renv_lockfile_read(lockfile_path)) + path <- renv_lockfile_path(project) + if (file.exists(path)) + return(renv_lockfile_read(path)) if (strict) { abort(c( @@ -142,11 +142,11 @@ renv_lockfile_load <- function(project, strict = FALSE) { )) } - manifest_path <- renv_manifest_path(project) - if (file.exists(manifest_path)) { + manifest <- renv_manifest_path(project) + if (file.exists(manifest)) { caution("No lockfile found; creating from `manifest.json`.") - renv_lockfile_from_manifest(manifest_path, lockfile_path) - return(renv_lockfile_read(lockfile_path)) + renv_lockfile_from_manifest(manifest, path) + return(renv_lockfile_read(path)) } renv_lockfile_init(project = project) From ce2b711575657a5ac83b8b0d6e5a13f4efe24977 Mon Sep 17 00:00:00 2001 From: Toph Allen Date: Fri, 6 Sep 2024 13:45:55 -0400 Subject: [PATCH 6/6] remove renv_manifest_path() function --- R/lockfile.R | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/R/lockfile.R b/R/lockfile.R index 543ce419a..e19ff9988 100644 --- a/R/lockfile.R +++ b/R/lockfile.R @@ -116,15 +116,6 @@ renv_lockfile_path <- function(project) { renv_paths_lockfile(project = project) } -renv_manifest_path <- function(project = NULL) { - - # similar to default case of `renv_paths_lockfile()` - project <- renv_project_resolve(project) - renv <- renv_paths_renv(project = project) - file.path(dirname(renv), "manifest.json") - -} - renv_lockfile_save <- function(lockfile, project) { file <- renv_lockfile_path(project) renv_lockfile_write(lockfile, file = file) @@ -142,7 +133,7 @@ renv_lockfile_load <- function(project, strict = FALSE) { )) } - manifest <- renv_manifest_path(project) + manifest <- file.path(project, "manifest.json") if (file.exists(manifest)) { caution("No lockfile found; creating from `manifest.json`.") renv_lockfile_from_manifest(manifest, path)