Skip to content

Commit 6384765

Browse files
committed
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.
1 parent 83511d1 commit 6384765

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ termize = "0.1"
3131
compiletest_rs = { version = "0.8", features = ["tmp"] }
3232
tester = "0.9"
3333
regex = "1.5"
34+
toml = "0.5"
3435
# This is used by the `collect-metadata` alias.
3536
filetime = "0.2"
3637

tests/compile-test.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn base_config(test_dir: &str) -> compiletest::Config {
130130
let mut config = compiletest::Config {
131131
edition: Some("2021".into()),
132132
mode: TestMode::Ui,
133-
..compiletest::Config::default()
133+
..Default::default()
134134
};
135135

136136
if let Ok(filters) = env::var("TESTNAME") {
@@ -286,6 +286,24 @@ fn run_ui_cargo() {
286286
}
287287

288288
env::set_current_dir(&src_path)?;
289+
290+
let cargo_toml_path = case.path().join("Cargo.toml");
291+
let cargo_content = fs::read(&cargo_toml_path)?;
292+
let cargo_parsed: toml::Value = toml::from_str(
293+
std::str::from_utf8(&cargo_content).expect("`Cargo.toml` is not a valid utf-8 file!"),
294+
)
295+
.expect("Can't parse `Cargo.toml`");
296+
297+
let _g = VarGuard::set("CARGO_MANIFEST_DIR", case.path());
298+
let _h = VarGuard::set(
299+
"CARGO_PKG_RUST_VERSION",
300+
cargo_parsed
301+
.get("package")
302+
.and_then(|p| p.get("rust-version"))
303+
.and_then(toml::Value::as_str)
304+
.unwrap_or(""),
305+
);
306+
289307
for file in fs::read_dir(&src_path)? {
290308
let file = file?;
291309
if file.file_type()?.is_dir() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Using config file `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/.clippy.toml`
2-
Warning: `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/clippy.toml` will be ignored.
1+
Using config file `$SRC_DIR/.clippy.toml`
2+
Warning: `$SRC_DIR/clippy.toml` will be ignored.

0 commit comments

Comments
 (0)