Skip to content

Commit

Permalink
[fixtures] loosen test_cwd check
Browse files Browse the repository at this point in the history
We're going to use a seed archive soon, which makes the current check not work
anyway. Just do a couple of basic heuristic checks.
  • Loading branch information
sunshowers committed Dec 9, 2024
1 parent 9990bd8 commit dd20c0f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
3 changes: 1 addition & 2 deletions fixture-data/src/nextest_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ pub static EXPECTED_TEST_SUITES: Lazy<BTreeMap<RustBinaryId, TestSuiteFixture>>
vec![
TestCaseFixture::new("test_cargo_env_vars", TestCaseFixtureStatus::Pass)
.with_property(TestCaseFixtureProperty::NotInDefaultSetUnix),
TestCaseFixture::new("test_cwd", TestCaseFixtureStatus::Pass)
.with_property(TestCaseFixtureProperty::NeedsSameCwd),
TestCaseFixture::new("test_cwd", TestCaseFixtureStatus::Pass),
TestCaseFixture::new("test_execute_bin", TestCaseFixtureStatus::Pass),
TestCaseFixture::new("test_failure_assert", TestCaseFixtureStatus::Fail),
TestCaseFixture::new("test_failure_error", TestCaseFixtureStatus::Fail),
Expand Down
41 changes: 29 additions & 12 deletions fixtures/nextest-tests/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright (c) The nextest Contributors
use std::{
env,
io::Read,
path::{Path, PathBuf},
};
use std::{env, io::Read, path::PathBuf};

#[test]
fn test_success() {
Expand Down Expand Up @@ -66,10 +62,33 @@ fn test_failure_should_panic() {}

#[test]
fn test_cwd() {
// Ensure that the cwd is correct.
// Ensure that the cwd is correct. It's a bit tricky to do this in the face
// of a relative path, but just ensure that the cwd looks like what it
// should be (has a `Cargo.toml` with `name = "nextest-tests"` within it).
let runtime_cwd = env::current_dir().expect("should be able to read current dir");
let compile_time_cwd = Path::new(env!("CARGO_MANIFEST_DIR"));
assert_eq!(runtime_cwd, compile_time_cwd, "current dir matches");
let cargo_toml_path = runtime_cwd.join("Cargo.toml");
let cargo_toml =
std::fs::read_to_string(runtime_cwd.join("Cargo.toml")).unwrap_or_else(|error| {
panic!(
"should be able to read Cargo.toml: {}",
cargo_toml_path.display()
)
});
assert!(
cargo_toml.contains("name = \"nextest-tests\""),
"{} contains name = \"nextest-tests\"",
cargo_toml_path.display()
);

// Also ensure that the runtime cwd and the runtime CARGO_MANIFEST_DIR are
// the same.
let runtime_cargo_manifest_dir =
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should be set");
assert_eq!(
runtime_cwd,
PathBuf::from(runtime_cargo_manifest_dir),
"runtime cwd and CARGO_MANIFEST_DIR are the same"
);
}

#[test]
Expand Down Expand Up @@ -149,10 +168,8 @@ fn test_cargo_env_vars() {

// Note: we do not test CARGO here because nextest does not set it -- it's set by Cargo when
// invoked as `cargo nextest`.
assert_env!(
"CARGO_MANIFEST_DIR",
"__NEXTEST_ORIGINAL_CARGO_MANIFEST_DIR"
);
// Also, CARGO_MANIFEST_DIR is tested separately by test_cwd.

assert_env!("CARGO_PKG_VERSION");
assert_env!("CARGO_PKG_VERSION_MAJOR");
assert_env!("CARGO_PKG_VERSION_MINOR");
Expand Down
84 changes: 39 additions & 45 deletions nextest-runner/src/test_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,53 +149,47 @@ where
}

fn apply_package_env(cmd: &mut std::process::Command, package: &PackageMetadata<'_>) {
cmd.env(
"__NEXTEST_ORIGINAL_CARGO_MANIFEST_DIR",
// This is a test-only environment variable set to the *old* cwd. Not part of the
// public API.
package.manifest_path().parent().unwrap(),
)
// These environment variables are set at runtime by cargo test:
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
.env("CARGO_PKG_VERSION", format!("{}", package.version()))
.env(
"CARGO_PKG_VERSION_MAJOR",
format!("{}", package.version().major),
)
.env(
"CARGO_PKG_VERSION_MINOR",
format!("{}", package.version().minor),
)
.env(
"CARGO_PKG_VERSION_PATCH",
format!("{}", package.version().patch),
)
.env(
"CARGO_PKG_VERSION_PRE",
format!("{}", package.version().pre),
)
.env("CARGO_PKG_AUTHORS", package.authors().join(":"))
.env("CARGO_PKG_NAME", package.name())
.env(
"CARGO_PKG_DESCRIPTION",
package.description().unwrap_or_default(),
)
.env("CARGO_PKG_HOMEPAGE", package.homepage().unwrap_or_default())
.env("CARGO_PKG_LICENSE", package.license().unwrap_or_default())
.env(
"CARGO_PKG_LICENSE_FILE",
package.license_file().unwrap_or_else(|| "".as_ref()),
)
.env(
"CARGO_PKG_REPOSITORY",
package.repository().unwrap_or_default(),
)
.env(
"CARGO_PKG_RUST_VERSION",
package
.minimum_rust_version()
.map_or(String::new(), |v| v.to_string()),
);
cmd.env("CARGO_PKG_VERSION", format!("{}", package.version()))
.env(
"CARGO_PKG_VERSION_MAJOR",
format!("{}", package.version().major),
)
.env(
"CARGO_PKG_VERSION_MINOR",
format!("{}", package.version().minor),
)
.env(
"CARGO_PKG_VERSION_PATCH",
format!("{}", package.version().patch),
)
.env(
"CARGO_PKG_VERSION_PRE",
format!("{}", package.version().pre),
)
.env("CARGO_PKG_AUTHORS", package.authors().join(":"))
.env("CARGO_PKG_NAME", package.name())
.env(
"CARGO_PKG_DESCRIPTION",
package.description().unwrap_or_default(),
)
.env("CARGO_PKG_HOMEPAGE", package.homepage().unwrap_or_default())
.env("CARGO_PKG_LICENSE", package.license().unwrap_or_default())
.env(
"CARGO_PKG_LICENSE_FILE",
package.license_file().unwrap_or_else(|| "".as_ref()),
)
.env(
"CARGO_PKG_REPOSITORY",
package.repository().unwrap_or_default(),
)
.env(
"CARGO_PKG_RUST_VERSION",
package
.minimum_rust_version()
.map_or(String::new(), |v| v.to_string()),
);
}

/// Applies environment variables spcified by the build script via `cargo::rustc-env`
Expand Down

0 comments on commit dd20c0f

Please sign in to comment.