aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/expect/src/lib.rs49
-rw-r--r--crates/ra_syntax/src/tests.rs3
2 files changed, 20 insertions, 32 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs
index 408448eed..c54e99203 100644
--- a/crates/expect/src/lib.rs
+++ b/crates/expect/src/lib.rs
@@ -42,8 +42,8 @@ macro_rules! expect {
42/// expect_file!["/crates/foo/test_data/bar.html"] 42/// expect_file!["/crates/foo/test_data/bar.html"]
43#[macro_export] 43#[macro_export]
44macro_rules! expect_file { 44macro_rules! expect_file {
45 [$path:literal] => {$crate::ExpectFile { 45 [$path:expr] => {$crate::ExpectFile {
46 path: $crate::ExpectFilePath::Static($path) 46 path: std::path::PathBuf::from($path)
47 }}; 47 }};
48} 48}
49 49
@@ -55,13 +55,7 @@ pub struct Expect {
55 55
56#[derive(Debug)] 56#[derive(Debug)]
57pub struct ExpectFile { 57pub struct ExpectFile {
58 pub path: ExpectFilePath, 58 pub path: PathBuf,
59}
60
61#[derive(Debug)]
62pub enum ExpectFilePath {
63 Static(&'static str),
64 Dynamic(PathBuf),
65} 59}
66 60
67#[derive(Debug)] 61#[derive(Debug)]
@@ -120,9 +114,6 @@ impl Expect {
120} 114}
121 115
122impl ExpectFile { 116impl ExpectFile {
123 pub fn new(path: PathBuf) -> ExpectFile {
124 ExpectFile { path: ExpectFilePath::Dynamic(path) }
125 }
126 pub fn assert_eq(&self, actual: &str) { 117 pub fn assert_eq(&self, actual: &str) {
127 let expected = self.read(); 118 let expected = self.read();
128 if actual == expected { 119 if actual == expected {
@@ -136,14 +127,8 @@ impl ExpectFile {
136 fn write(&self, contents: &str) { 127 fn write(&self, contents: &str) {
137 fs::write(self.abs_path(), contents).unwrap() 128 fs::write(self.abs_path(), contents).unwrap()
138 } 129 }
139 fn path(&self) -> &Path {
140 match &self.path {
141 ExpectFilePath::Static(it) => it.as_ref(),
142 ExpectFilePath::Dynamic(it) => it.as_path(),
143 }
144 }
145 fn abs_path(&self) -> PathBuf { 130 fn abs_path(&self) -> PathBuf {
146 workspace_root().join(self.path()) 131 WORKSPACE_ROOT.join(&self.path)
147 } 132 }
148} 133}
149 134
@@ -171,11 +156,11 @@ impl Runtime {
171 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { 156 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
172 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); 157 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
173 if update_expect() { 158 if update_expect() {
174 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display()); 159 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path.display());
175 expect.write(actual); 160 expect.write(actual);
176 return; 161 return;
177 } 162 }
178 rt.panic(expect.path().display().to_string(), expected, actual); 163 rt.panic(expect.path.display().to_string(), expected, actual);
179 } 164 }
180 165
181 fn panic(&mut self, position: String, expected: &str, actual: &str) { 166 fn panic(&mut self, position: String, expected: &str, actual: &str) {
@@ -219,7 +204,7 @@ struct FileRuntime {
219 204
220impl FileRuntime { 205impl FileRuntime {
221 fn new(expect: &Expect) -> FileRuntime { 206 fn new(expect: &Expect) -> FileRuntime {
222 let path = workspace_root().join(expect.position.file); 207 let path = WORKSPACE_ROOT.join(expect.position.file);
223 let original_text = fs::read_to_string(&path).unwrap(); 208 let original_text = fs::read_to_string(&path).unwrap();
224 let patchwork = Patchwork::new(original_text.clone()); 209 let patchwork = Patchwork::new(original_text.clone());
225 FileRuntime { path, original_text, patchwork } 210 FileRuntime { path, original_text, patchwork }
@@ -307,15 +292,17 @@ fn format_patch(line_indent: usize, patch: &str) -> String {
307 buf 292 buf
308} 293}
309 294
310fn workspace_root() -> PathBuf { 295static WORKSPACE_ROOT: Lazy<PathBuf> = Lazy::new(|| {
311 Path::new( 296 let my_manifest =
312 &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), 297 env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned());
313 ) 298 // Heuristic, see https://github.com/rust-lang/cargo/issues/3946
314 .ancestors() 299 Path::new(&my_manifest)
315 .nth(2) 300 .ancestors()
316 .unwrap() 301 .filter(|it| it.join("Cargo.toml").exists())
317 .to_path_buf() 302 .last()
318} 303 .unwrap()
304 .to_path_buf()
305});
319 306
320#[cfg(test)] 307#[cfg(test)]
321mod tests { 308mod tests {
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs
index a5b6e972e..aa78735da 100644
--- a/crates/ra_syntax/src/tests.rs
+++ b/crates/ra_syntax/src/tests.rs
@@ -4,6 +4,7 @@ use std::{
4 path::{Component, Path, PathBuf}, 4 path::{Component, Path, PathBuf},
5}; 5};
6 6
7use expect::expect_file;
7use test_utils::project_dir; 8use test_utils::project_dir;
8 9
9use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; 10use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
@@ -217,7 +218,7 @@ where
217 for (path, input_code) in collect_rust_files(test_data_dir, paths) { 218 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
218 let actual = f(&input_code, &path); 219 let actual = f(&input_code, &path);
219 let path = path.with_extension(outfile_extension); 220 let path = path.with_extension(outfile_extension);
220 expect::ExpectFile::new(path).assert_eq(&actual) 221 expect_file![path].assert_eq(&actual)
221 } 222 }
222} 223}
223 224