Skip to content

Commit

Permalink
rix_init fixed - tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Sep 27, 2024
1 parent 2dad381 commit 44845c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
34 changes: 24 additions & 10 deletions R/rix_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,24 @@ rix_init <- function(project_path,
rprofile_quoted <- nix_rprofile()
rprofile_deparsed <- deparse_chr1(expr = rprofile_quoted, collapse = "\n")
rprofile_file <- file.path(project_path, ".Rprofile")
rprofile_con <- file(rprofile_file, open = "wb", encoding = "native.enc")

rprofile_text <- get_rprofile_text(rprofile_deparsed)

on.exit(close(rprofile_con))
write_rprofile <- function(rprofile_text, rprofile_file) {
# This function creates the connection, write the text
# and closes the connexion
# Makes it "as pure as possible"
write_rprofile <- function(rprofile_text, rprofile_file, mode) {
create_rprofile_con <- function(rprofile_file, mode){
rprofile_con <- file(
rprofile_file,
open = mode,
encoding = "native.enc"
)
}

rprofile_con <- create_rprofile_con(rprofile_file, mode)
writeLines(enc2utf8(rprofile_text), rprofile_con, useBytes = TRUE)
on.exit(close(rprofile_con))
}

is_nix_r <- is_nix_r_session()
Expand All @@ -168,7 +180,7 @@ rix_init <- function(project_path,
)
}
} else {
write_rprofile(rprofile_text, rprofile_file = rprofile_con)
write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "wb")
if (isFALSE(is_quiet)) {
message_rprofile(action_string = "Added", project_path = project_path)
}
Expand All @@ -178,7 +190,7 @@ rix_init <- function(project_path,
create_backup = {
if (isTRUE(rprofile_exists)) {
file.copy(from = rprofile_file, to = rprofile_backup)
write_rprofile(rprofile_text, rprofile_file = rprofile_con)
write_rprofile(rprofile_text, rprofile_file = rprofile_file, mode = "wb")
if (isFALSE(is_quiet)) {
cat(
"\n==> Backed up existing `.Rprofile` in file:\n", rprofile_backup,
Expand All @@ -192,13 +204,13 @@ rix_init <- function(project_path,

if (message_type == "verbose") {
cat("\n* Current lines of local `.Rprofile` are\n:")
cat(readLines(con = rprofile_con), sep = "\n")
cat(readLines(con = rprofile_file), sep = "\n")
}
set_message_session_PATH(message_type = message_type)
}
},
overwrite = {
write_rprofile(rprofile_text, rprofile_file = rprofile_con)
write_rprofile(rprofile_text, rprofile_file = rprofile_file, "wb")
if (isTRUE(rprofile_exists)) {
message_rprofile(
action_string = "Overwrote", project_path = project_path
Expand All @@ -210,17 +222,19 @@ rix_init <- function(project_path,
}
},
append = {
cat(paste0(rprofile_text, "\n"), file = rprofile_con, append = TRUE)
#writeLines(paste0(rprofile_text, "\n"), rprofile_con, mode = "a+")
write_rprofile(rprofile_text, rprofile_file = rprofile_file, "a+")
message_rprofile(
action_string = "Appended", project_path = project_path
)
}
)
)

if (message_type == "verbose") {
cat("\n\n* Current lines of local `.Rprofile` are:\n\n")
cat(readLines(con = rprofile_action), sep = "\n")
cat(readLines(con = rprofile_file), sep = "\n")
}

}

#' Get character vector of length two with comment and code write `.Rprofile`
Expand Down
24 changes: 13 additions & 11 deletions tests/testthat/test-rix_init.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
testthat::test_that("Snapshot test of rix_init(), overwrite", {
path_env_nix <- tempdir()

# Create an empty file, and overwrite it
rprofile_file <- paste0(path_env_nix, "/.Rprofile")

save_rix_init_test <- function(path_env_nix) {
rix_init(
project_path = path_env_nix,
Expand All @@ -11,7 +14,8 @@ testthat::test_that("Snapshot test of rix_init(), overwrite", {
paste0(path_env_nix, "/.Rprofile")
}

testthat::announce_snapshot_file("find_rev/golden_Rprofile.txt")
rprofile_con <- file(rprofile_file, open = "wb", encoding = "native.enc")
on.exit(close(rprofile_con), add = TRUE)

testthat::expect_snapshot_file(
path = save_rix_init_test(path_env_nix),
Expand All @@ -38,8 +42,6 @@ testthat::test_that("Snapshot test of rix_init(), create_missing, no file", {
paste0(path_env_nix, "/.Rprofile")
}

testthat::announce_snapshot_file("find_rev/golden_Rprofile.txt")

testthat::expect_snapshot_file(
path = save_rix_init_test(path_env_nix),
name = "golden_Rprofile.txt",
Expand Down Expand Up @@ -101,20 +103,20 @@ testthat::test_that("Snapshot test of rix_init(), append", {
paste0(path_env_nix, "/.Rprofile")
}

#testthat::announce_snapshot_file("find_rev/append_Rprofile.txt")
rprofile_file <- paste0(path_env_nix, "/.Rprofile")
rprofile_con <- file(rprofile_file, open = "a+", encoding = "native.enc")

writeLines(enc2utf8("This is in the original Rprofile"),
rprofile_con,
useBytes = TRUE)

close(rprofile_con)

testthat::expect_snapshot_file(
path = save_rix_init_test(path_env_nix),
name = "append_Rprofile.txt",
)

rprofile_file <- paste0(path_env_nix, "/.Rprofile")
rprofile_con <- file(rprofile_file, open = "wb", encoding = "native.enc")

writeLines(enc2utf8("This is the origin Rprofile\n"),
rprofile_con,
useBytes = TRUE)

on.exit(
unlink(path_env_nix, recursive = TRUE, force = TRUE),
add = TRUE
Expand Down

0 comments on commit 44845c6

Please sign in to comment.