diff --git a/.travis.yml b/.travis.yml index 1af6399823a..699891faf3a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,9 +20,10 @@ install: | script: - cargo build -v - cargo test -v +- cargo test test_tooltip_std -- --ignored + env: global: - - RUSTFLAGS=--cfg=enable_tooltip_tests - RUST_BACKTRACE=1 - RLS_TEST_WAIT_FOR_AGES=1 - CARGO_INCREMENTAL=0 diff --git a/Cargo.lock b/Cargo.lock index 8403ac15b95..19618a80159 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -390,6 +390,11 @@ name = "diff" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "either" version = "1.5.0" @@ -1136,6 +1141,7 @@ dependencies = [ "cargo_metadata 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "clippy_lints 0.0.212 (git+https://github.com/rust-lang/rust-clippy?rev=a3c77f6ad1c1c185e561e9cd7fdec7db569169d1)", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1818,6 +1824,7 @@ dependencies = [ "checksum derive-new 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca414e896ae072546f4d789f452daaecf60ddee4c9df5dc6d5936d769e3d87c" "checksum derive_more 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f57d78cf3bd45270dad4e70c21ec77a960b36c7a841ff9db76aaa775a8fb871" "checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a" +"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f56c93cc076508c549d9bb747f79aa9b4eb098be7b8cad8830c3137ef52d1e00" "checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38" diff --git a/Cargo.toml b/Cargo.toml index f603dca53b5..81344c9db99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,9 @@ toml = "0.4" # for more information. rustc-workspace-hack = "1.0.0" +[dev-dependencies] +difference = "2" + [build-dependencies] rustc_tools_util = { git = "https://github.com/rust-lang/rust-clippy", rev = "a3c77f6ad1c1c185e561e9cd7fdec7db569169d1" } diff --git a/src/actions/hover.rs b/src/actions/hover.rs index 24d9504e34f..31c172a8f3f 100644 --- a/src/actions/hover.rs +++ b/src/actions/hover.rs @@ -987,6 +987,7 @@ pub mod test { use std::path::PathBuf; use std::process; use std::sync::{Arc, Mutex}; + use std::fmt; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct Test { @@ -1102,7 +1103,7 @@ pub mod test { } } - #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] + #[derive(PartialEq, Eq)] pub struct TestFailure { /// The test case, indicating file, line, and column pub test: Test, @@ -1119,6 +1120,23 @@ pub mod test { pub actual_data: Result, String>, ()>, } + impl fmt::Debug for TestFailure { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("TestFailure") + .field("test", &self.test) + .field("expect_file", &self.expect_file) + .field("actual_file", &self.actual_file) + .field("expect_data", &self.expect_data) + .field("actual_data", &self.actual_data) + .finish()?; + + let expected = format!("{:#?}", self.expect_data); + let actual = format!("{:#?}", self.actual_data); + write!(fmt, "-diff: {}", difference::Changeset::new(&expected, &actual, "")) + } + } + + #[derive(Clone, Default)] pub struct LineOutput { req_id: Arc>, @@ -1289,6 +1307,10 @@ pub mod test { impl Drop for TooltipTestHarness { fn drop(&mut self) { + if let Ok(mut jobs) = self.ctx.jobs.lock() { + jobs.wait_for_all(); + } + if fs::metadata(&self.working_dir).is_ok() { fs::remove_dir_all(&self.working_dir).expect("failed to tidy up"); } @@ -2030,9 +2052,8 @@ pub mod test { } #[test] - // doesn't work in the rust-lang/rust repo, enable on CI - #[cfg_attr(not(enable_tooltip_tests), ignore)] fn test_tooltip() -> Result<(), Box> { + let _ = env_logger::try_init(); use self::test::{LineOutput, Test, TooltipTestHarness}; use std::env; @@ -2088,8 +2109,48 @@ pub mod test { Test::new("test_tooltip_mod_use_external.rs", 11, 7), Test::new("test_tooltip_mod_use_external.rs", 12, 7), Test::new("test_tooltip_mod_use_external.rs", 12, 12), + ]; + + let cwd = env::current_dir()?; + let out = LineOutput::default(); + let proj_dir = cwd.join("test_data").join("hover"); + let save_dir = cwd + .join("target") + .join("tests") + .join("hover") + .join("save_data"); + let load_dir = proj_dir.join("save_data"); + + let harness = TooltipTestHarness::new(proj_dir, &out); + + out.reset(); + + let failures = harness.run_tests(&tests, load_dir, save_dir)?; + + if failures.is_empty() { + Ok(()) + } else { + eprintln!("{}\n\n", out.reset().join("\n")); + eprintln!("Failures (\x1b[91mexpected\x1b[92mactual\x1b[0m): {:#?}\n\n", failures); + Err(format!("{} of {} tooltip tests failed", failures.len(), tests.len()).into()) + } + } + + /// Note: This test is ignored as it doesn't work in the rust-lang/rust repo. + /// It is enabled on CI. + /// Run with `cargo test test_tooltip_std -- --ignored` + #[test] + #[ignore] + fn test_tooltip_std() -> Result<(), Box> { + let _ = env_logger::try_init(); + use self::test::{LineOutput, Test, TooltipTestHarness}; + use std::env; + + let tests = vec![ + // these test std stuff Test::new("test_tooltip_mod_use_external.rs", 14, 12), Test::new("test_tooltip_mod_use_external.rs", 15, 12), + Test::new("test_tooltip_std.rs", 18, 15), Test::new("test_tooltip_std.rs", 18, 27), Test::new("test_tooltip_std.rs", 19, 7), @@ -2125,7 +2186,7 @@ pub mod test { Ok(()) } else { eprintln!("{}\n\n", out.reset().join("\n")); - eprintln!("{:#?}\n\n", failures); + eprintln!("Failures (\x1b[91mexpected\x1b[92mactual\x1b[0m): {:#?}\n\n", failures); Err(format!("{} of {} tooltip tests failed", failures.len(), tests.len()).into()) } }