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.
This commit is contained in:
Marcel Hellwig 2022-06-27 11:11:52 +02:00
parent 83511d1d9a
commit 6384765665
3 changed files with 22 additions and 3 deletions

View file

@ -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"

View file

@ -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() {

View file

@ -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.