diff options
Diffstat (limited to 'crates/expect/src')
-rw-r--r-- | crates/expect/src/lib.rs | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs index a5e26fade..21a458d47 100644 --- a/crates/expect/src/lib.rs +++ b/crates/expect/src/lib.rs | |||
@@ -42,7 +42,9 @@ 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] |
44 | macro_rules! expect_file { | 44 | macro_rules! expect_file { |
45 | [$path:literal] => {$crate::ExpectFile { path: $path }}; | 45 | [$path:expr] => {$crate::ExpectFile { |
46 | path: std::path::PathBuf::from($path) | ||
47 | }}; | ||
46 | } | 48 | } |
47 | 49 | ||
48 | #[derive(Debug)] | 50 | #[derive(Debug)] |
@@ -53,7 +55,7 @@ pub struct Expect { | |||
53 | 55 | ||
54 | #[derive(Debug)] | 56 | #[derive(Debug)] |
55 | pub struct ExpectFile { | 57 | pub struct ExpectFile { |
56 | pub path: &'static str, | 58 | pub path: PathBuf, |
57 | } | 59 | } |
58 | 60 | ||
59 | #[derive(Debug)] | 61 | #[derive(Debug)] |
@@ -119,6 +121,10 @@ impl ExpectFile { | |||
119 | } | 121 | } |
120 | Runtime::fail_file(self, &expected, actual); | 122 | Runtime::fail_file(self, &expected, actual); |
121 | } | 123 | } |
124 | pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) { | ||
125 | let actual = format!("{:#?}\n", actual); | ||
126 | self.assert_eq(&actual) | ||
127 | } | ||
122 | fn read(&self) -> String { | 128 | fn read(&self) -> String { |
123 | fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n") | 129 | fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n") |
124 | } | 130 | } |
@@ -126,7 +132,7 @@ impl ExpectFile { | |||
126 | fs::write(self.abs_path(), contents).unwrap() | 132 | fs::write(self.abs_path(), contents).unwrap() |
127 | } | 133 | } |
128 | fn abs_path(&self) -> PathBuf { | 134 | fn abs_path(&self) -> PathBuf { |
129 | workspace_root().join(self.path) | 135 | WORKSPACE_ROOT.join(&self.path) |
130 | } | 136 | } |
131 | } | 137 | } |
132 | 138 | ||
@@ -154,11 +160,11 @@ impl Runtime { | |||
154 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { | 160 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { |
155 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | 161 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
156 | if update_expect() { | 162 | if update_expect() { |
157 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path); | 163 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path.display()); |
158 | expect.write(actual); | 164 | expect.write(actual); |
159 | return; | 165 | return; |
160 | } | 166 | } |
161 | rt.panic(expect.path.to_string(), expected, actual); | 167 | rt.panic(expect.path.display().to_string(), expected, actual); |
162 | } | 168 | } |
163 | 169 | ||
164 | fn panic(&mut self, position: String, expected: &str, actual: &str) { | 170 | fn panic(&mut self, position: String, expected: &str, actual: &str) { |
@@ -202,7 +208,7 @@ struct FileRuntime { | |||
202 | 208 | ||
203 | impl FileRuntime { | 209 | impl FileRuntime { |
204 | fn new(expect: &Expect) -> FileRuntime { | 210 | fn new(expect: &Expect) -> FileRuntime { |
205 | let path = workspace_root().join(expect.position.file); | 211 | let path = WORKSPACE_ROOT.join(expect.position.file); |
206 | let original_text = fs::read_to_string(&path).unwrap(); | 212 | let original_text = fs::read_to_string(&path).unwrap(); |
207 | let patchwork = Patchwork::new(original_text.clone()); | 213 | let patchwork = Patchwork::new(original_text.clone()); |
208 | FileRuntime { path, original_text, patchwork } | 214 | FileRuntime { path, original_text, patchwork } |
@@ -275,7 +281,7 @@ fn format_patch(line_indent: usize, patch: &str) -> String { | |||
275 | } | 281 | } |
276 | let mut final_newline = false; | 282 | let mut final_newline = false; |
277 | for line in lines_with_ends(patch) { | 283 | for line in lines_with_ends(patch) { |
278 | if is_multiline { | 284 | if is_multiline && !line.trim().is_empty() { |
279 | buf.push_str(indent); | 285 | buf.push_str(indent); |
280 | buf.push_str(" "); | 286 | buf.push_str(" "); |
281 | } | 287 | } |
@@ -290,15 +296,17 @@ fn format_patch(line_indent: usize, patch: &str) -> String { | |||
290 | buf | 296 | buf |
291 | } | 297 | } |
292 | 298 | ||
293 | fn workspace_root() -> PathBuf { | 299 | static WORKSPACE_ROOT: Lazy<PathBuf> = Lazy::new(|| { |
294 | Path::new( | 300 | let my_manifest = |
295 | &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), | 301 | env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()); |
296 | ) | 302 | // Heuristic, see https://github.com/rust-lang/cargo/issues/3946 |
297 | .ancestors() | 303 | Path::new(&my_manifest) |
298 | .nth(2) | 304 | .ancestors() |
299 | .unwrap() | 305 | .filter(|it| it.join("Cargo.toml").exists()) |
300 | .to_path_buf() | 306 | .last() |
301 | } | 307 | .unwrap() |
308 | .to_path_buf() | ||
309 | }); | ||
302 | 310 | ||
303 | #[cfg(test)] | 311 | #[cfg(test)] |
304 | mod tests { | 312 | mod tests { |