]> git.lizzy.rs Git - rust.git/blobdiff - tests/compile-test.rs
refactor: fix style
[rust.git] / tests / compile-test.rs
index 41048298349e8dbac16044dc479d62f3c2c4c6fa..c10ee969c014620b996dafa32d94367d10db825a 100644 (file)
@@ -23,7 +23,6 @@
 
 /// All crates used in UI tests are listed here
 static TEST_DEPENDENCIES: &[&str] = &[
-    "clap",
     "clippy_lints",
     "clippy_utils",
     "derive_new",
@@ -43,8 +42,6 @@
 // Test dependencies may need an `extern crate` here to ensure that they show up
 // in the depinfo file (otherwise cargo thinks they are unused)
 #[allow(unused_extern_crates)]
-extern crate clap;
-#[allow(unused_extern_crates)]
 extern crate clippy_lints;
 #[allow(unused_extern_crates)]
 extern crate clippy_utils;
         .collect();
     assert!(
         not_found.is_empty(),
-        "dependencies not found in depinfo: {:?}\n\
+        "dependencies not found in depinfo: {not_found:?}\n\
         help: Make sure the `-Z binary-dep-depinfo` rust flag is enabled\n\
         help: Try adding to dev-dependencies in Cargo.toml\n\
         help: Be sure to also add `extern crate ...;` to tests/compile-test.rs",
-        not_found,
     );
     crates
         .into_iter()
-        .map(|(name, path)| format!(" --extern {}={}", name, path))
+        .map(|(name, path)| format!(" --extern {name}={path}"))
         .collect()
 });
 
@@ -153,9 +149,8 @@ fn base_config(test_dir: &str) -> compiletest::Config {
         .map(|p| format!(" -L dependency={}", Path::new(p).join("deps").display()))
         .unwrap_or_default();
     config.target_rustcflags = Some(format!(
-        "--emit=metadata -Dwarnings -Zui-testing -L dependency={}{}{}",
+        "--emit=metadata -Dwarnings -Zui-testing -L dependency={}{host_libs}{}",
         deps_path.display(),
-        host_libs,
         &*EXTERN_FLAGS,
     ));
 
@@ -242,7 +237,7 @@ fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>
         Ok(true) => {},
         Ok(false) => panic!("Some tests failed"),
         Err(e) => {
-            panic!("I/O failure during tests: {:?}", e);
+            panic!("I/O failure during tests: {e:?}");
         },
     }
 }
@@ -288,7 +283,7 @@ fn run_tests(
                 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_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!"),
                 )
@@ -351,7 +346,7 @@ fn run_tests(
         Ok(true) => {},
         Ok(false) => panic!("Some tests failed"),
         Err(e) => {
-            panic!("I/O failure during tests: {:?}", e);
+            panic!("I/O failure during tests: {e:?}");
         },
     }
 }
@@ -396,6 +391,7 @@ fn compile_test() {
     "search_is_some.rs",
     "single_component_path_imports_nested_first.rs",
     "string_add.rs",
+    "suspicious_to_owned.rs",
     "toplevel_ref_arg_non_rustfix.rs",
     "unit_arg.rs",
     "unnecessary_clone.rs",
@@ -406,24 +402,30 @@ fn compile_test() {
 ];
 
 fn check_rustfix_coverage() {
-    let missing_coverage_path = Path::new("target/debug/test/ui/rustfix_missing_coverage.txt");
+    let missing_coverage_path = Path::new("debug/test/ui/rustfix_missing_coverage.txt");
+    let missing_coverage_path = if let Ok(target_dir) = std::env::var("CARGO_TARGET_DIR") {
+        PathBuf::from(target_dir).join(missing_coverage_path)
+    } else {
+        missing_coverage_path.to_path_buf()
+    };
 
     if let Ok(missing_coverage_contents) = std::fs::read_to_string(missing_coverage_path) {
         assert!(RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS.iter().is_sorted_by_key(Path::new));
 
-        for rs_path in missing_coverage_contents.lines() {
-            if Path::new(rs_path).starts_with("tests/ui/crashes") {
+        for rs_file in missing_coverage_contents.lines() {
+            let rs_path = Path::new(rs_file);
+            if rs_path.starts_with("tests/ui/crashes") {
                 continue;
             }
-            let filename = Path::new(rs_path).strip_prefix("tests/ui/").unwrap();
+            assert!(rs_path.starts_with("tests/ui/"), "{rs_file:?}");
+            let filename = rs_path.strip_prefix("tests/ui/").unwrap();
             assert!(
                 RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS
                     .binary_search_by_key(&filename, Path::new)
                     .is_ok(),
-                "`{}` runs `MachineApplicable` diagnostics but is missing a `run-rustfix` annotation. \
+                "`{rs_file}` runs `MachineApplicable` diagnostics but is missing a `run-rustfix` annotation. \
                 Please either add `// run-rustfix` at the top of the file or add the file to \
                 `RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS` in `tests/compile-test.rs`.",
-                rs_path,
             );
         }
     }
@@ -435,7 +437,7 @@ fn rustfix_coverage_known_exceptions_accuracy() {
         let rs_path = Path::new("tests/ui").join(filename);
         assert!(
             rs_path.exists(),
-            "`{}` does not exists",
+            "`{}` does not exist",
             rs_path.strip_prefix(env!("CARGO_MANIFEST_DIR")).unwrap().display()
         );
         let fixed_path = rs_path.with_extension("fixed");
@@ -447,6 +449,43 @@ fn rustfix_coverage_known_exceptions_accuracy() {
     }
 }
 
+#[test]
+fn ui_cargo_toml_metadata() {
+    let ui_cargo_path = Path::new("tests/ui-cargo");
+    let cargo_common_metadata_path = ui_cargo_path.join("cargo_common_metadata");
+    let publish_exceptions =
+        ["fail_publish", "fail_publish_true", "pass_publish_empty"].map(|path| cargo_common_metadata_path.join(path));
+
+    for entry in walkdir::WalkDir::new(ui_cargo_path) {
+        let entry = entry.unwrap();
+        let path = entry.path();
+        if path.file_name() != Some(OsStr::new("Cargo.toml")) {
+            continue;
+        }
+
+        let toml = fs::read_to_string(path).unwrap().parse::<toml::Value>().unwrap();
+
+        let package = toml.as_table().unwrap().get("package").unwrap().as_table().unwrap();
+
+        let name = package.get("name").unwrap().as_str().unwrap().replace('-', "_");
+        assert!(
+            path.parent()
+                .unwrap()
+                .components()
+                .map(|component| component.as_os_str().to_string_lossy().replace('-', "_"))
+                .any(|s| *s == name)
+                || path.starts_with(&cargo_common_metadata_path),
+            "{path:?} has incorrect package name"
+        );
+
+        let publish = package.get("publish").and_then(toml::Value::as_bool).unwrap_or(true);
+        assert!(
+            !publish || publish_exceptions.contains(&path.parent().unwrap().to_path_buf()),
+            "{path:?} lacks `publish = false`"
+        );
+    }
+}
+
 /// Restores an env var on drop
 #[must_use]
 struct VarGuard {