Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions crates/ark/src/modules/positron/hooks_source.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,47 @@ make_ark_source <- function(original_source) {
# fallback calls.
use_file <- missing(exprs)

# Capture environment early if local evaluation is requested.
# This is necessary if we have to fallback when `local = TRUE`.
if (isTRUE(local)) {
local <- parent.frame()
}

args <- alist(
file = file,
local = local,
echo = echo,
print.eval = print.eval,
exprs = exprs,
spaced = spaced,
verbose = verbose,
prompt.echo = prompt.echo,
max.deparse.length = max.deparse.length,
width.cutoff = width.cutoff,
deparseCtrl = deparseCtrl,
chdir = chdir,
catch.aborts = catch.aborts,
encoding = encoding,
continue.echo = continue.echo,
skip.echo = skip.echo,
keep.source = keep.source,
...
)

# Remove arguments that are not yet supported
if (getRversion() <= "4.4.0") {
args$catch.aborts <- NULL
}

# DRY: Promise for calling `original_source` with all arguments.
# Evaluated lazily only when needed for fallback paths.
delayedAssign(
"fall_back",
original_source(
file = file,
local = local,
echo = echo,
print.eval = print.eval,
exprs = exprs,
spaced = spaced,
verbose = verbose,
prompt.echo = prompt.echo,
max.deparse.length = max.deparse.length,
width.cutoff = width.cutoff,
deparseCtrl = deparseCtrl,
chdir = chdir,
catch.aborts = catch.aborts,
encoding = encoding,
continue.echo = continue.echo,
skip.echo = skip.echo,
keep.source = keep.source,
...
)
)
eval(bquote(
delayedAssign(
"fall_back",
original_source(..(args))
),
splice = TRUE
))

# Fall back if hook is disabled
if (!isTRUE(getOption("ark.source_hook", default = TRUE))) {
Expand All @@ -87,7 +103,9 @@ make_ark_source <- function(original_source) {
}

env <- if (isTRUE(local)) {
parent.frame()
stop(
"Internal error: `local = TRUE` should have been converted to an environment above."
)
} else if (isFALSE(local)) {
.GlobalEnv
} else if (is.environment(local)) {
Expand Down
73 changes: 73 additions & 0 deletions crates/ark/tests/kernel-hooks-source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::io::Write;

use ark::fixtures::DummyArkFrontend;

#[test]
fn test_source_local() {
let frontend = DummyArkFrontend::lock();

let mut file = tempfile::NamedTempFile::new().unwrap();
write!(file, "foobar\n").unwrap();

let path = file.path().to_str().unwrap().replace("\\", "/");

// Breakpoint injection path
let code = format!(
r#"local({{
foobar <- "worked"
source("{path}", local = TRUE)$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked\"");
});

// Fallback path (because we supply `encoding`)
let code = format!(
r#"local({{
foobar <- "worked"
source("{path}", local = TRUE, encoding = "UTF-8")$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked\"");
});
}

#[test]
fn test_source_global() {
let frontend = DummyArkFrontend::lock();

let mut file = tempfile::NamedTempFile::new().unwrap();
write!(file, "foo\n").unwrap();

let path = file.path().to_str().unwrap().replace("\\", "/");

// Breakpoint injection path
frontend.execute_request_invisibly(r#"foo <- "worked!""#);

let code = format!(
r#"local({{
foo <- "did not work!"
source("{path}")$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked!\"");
});

// Fallback path (because we supply `encoding`)
let code = format!(
r#"local({{
foo <- "did not work!"
source("{path}", encoding = "UTF-8")$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked!\"");
});
}
Loading