diff --git a/R/edit.R b/R/edit.R index 38895f84d..d5cb6d084 100644 --- a/R/edit.R +++ b/R/edit.R @@ -120,6 +120,7 @@ edit_r_profile <- function(scope = c("user", "project")) { #' @rdname edit edit_r_environ <- function(scope = c("user", "project")) { path <- scoped_path_r(scope, ".Renviron", envvar = "R_ENVIRON_USER") + challenge_renviron_precedence(path) edit_file(path) ui_bullets(c("_" = "Restart R for changes to take effect.")) invisible(path) @@ -266,3 +267,21 @@ edit_pkgdown_config <- function() { invisible(edit_file(path)) } } + + +challenge_renviron_precedence <- function(path) { + print(path) + if (!is.null(proj_find())) { + #checking for project specific .Renviron + if (file_exists(path(proj_find(), ".Renviron"))) { + ui_bullets(c( + "!" = "project-level .Renviron detected." + )) + if (ui_nah("Do you want to edit user level .Renviron?")) { + ui_abort( + "Use `edit_r_environ(scope = \"project\")` to edit project-level .Renviron." + ) + } + } + } +} diff --git a/tests/testthat/test-edit.R b/tests/testthat/test-edit.R index 3017e24fa..24952fcf2 100644 --- a/tests/testthat/test-edit.R +++ b/tests/testthat/test-edit.R @@ -150,6 +150,7 @@ test_that("edit_r_environ() ensures .Renviron exists in project", { expect_proj_file(".Renviron") }) + test_that("edit_r_makevars() ensures .R/Makevars exists in package", { create_local_package() edit_r_makevars("project") @@ -175,3 +176,48 @@ test_that("edit_git_ignore() ensures .gitignore exists in project", { edit_git_ignore("project") expect_proj_file(".gitignore") }) + +library(testthat) +library(cli) + +test_that("challenge_renviron_precedence aborts when project-level .Renviron exists and user rejects editing", { + expect_error( + with_mocked_bindings( + challenge_renviron_precedence(tempdir()), + proj_find = function() "/fake/project", + file_exists = function(path) TRUE, # pretend .Renviron exists + ui_nah = function(msg) TRUE # user rejects editing -> triggers abort + ), + regexp = "Use `edit_r_environ" + ) +}) + +test_that("challenge_renviron_precedence does nothing when no project", { + expect_no_error( + with_mocked_bindings( + challenge_renviron_precedence(tempdir()), + proj_find = function() NULL + ) + ) +}) + +test_that("challenge_renviron_precedence does nothing when no .Renviron file", { + expect_no_error( + with_mocked_bindings( + challenge_renviron_precedence(tempdir()), + proj_find = function() "/fake/project", + file_exists = function(path) FALSE + ) + ) +}) + +test_that("challenge_renviron_precedence prints bullet but does not abort when user accepts editing user-level", { + expect_no_error( + with_mocked_bindings( + challenge_renviron_precedence(tempdir()), + proj_find = function() "/fake/project", + file_exists = function(path) TRUE, # pretend .Renviron exists + ui_nah = function(msg) FALSE # user agrees -> no abort + ) + ) +})