diff options
author | Nadrieril | 2020-11-05 23:30:29 +0000 |
---|---|---|
committer | GitHub | 2020-11-05 23:30:29 +0000 |
commit | 2c870c1103b5596eaf936acb793d53b72b025e01 (patch) | |
tree | e25db497aa73bec8cbed1367269dea2282483348 /dhall/tests | |
parent | 5ec5176f2953c19e9741da936fd873603099c258 (diff) | |
parent | 4b93a5e252fe98840a0aaa3adcc453e4214d0b1f (diff) |
Merge pull request #198 from Nadrieril/bless-tests
Overwrite test outputs with a `--bless` option
Diffstat (limited to '')
-rw-r--r-- | dhall/tests/spec.rs | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/dhall/tests/spec.rs b/dhall/tests/spec.rs index 0f49833..6b156f6 100644 --- a/dhall/tests/spec.rs +++ b/dhall/tests/spec.rs @@ -7,6 +7,7 @@ use std::fmt::{Debug, Display}; use std::fs::{create_dir_all, read_to_string, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; use libtest_mimic::{Arguments, Outcome, Test}; use walkdir::WalkDir; @@ -119,9 +120,9 @@ impl TestFile { Ok(self.typecheck()?.normalize()) } - /// If UPDATE_TEST_FILES=1, we overwrite the output files with our own output. + /// If UPDATE_TEST_FILES is `true`, we overwrite the output files with our own output. fn force_update() -> bool { - env::var("UPDATE_TEST_FILES") == Ok("1".to_string()) + UPDATE_TEST_FILES.load(Ordering::Acquire) } /// Write the provided expression to the pointed file. fn write_expr(&self, expr: impl Into<Expr>) -> Result<()> { @@ -326,6 +327,9 @@ fn dhall_files_in_dir<'a>( }) } +// Whether to overwrite the output files when our own output differs. This is set once in `main()`. +static UPDATE_TEST_FILES: AtomicBool = AtomicBool::new(false); + static LOCAL_TEST_PATH: &str = "tests/"; static TEST_PATHS: &[&str] = &["../dhall-lang/tests/", LOCAL_TEST_PATH]; @@ -686,25 +690,31 @@ fn main() { .collect::<String>(); let cache_dir = format!("dhall-tests-{}", random_id); let cache_dir = env::temp_dir().join(cache_dir); - std::fs::create_dir_all(&cache_dir).unwrap(); fs_extra::dir::copy(&dhall_cache_dir, &cache_dir, &Default::default()) .unwrap(); env::set_var("XDG_CACHE_HOME", &cache_dir); - let res = - libtest_mimic::run_tests(&Arguments::from_args(), tests, |test| { - let result = std::panic::catch_unwind(move || { - run_test_stringy_error(&test.data) - }); - match result { - Ok(Ok(_)) => Outcome::Passed, - Ok(Err(e)) => Outcome::Failed { msg: Some(e) }, - Err(_) => Outcome::Failed { - msg: Some("thread panicked".to_string()), - }, - } + // Whether to overwrite the output files when our own output differs. + // Either set `UPDATE_TEST_FILES=1` (deprecated) or pass `--bless` as an argument to this test + // runner. Eg: `cargo test --test spec -- -q --bless`. + let bless = env::args().any(|arg| arg == "--bless") + || env::var("UPDATE_TEST_FILES") == Ok("1".to_string()); + UPDATE_TEST_FILES.store(bless, Ordering::Release); + + let args = Arguments::from_iter(env::args().filter(|arg| arg != "--bless")); + let res = libtest_mimic::run_tests(&args, tests, |test| { + let result = std::panic::catch_unwind(move || { + run_test_stringy_error(&test.data) }); + match result { + Ok(Ok(_)) => Outcome::Passed, + Ok(Err(e)) => Outcome::Failed { msg: Some(e) }, + Err(_) => Outcome::Failed { + msg: Some("thread panicked".to_string()), + }, + } + }); std::fs::remove_dir_all(&cache_dir).unwrap(); |