From 63847656655bda9fe3cb822b6f99d0268fabde03 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 27 Jun 2022 11:11:52 +0200 Subject: [PATCH] parse `Cargo.toml` file in ui-cargo tests compiletest_rs is not meant to test full cargo projects, but instead only files. So we need to parse the `Cargo.toml` file ourself and set the corresponding environment variable. In this case we just set `CARGO_PKG_RUST_VERSION`, nothing more. But, of course, this can be extended. --- Cargo.toml | 1 + tests/compile-test.rs | 20 ++++++++++++++++++- .../warn/src/main.stderr | 4 ++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42455998fe790..fa4dd123ee9a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ termize = "0.1" compiletest_rs = { version = "0.8", features = ["tmp"] } tester = "0.9" regex = "1.5" +toml = "0.5" # This is used by the `collect-metadata` alias. filetime = "0.2" diff --git a/tests/compile-test.rs b/tests/compile-test.rs index a303d90d953ed..319256814c3a6 100644 --- a/tests/compile-test.rs +++ b/tests/compile-test.rs @@ -130,7 +130,7 @@ fn base_config(test_dir: &str) -> compiletest::Config { let mut config = compiletest::Config { edition: Some("2021".into()), mode: TestMode::Ui, - ..compiletest::Config::default() + ..Default::default() }; if let Ok(filters) = env::var("TESTNAME") { @@ -286,6 +286,24 @@ fn run_ui_cargo() { } env::set_current_dir(&src_path)?; + + let cargo_toml_path = case.path().join("Cargo.toml"); + let cargo_content = fs::read(&cargo_toml_path)?; + let cargo_parsed: toml::Value = toml::from_str( + std::str::from_utf8(&cargo_content).expect("`Cargo.toml` is not a valid utf-8 file!"), + ) + .expect("Can't parse `Cargo.toml`"); + + let _g = VarGuard::set("CARGO_MANIFEST_DIR", case.path()); + let _h = VarGuard::set( + "CARGO_PKG_RUST_VERSION", + cargo_parsed + .get("package") + .and_then(|p| p.get("rust-version")) + .and_then(toml::Value::as_str) + .unwrap_or(""), + ); + for file in fs::read_dir(&src_path)? { let file = file?; if file.file_type()?.is_dir() { diff --git a/tests/ui-cargo/multiple_config_files/warn/src/main.stderr b/tests/ui-cargo/multiple_config_files/warn/src/main.stderr index 2abb4e3e06e64..98697e001f99a 100644 --- a/tests/ui-cargo/multiple_config_files/warn/src/main.stderr +++ b/tests/ui-cargo/multiple_config_files/warn/src/main.stderr @@ -1,2 +1,2 @@ -Using config file `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/.clippy.toml` -Warning: `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/clippy.toml` will be ignored. +Using config file `$SRC_DIR/.clippy.toml` +Warning: `$SRC_DIR/clippy.toml` will be ignored.