Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions R/edit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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."
)
}
}
}
}
46 changes: 46 additions & 0 deletions tests/testthat/test-edit.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
expect_proj_file(".Renviron")
})


test_that("edit_r_makevars() ensures .R/Makevars exists in package", {
create_local_package()
edit_r_makevars("project")
Expand All @@ -175,3 +176,48 @@
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()),

Check warning on line 186 in tests/testthat/test-edit.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-edit.R,line=186,col=37,[undesirable_function_linter] Avoid undesirable function "tempdir". As an alternative, use the fs package.
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()),

Check warning on line 198 in tests/testthat/test-edit.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-edit.R,line=198,col=37,[undesirable_function_linter] Avoid undesirable function "tempdir". As an alternative, use the fs package.
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()),

Check warning on line 207 in tests/testthat/test-edit.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-edit.R,line=207,col=37,[undesirable_function_linter] Avoid undesirable function "tempdir". As an alternative, use the fs package.
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()),

Check warning on line 217 in tests/testthat/test-edit.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-edit.R,line=217,col=37,[undesirable_function_linter] Avoid undesirable function "tempdir". As an alternative, use the fs package.
proj_find = function() "/fake/project",
file_exists = function(path) TRUE, # pretend .Renviron exists
ui_nah = function(msg) FALSE # user agrees -> no abort
)
)
})
Loading