Skip to content

Commit

Permalink
parse Cargo.toml file in ui-cargo tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hellow554 committed Jun 28, 2022
1 parent 83511d1 commit 6384765
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
20 changes: 19 additions & 1 deletion tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-cargo/multiple_config_files/warn/src/main.stderr
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 6384765

Please sign in to comment.