Skip to content

Commit

Permalink
Actual fix for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Dec 19, 2024
1 parent cfe015c commit 3e19f79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
11 changes: 7 additions & 4 deletions R/ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ construct_file_link <- function(params) {

params$path <- sub("^file://", "", params$path)
params$path <- path.expand(params$path)
if (is_windows() && grepl("^[a-zA-Z]:", params$path)) {
params$path <- paste0("/", params$path)

looks_absolute <- function(path) {
grepl("^/", params$path) || (is_windows() && grepl("^[a-zA-Z]:", params$path))
}
looks_absolute <- grepl("^/", params$path)
if (!looks_absolute) {
if (!looks_absolute(params$path)) {
params$path <- file.path(getwd(), params$path)
}
if (!grepl("^/", params$path)) {
params$path <- paste0("/", params$path)
}

res <- interpolate_parts(fmt, params)
list(url = res)
Expand Down
25 changes: 14 additions & 11 deletions tests/testthat/test-ansi-hyperlink.R
Original file line number Diff line number Diff line change
Expand Up @@ -541,23 +541,26 @@ test_that("construct_file_link() works with custom format and a relative path",
# inspired by test helpers `sanitize_wd()` and `sanitize_home()`, but these
# don't prefix the pattern-to-replace with `file://`
# (and also process the `url` element of `x`)
sanitize_wd2 <- function(x) {
sub(getwd(), "/working/directory", x$url, fixed = TRUE)
}
sanitize_home2 <- function(x) {
sub(path.expand("~"), "/my/home", x$url, fixed = TRUE)
sanitize_dir <- function(x, what = c("wd", "home")) {
what <- match.arg(what)
pattern <- switch(what, wd = getwd(), home = path.expand("~"))
if (is_windows()) {
pattern <- paste0("/", pattern)
}
replacement <- switch(what, wd = "/working/directory", home = "/my/home")
sub(pattern, replacement, x$url, fixed = TRUE)
}

expect_equal(
sanitize_wd2(construct_file_link(list(path = "relative/path"))),
sanitize_dir(construct_file_link(list(path = "relative/path")), what = "wd"),
"positron://file/working/directory/relative/path"
)
expect_equal(
sanitize_wd2(construct_file_link(list(path = "relative/path:12"))),
sanitize_dir(construct_file_link(list(path = "relative/path:12")), what = "wd"),
"positron://file/working/directory/relative/path:12"
)
expect_equal(
sanitize_wd2(construct_file_link(list(path = "relative/path:12:5"))),
sanitize_dir(construct_file_link(list(path = "relative/path:12:5")), what = "wd"),
"positron://file/working/directory/relative/path:12:5"
)

Expand All @@ -570,15 +573,15 @@ test_that("construct_file_link() works with custom format and a relative path",
# line and column

expect_equal(
sanitize_home2(construct_file_link(list(path = "~/relative/path"))),
sanitize_dir(construct_file_link(list(path = "~/relative/path")), what = "home"),
"positron://file/my/home/relative/path"
)
expect_equal(
sanitize_home2(construct_file_link(list(path = "~/relative/path:17"))),
sanitize_dir(construct_file_link(list(path = "~/relative/path:17")), what = "home"),
"positron://file/my/home/relative/path:17"
)
expect_equal(
sanitize_home2(construct_file_link(list(path = "~/relative/path:17:22"))),
sanitize_dir(construct_file_link(list(path = "~/relative/path:17:22")), what = "home"),
"positron://file/my/home/relative/path:17:22"
)
})
Expand Down

0 comments on commit 3e19f79

Please sign in to comment.