diff options
Diffstat (limited to 'vendor/compiletest_rs/tests')
-rw-r--r-- | vendor/compiletest_rs/tests/bless.rs | 85 | ||||
-rw-r--r-- | vendor/compiletest_rs/tests/test_support/mod.rs | 105 |
2 files changed, 190 insertions, 0 deletions
diff --git a/vendor/compiletest_rs/tests/bless.rs b/vendor/compiletest_rs/tests/bless.rs new file mode 100644 index 000000000..9ee26f4a6 --- /dev/null +++ b/vendor/compiletest_rs/tests/bless.rs @@ -0,0 +1,85 @@ +//! Tests for the `bless` option + +extern crate compiletest_rs as compiletest; + +mod test_support; +use test_support::{testsuite, TestsuiteBuilder, GLOBAL_ROOT}; +use compiletest::Config; + +fn setup(mode: &str) -> (Config, TestsuiteBuilder) { + let builder = testsuite(mode); + let mut config = Config::default(); + let cfg_mode = mode.parse().expect("Invalid mode"); + config.mode = cfg_mode; + config.src_base = builder.root.clone(); + config.build_base = GLOBAL_ROOT.join("build_base"); + + (config, builder) +} + +#[test] +fn test_bless_new_file() { + let (mut config, builder) = setup("ui"); + config.bless = true; + + builder.mk_file( + "foobar.rs", + r#" + #[warn(unused_variables)] + fn main() { + let abc = "foobar"; + } + "#, + ); + compiletest::run_tests(&config); + + // Blessing should cause the stderr to be created directly + assert!(builder.file_contents("foobar.stderr").contains("unused variable")); + + // And a second run of the tests, with blessing disabled should work just fine + config.bless = false; + compiletest::run_tests(&config); +} + +#[test] +fn test_bless_update_file() { + let (mut config, builder) = setup("ui"); + config.bless = true; + + builder.mk_file( + "foobar2.rs", + r#" + #[warn(unused_variables)] + fn main() { + let abc = "foobar_update"; + } + "#, + ); + builder.mk_file( + "foobar2.stderr", + r#" + warning: unused variable: `abc` + --> $DIR/foobar2.rs:4:27 + | + 4 | let abc = "foobar"; + | ^^^ help: if this is intentional, prefix it with an underscore: `_abc` + | + note: the lint level is defined here + --> $DIR/foobar2.rs:2:26 + | + 2 | #[warn(unused_variables)] + | ^^^^^^^^^^^^^^^^ + + warning: 1 warning emitted + "#, + ); + compiletest::run_tests(&config); + + // Blessing should cause the stderr to be created directly + assert!(builder.file_contents("foobar2.stderr").contains("unused variable")); + assert!(builder.file_contents("foobar2.stderr").contains("foobar_update")); + + // And a second run of the tests, with blessing disabled should work just fine + config.bless = false; + compiletest::run_tests(&config); +} diff --git a/vendor/compiletest_rs/tests/test_support/mod.rs b/vendor/compiletest_rs/tests/test_support/mod.rs new file mode 100644 index 000000000..c6962af86 --- /dev/null +++ b/vendor/compiletest_rs/tests/test_support/mod.rs @@ -0,0 +1,105 @@ +//! Provides a simple way to set up compiletest sample testsuites used in testing. +//! +//! Inspired by cargo's `cargo-test-support` crate: +//! https://github.com/rust-lang/cargo/tree/master/crates/cargo-test-support +use std::env; +use std::fs; +use std::path::{Path, PathBuf}; +use std::cell::RefCell; +use std::sync::atomic::{AtomicUsize, Ordering}; + + +static COMPILETEST_INTEGRATION_TEST_DIR: &str = "cit"; + +thread_local! { + static TEST_ID: RefCell<Option<usize>> = RefCell::new(None); +} + +lazy_static::lazy_static! { + pub static ref GLOBAL_ROOT: PathBuf = { + let mut path = env::current_exe().unwrap(); + path.pop(); // chop off exe name + path.pop(); // chop off 'deps' part + path.pop(); // chop off 'debug' + + path.push(COMPILETEST_INTEGRATION_TEST_DIR); + path.mkdir_p(); + path + }; +} + +pub fn testsuite(mode: &str) -> TestsuiteBuilder { + let builder = TestsuiteBuilder::new(mode); + builder.build(); + builder +} + +pub struct TestsuiteBuilder { + pub root: PathBuf, +} + +impl TestsuiteBuilder { + pub fn new(mode: &str) -> Self { + static NEXT_ID: AtomicUsize = AtomicUsize::new(0); + + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + TEST_ID.with(|n| *n.borrow_mut() = Some(id)); + let root = GLOBAL_ROOT.join(format!("id{}", TEST_ID.with(|n|n.borrow().unwrap()))).join(mode); + root.mkdir_p(); + + Self { + root, + } + } + + + /// Creates a new file to be used for the integration test + pub fn mk_file(&self, path: &str, body: &str) { + self.root.mkdir_p(); + fs::write(self.root.join(&path), &body) + .unwrap_or_else(|e| panic!("could not create file {}: {}", path, e)); + } + + /// Returns the contents of the file + pub fn file_contents(&self, name: &str) -> String { + fs::read_to_string(self.root.join(name)).expect("Unable to read file") + } + + // Sets up a new testsuite root directory + fn build(&self) { + // Cleanup before we run the next test + self.rm_root(); + + // Create the new directory + self.root.mkdir_p(); + } + + /// Deletes the root directory and all its contents + fn rm_root(&self) { + self.root.rm_rf(); + } +} + +pub trait PathExt { + fn rm_rf(&self); + fn mkdir_p(&self); +} + +impl PathExt for Path { + fn rm_rf(&self) { + if self.is_dir() { + if let Err(e) = fs::remove_dir_all(self) { + panic!("failed to remove {:?}: {:?}", self, e) + } + } else { + if let Err(e) = fs::remove_file(self) { + panic!("failed to remove {:?}: {:?}", self, e) + } + } + } + + fn mkdir_p(&self) { + fs::create_dir_all(self) + .unwrap_or_else(|e| panic!("failed to mkdir_p {}: {}", self.display(), e)) + } +} |