Skip to content

Commit 55d066a

Browse files
committed
fixed tests
1 parent a6ecb7e commit 55d066a

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let
2-
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/2025-11-10.tar.gz") {};
2+
pkgs = import (fetchTarball "https://github.com/rstats-on-nix/nixpkgs/archive/2025-12-02.tar.gz") {};
33

44
rpkgs = builtins.attrValues {
55
inherit (pkgs.rPackages)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### File generated by `rix::rix_init()` ###
2+
# 1. Currently, system RStudio does not inherit environmental variables
3+
# defined in `$HOME/.zshrc`, `$HOME/.bashrc` and alike. This is workaround to
4+
# make the path of the nix store and hence basic nix commands available
5+
# in an RStudio session
6+
# 2. For nix-R session, remove `R_LIBS_USER`, system's R user library.`.
7+
# This guarantees no user libraries from the system are loaded and only
8+
# R packages in the Nix store are used. This makes Nix-R behave in pure manner
9+
# at run-time.
10+
{
11+
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
12+
is_nix_r <- nzchar(Sys.getenv("NIX_STORE"))
13+
is_code <- Sys.getenv("TERM_PROGRAM") == "vscode"
14+
is_positron <- Sys.getenv("POSITRON") == "1"
15+
if (isFALSE(is_nix_r) && isTRUE(is_rstudio)) {
16+
cat("{rix} detected RStudio R session")
17+
old_path <- Sys.getenv("PATH")
18+
nix_path <- "/nix/var/nix/profiles/default/bin"
19+
has_nix_path <- any(grepl(nix_path, old_path))
20+
if (isFALSE(has_nix_path)) {
21+
Sys.setenv(PATH = paste(old_path, nix_path, sep = ":"))
22+
}
23+
rm(old_path, nix_path)
24+
}
25+
if (isTRUE(is_nix_r)) {
26+
install.packages <- function(...) {
27+
stop("You are currently in an R session running from Nix.\n", "Don't install packages using install.packages(),\nadd them to ", "the default.nix file instead.")
28+
}
29+
update.packages <- function(...) {
30+
stop("You are currently in an R session running from Nix.\n", "Don't update packages using update.packages(),\n", "generate a new default.nix with a more recent version of R. ", "If you need bleeding edge packages, read the", "'Understanding the rPackages set release cycle and using ", "bleeding edge packages' vignette.")
31+
}
32+
remove.packages <- function(...) {
33+
stop("You are currently in an R session running from Nix.\n", "Don't remove packages using `remove.packages()``,\ndelete them ", "from the default.nix file instead.")
34+
}
35+
current_paths <- .libPaths()
36+
userlib_paths <- Sys.getenv("R_LIBS_USER")
37+
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE)
38+
new_paths <- current_paths[-user_dir]
39+
.libPaths(new_paths)
40+
rm(current_paths, userlib_paths, user_dir, new_paths)
41+
}
42+
if (isTRUE(is_code) && interactive() && isFALSE(is_rstudio) && isFALSE(is_positron)) {
43+
vscode_r_init <- file.path(Sys.getenv(if (.Platform$OS.type == "windows")
44+
"USERPROFILE"
45+
else "HOME"), ".vscode-R", "init.R")
46+
if (file.exists(vscode_r_init)) {
47+
source(vscode_r_init)
48+
}
49+
else {
50+
message("No .vscode-R/init.R file found. If you want to use VSCode-R, you need to source it in your .Rprofile or start vscode from within nix-shell")
51+
}
52+
}
53+
rm(is_rstudio, is_nix_r, is_code, is_positron)
54+
}

tests/testthat/test-rix_init.R

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,6 @@ testthat::test_that("Snapshot test of rix_init(), overwrite", {
2828
)
2929
})
3030

31-
32-
testthat::test_that("Snapshot test of rix_init(), create_missing, no file", {
33-
path_env_nix <- tempdir()
34-
35-
save_rix_init_test <- function(path_env_nix) {
36-
rix_init(
37-
project_path = path_env_nix,
38-
rprofile_action = "create_missing",
39-
message_type = "quiet"
40-
)
41-
42-
paste0(path_env_nix, "/.Rprofile")
43-
}
44-
45-
testthat::expect_snapshot_file(
46-
path = save_rix_init_test(path_env_nix),
47-
name = "golden_Rprofile.txt",
48-
)
49-
50-
on.exit(
51-
unlink(path_env_nix, recursive = TRUE, force = TRUE),
52-
add = TRUE
53-
)
54-
})
55-
5631
testthat::test_that("Snapshot test of rix_init(), create_missing, empty file", {
5732
# Empty should be considered as missing
5833
# We're creating an empty .Rprofile by opening a file connection
@@ -77,7 +52,7 @@ testthat::test_that("Snapshot test of rix_init(), create_missing, empty file", {
7752

7853
testthat::expect_snapshot_file(
7954
path = save_rix_init_test(path_env_nix),
80-
name = "golden_Rprofile.txt",
55+
name = "create_missing_Rprofile.txt",
8156
)
8257

8358
on.exit(

0 commit comments

Comments
 (0)