Skip to content

Commit

Permalink
check for existence of .Rprofile
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Jul 1, 2024
1 parent 93a7fb9 commit 2b62be4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 73 deletions.
31 changes: 22 additions & 9 deletions R/rix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 == ".") {
Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/test-nix_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
})
})
109 changes: 71 additions & 38 deletions tests/testthat/test-rix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]", "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", "[email protected]", "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")
Expand Down Expand Up @@ -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")
Expand All @@ -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)
})
})


Expand All @@ -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)
})
})


Expand All @@ -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", {
Expand All @@ -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", {
Expand All @@ -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", {
Expand All @@ -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", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-rix_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
36 changes: 16 additions & 20 deletions tests/testthat/test-with_nix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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", {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
})
})

0 comments on commit 2b62be4

Please sign in to comment.