From 2b62be410ba39cb22d181f94a56ebf15701de932 Mon Sep 17 00:00:00 2001 From: Bruno Rodrigues Date: Mon, 1 Jul 2024 17:09:54 +0200 Subject: [PATCH] check for existence of .Rprofile --- R/rix.R | 31 ++++++--- tests/testthat/test-nix_build.R | 10 ++- tests/testthat/test-rix.R | 109 +++++++++++++++++++++----------- tests/testthat/test-rix_init.R | 4 ++ tests/testthat/test-with_nix.R | 36 +++++------ 5 files changed, 117 insertions(+), 73 deletions(-) diff --git a/R/rix.R b/R/rix.R index eee8500e..f4041c75 100644 --- a/R/rix.R +++ b/R/rix.R @@ -207,12 +207,19 @@ for more details." paste0(project_path, "/default.nix") } + .Rprofile_path <- if (project_path == ".") { + ".Rprofile" + } else { + paste0(project_path, "/.Rprofile") + } + # Find url to use # In case of bleeding or frozen edge, the rstats-on-nix/nixpkgs # fork is used. Otherwise, upstream NixOS/nixpkgs nix_repo <- make_nixpkgs_url(r_ver) default.nix_path <- file.path(default.nix_path) + .Rprofile_path <- file.path(.Rprofile_path) rix_call <- match.call() @@ -288,16 +295,22 @@ for more details." if (!file.exists(default.nix_path) || overwrite) { writeLines(default.nix, default.nix_path) - rix_init(project_path = project_path, - rprofile_action = "create_missing", - # 'verbose' is too chatty for rix() - # hence why it's transformed to "simple" - message_type = ifelse(message_type == "verbose", - "simple", message_type)) + if(!file.exists(.Rprofile_path)){ + rix_init(project_path = project_path, + rprofile_action = "create_missing", + # 'verbose' is too chatty for rix() + # hence why it's transformed to "simple" + message_type = ifelse(message_type == "verbose", + "simple", message_type)) + + if(message_type != "quiet"){ + message("\n\n### Successfully generated `default.nix` and `.Rprofile` ###\n\n") + } - if(message_type != "quiet"){ - message("\n\n### Successfully generated `default.nix` and `.Rprofile` ###\n\n") - } + } else if(all(!grepl("File generated by `rix::rix_init()", + readLines(.Rprofile_path)))){ + warning("\n\n### .Rprofile file already exists. You may want to call rix_init(profile_action = 'append') manually to ensure correct functioning of your Nix environment. ###\n\n") + } } else { project_path <- if(project_path == ".") { diff --git a/tests/testthat/test-nix_build.R b/tests/testthat/test-nix_build.R index d82c6189..4c756f40 100644 --- a/tests/testthat/test-nix_build.R +++ b/tests/testthat/test-nix_build.R @@ -11,12 +11,6 @@ testthat::test_that("Testing that `nix_build()` builds derivation", { path_subshell <- tempdir() - rix_init( - project_path = path_subshell, - rprofile_action = "overwrite", - message_type = "simple" - ) - rix( r_ver = "latest", overwrite = TRUE, @@ -29,4 +23,8 @@ testthat::test_that("Testing that `nix_build()` builds derivation", { project_path = path_subshell ) ) + + on.exit({ + unlink(path_subshell, recursive = TRUE, force = TRUE) + }) }) diff --git a/tests/testthat/test-rix.R b/tests/testthat/test-rix.R index d56f98f4..b030bac3 100644 --- a/tests/testthat/test-rix.R +++ b/tests/testthat/test-rix.R @@ -56,28 +56,33 @@ testthat::test_that("rix(), ide is 'other' or 'code'", { path_default_nix <- normalizePath(path_default_nix) save_default_nix_test <- function(ide, path_default_nix) { - rix( - r_ver = "4.3.1", - r_pkgs = c("data.table", "janitor", "AER@1.2-8", "quarto"), - tex_pkgs = c("amsmath"), - git_pkgs = list( - list( - package_name = "housing", - repo_url = "https://github.com/rap4all/housing/", - branch_name = "fusen", - commit = "1c860959310b80e67c41f7bbdc3e84cef00df18e" + # We need to add this because this function gets called + # twice, so the generated .Rprofile is there already and + # calling the function again raises the warning. + suppressWarnings( + rix( + r_ver = "4.3.1", + r_pkgs = c("data.table", "janitor", "AER@1.2-8", "quarto"), + tex_pkgs = c("amsmath"), + git_pkgs = list( + list( + package_name = "housing", + repo_url = "https://github.com/rap4all/housing/", + branch_name = "fusen", + commit = "1c860959310b80e67c41f7bbdc3e84cef00df18e" + ), + list( + package_name = "fusen", + repo_url = "https://github.com/ThinkR-open/fusen", + branch_name = "main", + commit = "d617172447d2947efb20ad6a4463742b8a5d79dc" + ) ), - list( - package_name = "fusen", - repo_url = "https://github.com/ThinkR-open/fusen", - branch_name = "main", - commit = "d617172447d2947efb20ad6a4463742b8a5d79dc" - ) - ), - ide = ide, - project_path = path_default_nix, - overwrite = TRUE, - shell_hook = NULL + ide = ide, + project_path = path_default_nix, + overwrite = TRUE, + shell_hook = NULL + ) ) file.path(path_default_nix, "default.nix") @@ -114,13 +119,16 @@ testthat::test_that("Quarto gets added to sys packages", { path_default_nix <- normalizePath(tempdir()) save_default_nix_test <- function(pkgs, interface, path_default_nix) { - rix( - r_ver = "4.3.1", - r_pkgs = pkgs, - ide = interface, - project_path = path_default_nix, - overwrite = TRUE, - shell_hook = NULL + # Because of rix_init, see above + suppressWarnings( + rix( + r_ver = "4.3.1", + r_pkgs = pkgs, + ide = interface, + project_path = path_default_nix, + overwrite = TRUE, + shell_hook = NULL + ) ) file.path(path_default_nix, "default.nix") @@ -147,6 +155,10 @@ testthat::test_that("Quarto gets added to sys packages", { ), name = "yes_quarto_default.nix" ) + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) @@ -171,13 +183,17 @@ testthat::test_that("r_pkgs = NULL and ide = 'rstudio' work together", { testthat::announce_snapshot_file("rix/null_pkgs_rstudio.nix") testthat::expect_snapshot_file( - path = save_default_nix_test( - pkgs = NULL, - interface = "rstudio", - path_default_nix - ), - name = "null_pkgs_rstudio.nix" - ) + path = save_default_nix_test( + pkgs = NULL, + interface = "rstudio", + path_default_nix + ), + name = "null_pkgs_rstudio.nix" + ) + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) @@ -200,9 +216,13 @@ testthat::test_that("If on darwin and ide = rstudio, raise warning", { } testthat::expect_warning( - save_default_nix_test(path_default_nix), - regexp = "refer to the macOS" - ) + save_default_nix_test(path_default_nix), + regexp = "refer to the macOS" + ) + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) testthat::test_that("If R version is 4.4.0, raise warning", { @@ -225,6 +245,10 @@ testthat::test_that("If R version is 4.4.0, raise warning", { save_default_nix_test(path_default_nix), regexp = "version is not available" ) + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) testthat::test_that("If R version is <= 4.1.1, raise warning", { @@ -247,6 +271,11 @@ testthat::test_that("If R version is <= 4.1.1, raise warning", { save_default_nix_test(path_default_nix), regexp = "older version of R" ) + + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) testthat::test_that("If R version is == 3.5.3, raise warning", { @@ -269,6 +298,10 @@ testthat::test_that("If R version is == 3.5.3, raise warning", { save_default_nix_test(path_default_nix), regexp = "older version of R" ) + + on.exit({ + unlink(path_default_nix, recursive = TRUE, force = TRUE) + }) }) testthat::test_that("rix(), bleeding_edge", { diff --git a/tests/testthat/test-rix_init.R b/tests/testthat/test-rix_init.R index 6612219f..5ed939c9 100644 --- a/tests/testthat/test-rix_init.R +++ b/tests/testthat/test-rix_init.R @@ -20,4 +20,8 @@ testthat::test_that("Snapshot test of rix_init()", { path = save_rix_init_test(path_env_nix), name = "golden_Rprofile.txt", ) + + on.exit({ + unlink(path_env_nix, recursive = TRUE, force = TRUE) + }) }) diff --git a/tests/testthat/test-with_nix.R b/tests/testthat/test-with_nix.R index 647c2e04..71c6e3ad 100644 --- a/tests/testthat/test-with_nix.R +++ b/tests/testthat/test-with_nix.R @@ -14,12 +14,6 @@ testthat::test_that("Testing `with_nix()` if Nix is installed", { path_subshell <- tempdir() - rix_init( - project_path = path_subshell, - rprofile_action = "overwrite", - message_type = "simple" - ) - # Suppress the warning related to generating an expression # for an old version of R suppressWarnings( @@ -49,6 +43,10 @@ testthat::test_that("Testing `with_nix()` if Nix is installed", { testthat::expect_true( all(c(2, 6, 5, 8, 9) == out_subshell) ) + + on.exit({ + unlink(path_subshell, recursive = TRUE, force = TRUE) + }) }) testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", { @@ -64,12 +62,6 @@ testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", { path_subshell <- tempdir() - rix_init( - project_path = path_subshell, - rprofile_action = "overwrite", - message_type = "simple" - ) - rix( r_ver = "4.2.0", overwrite = TRUE, @@ -91,28 +83,28 @@ testthat::test_that("Test `with_nix()` if Nix is installed on R 4.2.0", { testthat::expect_false( inherits(out_subshell, "data.frame") ) + + + on.exit({ + unlink(path_subshell, recursive = TRUE, force = TRUE) + }) + }) testthat::test_that("Test `with_nix()` correct .libPaths()", { skip_on_covr() - + if (isFALSE(is_nix_r_session())) { # needed for the GitHub test runners with system's R set_nix_path() } - + skip_if_not(nix_shell_available()) path_subshell <- tempdir() - rix_init( - project_path = path_subshell, - rprofile_action = "overwrite", - message_type = "simple" - ) - rix( r_ver = "4.3.1", overwrite = TRUE, @@ -132,5 +124,9 @@ testthat::test_that("Test `with_nix()` correct .libPaths()", { testthat::expect_true( is.character(out_subshell) ) + + on.exit({ + unlink(path_subshell, recursive = TRUE, force = TRUE) + }) })