aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-09 10:01:45 +0100
committerGitHub <[email protected]>2020-07-09 10:01:45 +0100
commitf9d41bd18fa8e51580f223464d200342de7c6d4a (patch)
treee0c8c3e22bcf2df355eedf60801cad3bbc275e15 /crates
parent9eb9b9df9f6adebb938029a86e47c0aa1eb0f678 (diff)
parent2aa27d612ebdcf1eb34fd22fde5f180a48c3a571 (diff)
Merge #5278
5278: expect should be a dev dep r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/expect/src/lib.rs27
-rw-r--r--crates/ra_ide/Cargo.toml2
-rw-r--r--crates/ra_syntax/Cargo.toml1
-rw-r--r--crates/ra_syntax/src/tests.rs36
4 files changed, 26 insertions, 40 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs
index 7579a5027..408448eed 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]
44macro_rules! expect_file { 44macro_rules! expect_file {
45 [$path:literal] => {$crate::ExpectFile { path: $path }}; 45 [$path:literal] => {$crate::ExpectFile {
46 path: $crate::ExpectFilePath::Static($path)
47 }};
46} 48}
47 49
48#[derive(Debug)] 50#[derive(Debug)]
@@ -53,7 +55,13 @@ pub struct Expect {
53 55
54#[derive(Debug)] 56#[derive(Debug)]
55pub struct ExpectFile { 57pub struct ExpectFile {
56 pub path: &'static str, 58 pub path: ExpectFilePath,
59}
60
61#[derive(Debug)]
62pub enum ExpectFilePath {
63 Static(&'static str),
64 Dynamic(PathBuf),
57} 65}
58 66
59#[derive(Debug)] 67#[derive(Debug)]
@@ -112,6 +120,9 @@ impl Expect {
112} 120}
113 121
114impl ExpectFile { 122impl ExpectFile {
123 pub fn new(path: PathBuf) -> ExpectFile {
124 ExpectFile { path: ExpectFilePath::Dynamic(path) }
125 }
115 pub fn assert_eq(&self, actual: &str) { 126 pub fn assert_eq(&self, actual: &str) {
116 let expected = self.read(); 127 let expected = self.read();
117 if actual == expected { 128 if actual == expected {
@@ -125,8 +136,14 @@ impl ExpectFile {
125 fn write(&self, contents: &str) { 136 fn write(&self, contents: &str) {
126 fs::write(self.abs_path(), contents).unwrap() 137 fs::write(self.abs_path(), contents).unwrap()
127 } 138 }
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 }
128 fn abs_path(&self) -> PathBuf { 145 fn abs_path(&self) -> PathBuf {
129 workspace_root().join(self.path) 146 workspace_root().join(self.path())
130 } 147 }
131} 148}
132 149
@@ -154,11 +171,11 @@ impl Runtime {
154 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { 171 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
155 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); 172 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
156 if update_expect() { 173 if update_expect() {
157 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path); 174 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display());
158 expect.write(actual); 175 expect.write(actual);
159 return; 176 return;
160 } 177 }
161 rt.panic(expect.path.to_string(), expected, actual); 178 rt.panic(expect.path().display().to_string(), expected, actual);
162 } 179 }
163 180
164 fn panic(&mut self, position: String, expected: &str, actual: &str) { 181 fn panic(&mut self, position: String, expected: &str, actual: &str) {
diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml
index 8e8892309..5c51828ea 100644
--- a/crates/ra_ide/Cargo.toml
+++ b/crates/ra_ide/Cargo.toml
@@ -28,7 +28,6 @@ ra_cfg = { path = "../ra_cfg" }
28ra_fmt = { path = "../ra_fmt" } 28ra_fmt = { path = "../ra_fmt" }
29ra_prof = { path = "../ra_prof" } 29ra_prof = { path = "../ra_prof" }
30test_utils = { path = "../test_utils" } 30test_utils = { path = "../test_utils" }
31expect = { path = "../expect" }
32ra_assists = { path = "../ra_assists" } 31ra_assists = { path = "../ra_assists" }
33ra_ssr = { path = "../ra_ssr" } 32ra_ssr = { path = "../ra_ssr" }
34 33
@@ -38,3 +37,4 @@ hir = { path = "../ra_hir", package = "ra_hir" }
38 37
39[dev-dependencies] 38[dev-dependencies]
40insta = "0.16.0" 39insta = "0.16.0"
40expect = { path = "../expect" }
diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml
index 49696ce75..cb21b8053 100644
--- a/crates/ra_syntax/Cargo.toml
+++ b/crates/ra_syntax/Cargo.toml
@@ -31,4 +31,5 @@ serde = { version = "1.0.106", features = ["derive"] }
31 31
32[dev-dependencies] 32[dev-dependencies]
33test_utils = { path = "../test_utils" } 33test_utils = { path = "../test_utils" }
34expect = { path = "../expect" }
34walkdir = "2.3.1" 35walkdir = "2.3.1"
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs
index 7b4232497..a5b6e972e 100644
--- a/crates/ra_syntax/src/tests.rs
+++ b/crates/ra_syntax/src/tests.rs
@@ -1,11 +1,10 @@
1use std::{ 1use std::{
2 env,
3 fmt::Write, 2 fmt::Write,
4 fs, 3 fs,
5 path::{Component, Path, PathBuf}, 4 path::{Component, Path, PathBuf},
6}; 5};
7 6
8use test_utils::{assert_eq_text, project_dir}; 7use test_utils::project_dir;
9 8
10use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; 9use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
11 10
@@ -218,15 +217,7 @@ where
218 for (path, input_code) in collect_rust_files(test_data_dir, paths) { 217 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
219 let actual = f(&input_code, &path); 218 let actual = f(&input_code, &path);
220 let path = path.with_extension(outfile_extension); 219 let path = path.with_extension(outfile_extension);
221 if !path.exists() { 220 expect::ExpectFile::new(path).assert_eq(&actual)
222 println!("\nfile: {}", path.display());
223 println!("No .txt file with expected result, creating...\n");
224 println!("{}\n{}", input_code, actual);
225 fs::write(&path, &actual).unwrap();
226 panic!("No expected result");
227 }
228 let expected = read_text(&path);
229 assert_equal_text(&expected, &actual, &path);
230 } 221 }
231} 222}
232 223
@@ -259,29 +250,6 @@ fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
259 acc 250 acc
260} 251}
261 252
262/// Asserts that `expected` and `actual` strings are equal. If they differ only
263/// in trailing or leading whitespace the test won't fail and
264/// the contents of `actual` will be written to the file located at `path`.
265fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
266 if expected == actual {
267 return;
268 }
269 let dir = project_dir();
270 let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path);
271 if expected.trim() == actual.trim() {
272 println!("whitespace difference, rewriting");
273 println!("file: {}\n", pretty_path.display());
274 fs::write(path, actual).unwrap();
275 return;
276 }
277 if env::var("UPDATE_EXPECT").is_ok() {
278 println!("rewriting {}", pretty_path.display());
279 fs::write(path, actual).unwrap();
280 return;
281 }
282 assert_eq_text!(expected, actual, "file: {}", pretty_path.display());
283}
284
285/// Read file and normalize newlines. 253/// Read file and normalize newlines.
286/// 254///
287/// `rustc` seems to always normalize `\r\n` newlines to `\n`: 255/// `rustc` seems to always normalize `\r\n` newlines to `\n`: