diff --git a/Cargo.lock b/Cargo.lock index a9c95951a82..e3c886d2240 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -455,7 +455,7 @@ version = "0.3.3" [[package]] name = "cargo-test-support" -version = "0.6.0" +version = "0.7.0" dependencies = [ "anstream", "anstyle", diff --git a/Cargo.toml b/Cargo.toml index 7e75ceaebd6..311363cdc6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo- cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" } cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" } cargo-test-macro = { version = "0.3.0", path = "crates/cargo-test-macro" } -cargo-test-support = { version = "0.6.0", path = "crates/cargo-test-support" } +cargo-test-support = { version = "0.7.0", path = "crates/cargo-test-support" } cargo-util = { version = "0.2.14", path = "crates/cargo-util" } cargo-util-schemas = { version = "0.7.0", path = "crates/cargo-util-schemas" } cargo_metadata = "0.18.1" diff --git a/crates/cargo-test-support/Cargo.toml b/crates/cargo-test-support/Cargo.toml index e083b3cee11..c8c5db5011d 100644 --- a/crates/cargo-test-support/Cargo.toml +++ b/crates/cargo-test-support/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-test-support" -version = "0.6.0" +version = "0.7.0" edition.workspace = true rust-version = "1.82" # MSRV:1 license.workspace = true diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index a36e61c3b80..12ff729b164 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -80,9 +80,32 @@ macro_rules! t { } pub use cargo_util::ProcessBuilder; -pub use snapbox::file; pub use snapbox::str; -pub use snapbox::utils::current_dir; + +/// Find the directory for your source file +#[macro_export] +macro_rules! current_dir { + () => {{ + let root = ::std::path::Path::new(::std::env!("CARGO_MANIFEST_DIR")); + let file = ::std::file!(); + let rel_path = ::std::path::Path::new(file).parent().unwrap(); + root.join(rel_path) + }}; +} + +/// Declare an expected value for an assert from a file +/// +/// This is relative to the source file the macro is run from +/// +/// Output type: [`snapbox::Data`] +#[macro_export] +macro_rules! file { + [$path:literal] => {{ + let mut path = $crate::current_dir!(); + path.push($path); + ::snapbox::Data::read_from(&path, None) + }}; +} /// `panic!`, reporting the specified error , see also [`t!`] #[track_caller] diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index 99531bfbd7b..0793c8928ba 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -610,6 +610,7 @@ mod tests { use itertools::Itertools; use snapbox::ToDebug; use std::collections::HashSet; + use std::path::Path; #[test] fn ensure_sorted_lints() { @@ -647,7 +648,7 @@ mod tests { #[test] fn ensure_updated_lints() { - let path = snapbox::utils::current_rs!(); + let path = Path::new(std::env!("CARGO_MANIFEST_DIR")).join(file!()); let expected = std::fs::read_to_string(&path).unwrap(); let expected = expected .lines() @@ -686,7 +687,7 @@ mod tests { #[test] fn ensure_updated_lint_groups() { - let path = snapbox::utils::current_rs!(); + let path = Path::new(std::env!("CARGO_MANIFEST_DIR")).join(file!()); let expected = std::fs::read_to_string(&path).unwrap(); let expected = expected .lines()