]> git.lizzy.rs Git - rust.git/commitdiff
Adapt compile-test to run tests for cargo lints
authorEduardo Broto <ebroto@tutanota.com>
Mon, 18 May 2020 18:54:09 +0000 (20:54 +0200)
committerEduardo Broto <ebroto@tutanota.com>
Thu, 21 May 2020 12:11:11 +0000 (14:11 +0200)
tests/compile-test.rs
tests/ui-cargo/update-all-references.sh [new file with mode: 0755]
tests/ui-cargo/update-references.sh [new file with mode: 0755]

index a3df9d5ccbd161e22dab653a5b2fea127932ebf2..91b9c73c9d47a0e0ab2e45e7940b370590ba3228 100644 (file)
@@ -101,49 +101,124 @@ fn run_mode(cfg: &mut compiletest::Config) {
     compiletest::run_tests(&cfg);
 }
 
-#[allow(clippy::identity_conversion)]
-fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
-    let mut result = true;
-    let opts = compiletest::test_opts(config);
-    for dir in fs::read_dir(&config.src_base)? {
-        let dir = dir?;
-        if !dir.file_type()?.is_dir() {
-            continue;
-        }
-        let dir_path = dir.path();
-        set_var("CARGO_MANIFEST_DIR", &dir_path);
-        for file in fs::read_dir(&dir_path)? {
-            let file = file?;
-            let file_path = file.path();
-            if file.file_type()?.is_dir() {
+fn run_ui_toml(config: &mut compiletest::Config) {
+    fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
+        let mut result = true;
+        let opts = compiletest::test_opts(config);
+        for dir in fs::read_dir(&config.src_base)? {
+            let dir = dir?;
+            if !dir.file_type()?.is_dir() {
                 continue;
             }
-            if file_path.extension() != Some(OsStr::new("rs")) {
-                continue;
+            let dir_path = dir.path();
+            set_var("CARGO_MANIFEST_DIR", &dir_path);
+            for file in fs::read_dir(&dir_path)? {
+                let file = file?;
+                let file_path = file.path();
+                if file.file_type()?.is_dir() {
+                    continue;
+                }
+                if file_path.extension() != Some(OsStr::new("rs")) {
+                    continue;
+                }
+                let paths = compiletest::common::TestPaths {
+                    file: file_path,
+                    base: config.src_base.clone(),
+                    relative_dir: dir_path.file_name().unwrap().into(),
+                };
+                let test_name = compiletest::make_test_name(&config, &paths);
+                let index = tests
+                    .iter()
+                    .position(|test| test.desc.name == test_name)
+                    .expect("The test should be in there");
+                result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
             }
-            let paths = compiletest::common::TestPaths {
-                file: file_path,
-                base: config.src_base.clone(),
-                relative_dir: dir_path.file_name().unwrap().into(),
-            };
-            let test_name = compiletest::make_test_name(&config, &paths);
-            let index = tests
-                .iter()
-                .position(|test| test.desc.name == test_name)
-                .expect("The test should be in there");
-            result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
         }
+        Ok(result)
     }
-    Ok(result)
-}
 
-fn run_ui_toml(config: &mut compiletest::Config) {
     config.mode = TestMode::Ui;
     config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
 
     let tests = compiletest::make_tests(&config);
 
-    let res = run_ui_toml_tests(&config, tests);
+    let res = run_tests(&config, tests);
+    match res {
+        Ok(true) => {},
+        Ok(false) => panic!("Some tests failed"),
+        Err(e) => {
+            println!("I/O failure during tests: {:?}", e);
+        },
+    }
+}
+
+fn run_ui_cargo(config: &mut compiletest::Config) {
+    fn run_tests(
+        config: &compiletest::Config,
+        filter: &Option<String>,
+        mut tests: Vec<tester::TestDescAndFn>,
+    ) -> Result<bool, io::Error> {
+        let mut result = true;
+        let opts = compiletest::test_opts(config);
+
+        for dir in fs::read_dir(&config.src_base)? {
+            let dir = dir?;
+            if !dir.file_type()?.is_dir() {
+                continue;
+            }
+
+            // Use the filter if provided
+            let dir_path = dir.path();
+            match &filter {
+                Some(name) if !dir_path.ends_with(name) => continue,
+                _ => {},
+            }
+
+            for case in &["pass", "fail"] {
+                let tail: PathBuf = [case, "src"].iter().collect();
+                let src_path = dir_path.join(tail);
+                env::set_current_dir(&src_path)?;
+
+                for file in fs::read_dir(&src_path)? {
+                    let file = file?;
+                    if file.file_type()?.is_dir() {
+                        continue;
+                    }
+
+                    // Search for the main file to avoid running a test for each file in the project
+                    let file_path = file.path();
+                    match file_path.file_name().and_then(OsStr::to_str) {
+                        Some("main.rs") => {},
+                        _ => continue,
+                    }
+
+                    let paths = compiletest::common::TestPaths {
+                        file: file_path,
+                        base: config.src_base.clone(),
+                        relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(),
+                    };
+                    let test_name = compiletest::make_test_name(&config, &paths);
+                    let index = tests
+                        .iter()
+                        .position(|test| test.desc.name == test_name)
+                        .expect("The test should be in there");
+                    result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
+                }
+            }
+        }
+        Ok(result)
+    }
+
+    config.mode = TestMode::Ui;
+    config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
+
+    let tests = compiletest::make_tests(&config);
+
+    let current_dir = env::current_dir().unwrap();
+    let filter = env::var("TESTNAME").ok();
+    let res = run_tests(&config, &filter, tests);
+    env::set_current_dir(current_dir).unwrap();
+
     match res {
         Ok(true) => {},
         Ok(false) => panic!("Some tests failed"),
@@ -165,4 +240,5 @@ fn compile_test() {
     let mut config = default_config();
     run_mode(&mut config);
     run_ui_toml(&mut config);
+    run_ui_cargo(&mut config);
 }
diff --git a/tests/ui-cargo/update-all-references.sh b/tests/ui-cargo/update-all-references.sh
new file mode 100755 (executable)
index 0000000..7028b25
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/bash
+#
+# A script to update the references for all tests. The idea is that
+# you do a run, which will generate files in the build directory
+# containing the (normalized) actual output of the compiler. You then
+# run this script, which will copy those files over. If you find
+# yourself manually editing a foo.stderr file, you're doing it wrong.
+#
+# See all `update-references.sh`, if you just want to update a single test.
+
+if [[ "$1" == "--help" || "$1" == "-h" ]]; then
+    echo "usage: $0"
+fi
+
+BUILD_DIR=$PWD/target/debug/test_build_base
+MY_DIR=$(dirname "$0")
+cd "$MY_DIR" || exit
+find . -name '*.rs' -exec ./update-references.sh "$BUILD_DIR" {} +
diff --git a/tests/ui-cargo/update-references.sh b/tests/ui-cargo/update-references.sh
new file mode 100755 (executable)
index 0000000..50d4267
--- /dev/null
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# A script to update the references for particular tests. The idea is
+# that you do a run, which will generate files in the build directory
+# containing the (normalized) actual output of the compiler. This
+# script will then copy that output and replace the "expected output"
+# files. You can then commit the changes.
+#
+# If you find yourself manually editing a foo.stderr file, you're
+# doing it wrong.
+
+if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" == "" ]]; then
+    echo "usage: $0 <build-directory> <relative-path-to-rs-files>"
+    echo ""
+    echo "For example:"
+    echo "   $0 ../../../build/x86_64-apple-darwin/test/ui *.rs */*.rs"
+fi
+
+MYDIR=$(dirname "$0")
+
+BUILD_DIR="$1"
+shift
+
+while [[ "$1" != "" ]]; do
+    STDERR_NAME="${1/%.rs/.stderr}"
+    STDOUT_NAME="${1/%.rs/.stdout}"
+    shift
+    if [[ -f "$BUILD_DIR"/"$STDOUT_NAME" ]] && \
+           ! (cmp -s -- "$BUILD_DIR"/"$STDOUT_NAME" "$MYDIR"/"$STDOUT_NAME"); then
+        echo updating "$MYDIR"/"$STDOUT_NAME"
+        cp "$BUILD_DIR"/"$STDOUT_NAME" "$MYDIR"/"$STDOUT_NAME"
+    fi
+    if [[ -f "$BUILD_DIR"/"$STDERR_NAME" ]] && \
+           ! (cmp -s -- "$BUILD_DIR"/"$STDERR_NAME" "$MYDIR"/"$STDERR_NAME"); then
+        echo updating "$MYDIR"/"$STDERR_NAME"
+        cp "$BUILD_DIR"/"$STDERR_NAME" "$MYDIR"/"$STDERR_NAME"
+    fi
+done