Add import layers for testthat files#1260
Conversation
10b4801 to
a6cc84b
Compare
464855a to
0e4fa01
Compare
| .as_url() | ||
| .path_segments()? | ||
| .next_back() | ||
| .filter(|seg| !seg.is_empty())?; |
There was a problem hiding this comment.
is this because https://example.com/foo/ sees that last / and returns an empty string for the (not really there) segment after the /?
There was a problem hiding this comment.
yep I've added this comment:
// A trailing slash (`.../foo/`) yields a final empty segment,
// meaning the URL points at a directory. Drop it so we return
// `None` rather than `""`.
| // at any offset. Only the file's own top-level `library()` calls | ||
| // narrow. |
There was a problem hiding this comment.
"Only the file's own top-level library() calls"
GROSSSSSSSS, THE HORROR
There was a problem hiding this comment.
Possibly, but it feels to me more like Jarl's territory than Oak's.
| /// order (latest-attached first). `before` narrows to a top-level cursor: with | ||
| /// `Some(offset)`, keep only file-scope calls that have run by `offset`; with | ||
| /// `None`, keep every attach (the end-of-file view, used for lazy contexts). |
There was a problem hiding this comment.
is the cursor itself top level? not always right? like you can be in a test_that({<cursor>}) block and i wouldnt consider that top level?
i would also appreciate if this comment were rewritten as like a bulleted list of the 2 cases. it was very hard for me to mentally parse with the colon and semicolon
There was a problem hiding this comment.
This is now:
/// The file's `library()` / `require()` attaches as `Package` layers, in LIFO
/// order (latest-attached first). `before` selects which calls to include:
///
/// - `None`: every attach. The end-of-file view, used for lazy contexts.
/// - `Some(offset)`: only top-level (file-scope) calls that have run by
/// `offset`. Calls nested in a block (e.g. inside `test_that({})`) are
/// dropped, as are calls after the offset.
| fn in_testthat_dir(path: &Utf8Path) -> bool { | ||
| let Some(parent) = path.parent() else { | ||
| return false; | ||
| }; | ||
| parent.file_name() == Some("testthat") && | ||
| parent.parent().and_then(Utf8Path::file_name) == Some("tests") | ||
| } |
There was a problem hiding this comment.
we should upgrade to 2024 edition rust at some point for let chains so you can do things like this
fn in_testthat_dir(path: &Utf8Path) -> bool {
if let Some(parent) = path.parent() &&
parent.file_name() == Some("testthat") &&
let Some(parent) = parent.parent() &&
parent.file_name() == Some("tests")
{
return true
} else {
return false
}
}| /// which currently sorts based on locale, but arguably this should be fixed on | ||
| /// the testthat side. | ||
| fn testthat_support_key(file: File, db: &dyn Db) -> Cow<'_, str> { | ||
| file.path(db).file_name().unwrap_or_default() |
There was a problem hiding this comment.
What is the default? An empty Owned string i guess? can you just leave it as Option<Cow>? I have no idea if that would sort right
There was a problem hiding this comment.
Default but it's unreachable.
| pkg.set_scripts(&mut db) | ||
| .to(vec![helper, setup, test_foo, test_bar]); |
There was a problem hiding this comment.
i wonder if it will be useful to have a specific field for testthat_scripts or something, so we dont have to tease them apart from regular scripts every time
There was a problem hiding this comment.
Potentially, but I'd rather have some kind of collation annotation to branch on (via a tracked query).
688c41f to
d06068b
Compare
79df227 to
cac4c04
Compare
9252bcc to
934cb90
Compare
17a154b to
2ebe7c4
Compare
Branched from #1259
Whereas scripts in
tests/folders are standalone, scripts intests/testthat/are sourced in an environment where:testthatexports are attachedThis PR detects testthat files and adjusts the import layers accordingly, so that symbols injected by testthat are visible in these files.