diff options
| author | Marcel Hellwig <github@cookiesoft.de> | 2022-06-27 11:11:52 +0200 |
|---|---|---|
| committer | Marcel Hellwig <github@cookiesoft.de> | 2022-06-28 08:03:48 +0200 |
| commit | 63847656655bda9fe3cb822b6f99d0268fabde03 (patch) | |
| tree | f2597fa824b8226273f24b29d44b6ca9c18ddf0b | |
| parent | 83511d1d9a98b9506df710fb5bba3c20f57e8c85 (diff) | |
| download | rust-63847656655bda9fe3cb822b6f99d0268fabde03.tar.gz rust-63847656655bda9fe3cb822b6f99d0268fabde03.zip | |
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.
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | tests/compile-test.rs | 20 | ||||
| -rw-r--r-- | tests/ui-cargo/multiple_config_files/warn/src/main.stderr | 4 |
3 files changed, 22 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml index 42455998fe7..fa4dd123ee9 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 a303d90d953..319256814c3 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 2abb4e3e06e..98697e001f9 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. |
