aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-01 11:30:17 +0100
committerAleksey Kladov <[email protected]>2020-07-01 11:30:17 +0100
commit7b0113b3d588fdc1f95eca1286fb2f6881abe65a (patch)
tree9304d504567e48cee870644701f8305c418e77ef /crates/test_utils/src
parenta9db3d53a0997010f8d3f9c9b14636bef3754f8b (diff)
Move parser specific tests utils to parser tests
Diffstat (limited to 'crates/test_utils/src')
-rw-r--r--crates/test_utils/src/lib.rs106
1 files changed, 5 insertions, 101 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index e32a0a0c3..fba5f4281 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -13,7 +13,7 @@ mod fixture;
13use std::{ 13use std::{
14 convert::{TryFrom, TryInto}, 14 convert::{TryFrom, TryInto},
15 env, fs, 15 env, fs,
16 path::{Path, PathBuf}, 16 path::PathBuf,
17}; 17};
18 18
19use serde_json::Value; 19use serde_json::Value;
@@ -299,85 +299,6 @@ pub fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a
299 } 299 }
300} 300}
301 301
302/// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir`
303/// subdirectories defined by `paths`.
304///
305/// If the content of the matching output file differs from the output of `f()`
306/// the test will fail.
307///
308/// If there is no matching output file it will be created and filled with the
309/// output of `f()`, but the test will fail.
310pub fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
311where
312 F: Fn(&str, &Path) -> String,
313{
314 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
315 let actual = f(&input_code, &path);
316 let path = path.with_extension(outfile_extension);
317 if !path.exists() {
318 println!("\nfile: {}", path.display());
319 println!("No .txt file with expected result, creating...\n");
320 println!("{}\n{}", input_code, actual);
321 fs::write(&path, &actual).unwrap();
322 panic!("No expected result");
323 }
324 let expected = read_text(&path);
325 assert_equal_text(&expected, &actual, &path);
326 }
327}
328
329/// Collects all `.rs` files from `dir` subdirectories defined by `paths`.
330pub fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> {
331 paths
332 .iter()
333 .flat_map(|path| {
334 let path = root_dir.to_owned().join(path);
335 rust_files_in_dir(&path).into_iter()
336 })
337 .map(|path| {
338 let text = read_text(&path);
339 (path, text)
340 })
341 .collect()
342}
343
344/// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`.
345fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
346 let mut acc = Vec::new();
347 for file in fs::read_dir(&dir).unwrap() {
348 let file = file.unwrap();
349 let path = file.path();
350 if path.extension().unwrap_or_default() == "rs" {
351 acc.push(path);
352 }
353 }
354 acc.sort();
355 acc
356}
357
358/// Returns the path to the root directory of `rust-analyzer` project.
359pub fn project_dir() -> PathBuf {
360 let dir = env!("CARGO_MANIFEST_DIR");
361 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
362}
363
364/// Read file and normalize newlines.
365///
366/// `rustc` seems to always normalize `\r\n` newlines to `\n`:
367///
368/// ```
369/// let s = "
370/// ";
371/// assert_eq!(s.as_bytes(), &[10]);
372/// ```
373///
374/// so this should always be correct.
375pub fn read_text(path: &Path) -> String {
376 fs::read_to_string(path)
377 .unwrap_or_else(|_| panic!("File at {:?} should be valid", path))
378 .replace("\r\n", "\n")
379}
380
381/// Returns `false` if slow tests should not run, otherwise returns `true` and 302/// Returns `false` if slow tests should not run, otherwise returns `true` and
382/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag 303/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
383/// that slow tests did run. 304/// that slow tests did run.
@@ -392,25 +313,8 @@ pub fn skip_slow_tests() -> bool {
392 should_skip 313 should_skip
393} 314}
394 315
395/// Asserts that `expected` and `actual` strings are equal. If they differ only 316/// Returns the path to the root directory of `rust-analyzer` project.
396/// in trailing or leading whitespace the test won't fail and 317pub fn project_dir() -> PathBuf {
397/// the contents of `actual` will be written to the file located at `path`. 318 let dir = env!("CARGO_MANIFEST_DIR");
398fn assert_equal_text(expected: &str, actual: &str, path: &Path) { 319 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
399 if expected == actual {
400 return;
401 }
402 let dir = project_dir();
403 let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path);
404 if expected.trim() == actual.trim() {
405 println!("whitespace difference, rewriting");
406 println!("file: {}\n", pretty_path.display());
407 fs::write(path, actual).unwrap();
408 return;
409 }
410 if env::var("UPDATE_EXPECTATIONS").is_ok() {
411 println!("rewriting {}", pretty_path.display());
412 fs::write(path, actual).unwrap();
413 return;
414 }
415 assert_eq_text!(expected, actual, "file: {}", pretty_path.display());
416} 320}