mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Merge #5278
5278: expect should be a dev dep r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
f9d41bd18f
5 changed files with 27 additions and 40 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1262,6 +1262,7 @@ name = "ra_syntax"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"expect",
|
||||
"itertools",
|
||||
"once_cell",
|
||||
"ra_parser",
|
||||
|
|
|
@ -42,7 +42,9 @@ macro_rules! expect {
|
|||
/// expect_file!["/crates/foo/test_data/bar.html"]
|
||||
#[macro_export]
|
||||
macro_rules! expect_file {
|
||||
[$path:literal] => {$crate::ExpectFile { path: $path }};
|
||||
[$path:literal] => {$crate::ExpectFile {
|
||||
path: $crate::ExpectFilePath::Static($path)
|
||||
}};
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -53,7 +55,13 @@ pub struct Expect {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct ExpectFile {
|
||||
pub path: &'static str,
|
||||
pub path: ExpectFilePath,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ExpectFilePath {
|
||||
Static(&'static str),
|
||||
Dynamic(PathBuf),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -112,6 +120,9 @@ impl Expect {
|
|||
}
|
||||
|
||||
impl ExpectFile {
|
||||
pub fn new(path: PathBuf) -> ExpectFile {
|
||||
ExpectFile { path: ExpectFilePath::Dynamic(path) }
|
||||
}
|
||||
pub fn assert_eq(&self, actual: &str) {
|
||||
let expected = self.read();
|
||||
if actual == expected {
|
||||
|
@ -125,8 +136,14 @@ impl ExpectFile {
|
|||
fn write(&self, contents: &str) {
|
||||
fs::write(self.abs_path(), contents).unwrap()
|
||||
}
|
||||
fn path(&self) -> &Path {
|
||||
match &self.path {
|
||||
ExpectFilePath::Static(it) => it.as_ref(),
|
||||
ExpectFilePath::Dynamic(it) => it.as_path(),
|
||||
}
|
||||
}
|
||||
fn abs_path(&self) -> PathBuf {
|
||||
workspace_root().join(self.path)
|
||||
workspace_root().join(self.path())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,11 +171,11 @@ impl Runtime {
|
|||
fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
|
||||
let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
if update_expect() {
|
||||
println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path);
|
||||
println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display());
|
||||
expect.write(actual);
|
||||
return;
|
||||
}
|
||||
rt.panic(expect.path.to_string(), expected, actual);
|
||||
rt.panic(expect.path().display().to_string(), expected, actual);
|
||||
}
|
||||
|
||||
fn panic(&mut self, position: String, expected: &str, actual: &str) {
|
||||
|
|
|
@ -28,7 +28,6 @@ ra_cfg = { path = "../ra_cfg" }
|
|||
ra_fmt = { path = "../ra_fmt" }
|
||||
ra_prof = { path = "../ra_prof" }
|
||||
test_utils = { path = "../test_utils" }
|
||||
expect = { path = "../expect" }
|
||||
ra_assists = { path = "../ra_assists" }
|
||||
ra_ssr = { path = "../ra_ssr" }
|
||||
|
||||
|
@ -38,3 +37,4 @@ hir = { path = "../ra_hir", package = "ra_hir" }
|
|||
|
||||
[dev-dependencies]
|
||||
insta = "0.16.0"
|
||||
expect = { path = "../expect" }
|
||||
|
|
|
@ -31,4 +31,5 @@ serde = { version = "1.0.106", features = ["derive"] }
|
|||
|
||||
[dev-dependencies]
|
||||
test_utils = { path = "../test_utils" }
|
||||
expect = { path = "../expect" }
|
||||
walkdir = "2.3.1"
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use std::{
|
||||
env,
|
||||
fmt::Write,
|
||||
fs,
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
|
||||
use test_utils::{assert_eq_text, project_dir};
|
||||
use test_utils::project_dir;
|
||||
|
||||
use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
|
||||
|
||||
|
@ -218,15 +217,7 @@ where
|
|||
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);
|
||||
expect::ExpectFile::new(path).assert_eq(&actual)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,29 +250,6 @@ fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
|
|||
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_EXPECT").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`:
|
||||
|
|
Loading…
Reference in a new issue