From 7b0113b3d588fdc1f95eca1286fb2f6881abe65a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 12:30:17 +0200 Subject: Move parser specific tests utils to parser tests --- crates/ra_syntax/src/tests.rs | 100 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index 959967b79..f14f23628 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs @@ -1,9 +1,11 @@ use std::{ + env, fmt::Write, + fs, path::{Component, Path, PathBuf}, }; -use test_utils::{collect_rust_files, dir_tests, project_dir, read_text}; +use test_utils::{assert_eq_text, project_dir}; use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; @@ -200,3 +202,99 @@ where } }); } + +/// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir` +/// subdirectories defined by `paths`. +/// +/// If the content of the matching output file differs from the output of `f()` +/// the test will fail. +/// +/// If there is no matching output file it will be created and filled with the +/// output of `f()`, but the test will fail. +fn dir_tests(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F) +where + F: Fn(&str, &Path) -> String, +{ + for (path, input_code) in collect_rust_files(test_data_dir, paths) { + let actual = f(&input_code, &path); + let path = path.with_extension(outfile_extension); + if !path.exists() { + println!("\nfile: {}", path.display()); + println!("No .txt file with expected result, creating...\n"); + println!("{}\n{}", input_code, actual); + fs::write(&path, &actual).unwrap(); + panic!("No expected result"); + } + let expected = read_text(&path); + assert_equal_text(&expected, &actual, &path); + } +} + +/// Collects all `.rs` files from `dir` subdirectories defined by `paths`. +fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> { + paths + .iter() + .flat_map(|path| { + let path = root_dir.to_owned().join(path); + rust_files_in_dir(&path).into_iter() + }) + .map(|path| { + let text = read_text(&path); + (path, text) + }) + .collect() +} + +/// Collects paths to all `.rs` files from `dir` in a sorted `Vec`. +fn rust_files_in_dir(dir: &Path) -> Vec { + let mut acc = Vec::new(); + for file in fs::read_dir(&dir).unwrap() { + let file = file.unwrap(); + let path = file.path(); + if path.extension().unwrap_or_default() == "rs" { + acc.push(path); + } + } + acc.sort(); + acc +} + +/// Asserts that `expected` and `actual` strings are equal. If they differ only +/// in trailing or leading whitespace the test won't fail and +/// the contents of `actual` will be written to the file located at `path`. +fn assert_equal_text(expected: &str, actual: &str, path: &Path) { + if expected == actual { + return; + } + let dir = project_dir(); + let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path); + if expected.trim() == actual.trim() { + println!("whitespace difference, rewriting"); + println!("file: {}\n", pretty_path.display()); + fs::write(path, actual).unwrap(); + return; + } + if env::var("UPDATE_EXPECTATIONS").is_ok() { + println!("rewriting {}", pretty_path.display()); + fs::write(path, actual).unwrap(); + return; + } + assert_eq_text!(expected, actual, "file: {}", pretty_path.display()); +} + +/// Read file and normalize newlines. +/// +/// `rustc` seems to always normalize `\r\n` newlines to `\n`: +/// +/// ``` +/// let s = " +/// "; +/// assert_eq!(s.as_bytes(), &[10]); +/// ``` +/// +/// so this should always be correct. +fn read_text(path: &Path) -> String { + fs::read_to_string(path) + .unwrap_or_else(|_| panic!("File at {:?} should be valid", path)) + .replace("\r\n", "\n") +} -- cgit v1.2.3 From 991850bc3c2ff34fe8b3e63815067307d8d90db6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 12:31:03 +0200 Subject: Unify magic env var name --- crates/ra_syntax/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index f14f23628..7b4232497 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs @@ -274,7 +274,7 @@ fn assert_equal_text(expected: &str, actual: &str, path: &Path) { fs::write(path, actual).unwrap(); return; } - if env::var("UPDATE_EXPECTATIONS").is_ok() { + if env::var("UPDATE_EXPECT").is_ok() { println!("rewriting {}", pretty_path.display()); fs::write(path, actual).unwrap(); return; -- cgit v1.2.3