mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Simplify
This commit is contained in:
parent
f9d41bd18f
commit
8e1ebbcc13
2 changed files with 20 additions and 32 deletions
|
@ -42,8 +42,8 @@ macro_rules! expect {
|
||||||
/// expect_file!["/crates/foo/test_data/bar.html"]
|
/// expect_file!["/crates/foo/test_data/bar.html"]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! expect_file {
|
macro_rules! expect_file {
|
||||||
[$path:literal] => {$crate::ExpectFile {
|
[$path:expr] => {$crate::ExpectFile {
|
||||||
path: $crate::ExpectFilePath::Static($path)
|
path: std::path::PathBuf::from($path)
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,13 +55,7 @@ pub struct Expect {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ExpectFile {
|
pub struct ExpectFile {
|
||||||
pub path: ExpectFilePath,
|
pub path: PathBuf,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ExpectFilePath {
|
|
||||||
Static(&'static str),
|
|
||||||
Dynamic(PathBuf),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -120,9 +114,6 @@ impl Expect {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExpectFile {
|
impl ExpectFile {
|
||||||
pub fn new(path: PathBuf) -> ExpectFile {
|
|
||||||
ExpectFile { path: ExpectFilePath::Dynamic(path) }
|
|
||||||
}
|
|
||||||
pub fn assert_eq(&self, actual: &str) {
|
pub fn assert_eq(&self, actual: &str) {
|
||||||
let expected = self.read();
|
let expected = self.read();
|
||||||
if actual == expected {
|
if actual == expected {
|
||||||
|
@ -136,14 +127,8 @@ impl ExpectFile {
|
||||||
fn write(&self, contents: &str) {
|
fn write(&self, contents: &str) {
|
||||||
fs::write(self.abs_path(), contents).unwrap()
|
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 {
|
fn abs_path(&self) -> PathBuf {
|
||||||
workspace_root().join(self.path())
|
WORKSPACE_ROOT.join(&self.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,11 +156,11 @@ impl Runtime {
|
||||||
fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
|
fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
|
||||||
let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||||
if update_expect() {
|
if update_expect() {
|
||||||
println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display());
|
println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path.display());
|
||||||
expect.write(actual);
|
expect.write(actual);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rt.panic(expect.path().display().to_string(), expected, actual);
|
rt.panic(expect.path.display().to_string(), expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn panic(&mut self, position: String, expected: &str, actual: &str) {
|
fn panic(&mut self, position: String, expected: &str, actual: &str) {
|
||||||
|
@ -219,7 +204,7 @@ struct FileRuntime {
|
||||||
|
|
||||||
impl FileRuntime {
|
impl FileRuntime {
|
||||||
fn new(expect: &Expect) -> FileRuntime {
|
fn new(expect: &Expect) -> FileRuntime {
|
||||||
let path = workspace_root().join(expect.position.file);
|
let path = WORKSPACE_ROOT.join(expect.position.file);
|
||||||
let original_text = fs::read_to_string(&path).unwrap();
|
let original_text = fs::read_to_string(&path).unwrap();
|
||||||
let patchwork = Patchwork::new(original_text.clone());
|
let patchwork = Patchwork::new(original_text.clone());
|
||||||
FileRuntime { path, original_text, patchwork }
|
FileRuntime { path, original_text, patchwork }
|
||||||
|
@ -307,15 +292,17 @@ fn format_patch(line_indent: usize, patch: &str) -> String {
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
fn workspace_root() -> PathBuf {
|
static WORKSPACE_ROOT: Lazy<PathBuf> = Lazy::new(|| {
|
||||||
Path::new(
|
let my_manifest =
|
||||||
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
|
env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned());
|
||||||
)
|
// Heuristic, see https://github.com/rust-lang/cargo/issues/3946
|
||||||
|
Path::new(&my_manifest)
|
||||||
.ancestors()
|
.ancestors()
|
||||||
.nth(2)
|
.filter(|it| it.join("Cargo.toml").exists())
|
||||||
|
.last()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_path_buf()
|
.to_path_buf()
|
||||||
}
|
});
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::{
|
||||||
path::{Component, Path, PathBuf},
|
path::{Component, Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use expect::expect_file;
|
||||||
use test_utils::project_dir;
|
use test_utils::project_dir;
|
||||||
|
|
||||||
use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
|
use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
|
||||||
|
@ -217,7 +218,7 @@ where
|
||||||
for (path, input_code) in collect_rust_files(test_data_dir, paths) {
|
for (path, input_code) in collect_rust_files(test_data_dir, paths) {
|
||||||
let actual = f(&input_code, &path);
|
let actual = f(&input_code, &path);
|
||||||
let path = path.with_extension(outfile_extension);
|
let path = path.with_extension(outfile_extension);
|
||||||
expect::ExpectFile::new(path).assert_eq(&actual)
|
expect_file![path].assert_eq(&actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue