-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils-io.R
53 lines (45 loc) · 1.75 KB
/
utils-io.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
readLines <- function(...) {
drive_abort("In this house, we use {.fun read_utf8} for UTF-8 reasons.")
}
writeLines <- function(...) {
drive_abort("In this house, we use {.fun write_utf8} for UTF-8 reasons.")
}
# https://github.com/gaborcsardi/rencfaq#with-base-r
read_utf8 <- function(path) {
opts <- options(encoding = "native.enc")
withr::defer(options(opts))
x <- base::readLines(path, encoding = "UTF-8", warn = FALSE)
x
}
# https://github.com/gaborcsardi/rencfaq#with-base-r
write_utf8 <- function(text, path = NULL) {
# sometimes we use writeLines() basically to print something for a snapshot
if (is.null(path)) {
return(base::writeLines(text))
}
# step 1: ensure our text is utf8 encoded
utf8 <- enc2utf8(text)
upath <- enc2utf8(path)
# step 2: create a connection with 'native' encoding
# this signals to R that translation before writing
# to the connection should be skipped
con <- file(upath, open = "w+", encoding = "native.enc")
withr::defer(close(con))
# step 3: write to the connection with 'useBytes = TRUE',
# telling R to skip translation to the native encoding
base::writeLines(utf8, con = con, useBytes = TRUE)
}
# used for building functions that construct Drive file names in tests ----
nm_fun <- function(context, user_run = TRUE) {
user_run <- if (isTRUE(user_run)) nm_user_run() else NULL
y <- purrr::compact(list(context, user_run))
function(x = character()) as.character(glue_collapse(c(x, y), sep = "-"))
}
nm_user_run <- function() {
if (as.logical(Sys.getenv("GITHUB_ACTIONS", unset = "false"))) {
glue("gha-{Sys.getenv('GITHUB_WORKFLOW')}-{Sys.getenv('GITHUB_RUN_ID')}")
} else {
random_id <- strsplit(uuid::UUIDgenerate(TRUE), "-")[[1]][[1]]
glue("{Sys.info()['user']}-{random_id}")
}
}