Skip to content

Commit fbe2e68

Browse files
committed
Auto merge of #10658 - epage:fix, r=ehuss
test: Make curr_dir work in/out of workspace ### What does this PR try to resolve? Get snapbox tests passing when testing `cargo` as part of the `rust-lang/rust` workspace via a git submodule. - When running tests in the `rust-lang/cargo` repo, `file!` is relative to the crate root and tests are run relative to the crate root and everything is fine. - When running tests in the `rust-lang/rust` repo, `file!` is relative to the workspace root and tests are run relative to the crate root and there is much sadness. If we are compiling relative to the crate root, we could make the path absolute and everything would be dandy but this needs to happen at compile time. Didn't see a way to do this. We could stop using `curr_dir` but that makes the tests a bit noisier with more overhead for creating a new tests from an existing case. Since we can reasonly know what all roots will be used for `file!`, we can just hard code-in support for those two roots. Much happiness ensues as everything works with this surgical hack. ### How should we test and review this PR? I ran the tests in both the `cargo` and `rust` repos. You can as well. ### Additional information
2 parents 526f747 + dde4a1c commit fbe2e68

File tree

1 file changed

+14
-1
lines changed
  • crates/cargo-test-support/src

1 file changed

+14
-1
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@ macro_rules! t {
3535
#[macro_export]
3636
macro_rules! curr_dir {
3737
() => {
38-
std::path::Path::new(file!()).parent().unwrap()
38+
$crate::_curr_dir(std::path::Path::new(file!()));
3939
};
4040
}
4141

42+
#[doc(hidden)]
43+
pub fn _curr_dir(mut file_path: &'static Path) -> &'static Path {
44+
if !file_path.exists() {
45+
// HACK: Must be running in the rust-lang/rust workspace, adjust the paths accordingly.
46+
let prefix = PathBuf::from("src").join("tools").join("cargo");
47+
if let Ok(crate_relative) = file_path.strip_prefix(prefix) {
48+
file_path = crate_relative
49+
}
50+
}
51+
assert!(file_path.exists(), "{} does not exist", file_path.display());
52+
file_path.parent().unwrap()
53+
}
54+
4255
#[track_caller]
4356
pub fn panic_error(what: &str, err: impl Into<anyhow::Error>) -> ! {
4457
let err = err.into();

0 commit comments

Comments
 (0)