From b6ba0dec0c08dd2afcda951b09966b457f2c8bc3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 17:20:36 +0300 Subject: Generalize file ensuring infrastructure --- crates/test_utils/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'crates/test_utils/src') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 27b05e34b..7f122b13e 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -14,7 +14,7 @@ mod fixture; use std::{ convert::{TryFrom, TryInto}, env, fs, - path::PathBuf, + path::{Path, PathBuf}, }; use profile::StopWatch; @@ -353,3 +353,36 @@ pub fn bench(label: &'static str) -> impl Drop { Bencher { sw: StopWatch::start(), label } } + +/// Checks that the `file` has the specified `contents`. If that is not the +/// case, updates the file and then fails the test. +pub fn ensure_file_contents(file: &Path, contents: &str) { + if let Err(()) = try_ensure_file_contents(file, contents) { + panic!("Some files were not up-to-date"); + } +} + +/// Checks that the `file` has the specified `contents`. If that is not the +/// case, updates the file and return an Error. +pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> { + match std::fs::read_to_string(file) { + Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => { + return Ok(()) + } + _ => (), + } + let display_path = file.strip_prefix(&project_dir()).unwrap_or(file); + eprintln!( + "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", + display_path.display() + ); + if let Some(parent) = file.parent() { + let _ = std::fs::create_dir_all(parent); + } + std::fs::write(file, contents).unwrap(); + Err(()) +} + +fn normalize_newlines(s: &str) -> String { + s.replace("\r\n", "\n") +} -- cgit v1.2.3 From abb6b8f14c4d05cf344048263651d8192997b6cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 20:22:33 +0300 Subject: Use the same name in xtask and test utils --- crates/test_utils/src/bench_fixture.rs | 6 +++--- crates/test_utils/src/lib.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/test_utils/src') diff --git a/crates/test_utils/src/bench_fixture.rs b/crates/test_utils/src/bench_fixture.rs index d775e2cc9..3a37c4473 100644 --- a/crates/test_utils/src/bench_fixture.rs +++ b/crates/test_utils/src/bench_fixture.rs @@ -4,7 +4,7 @@ use std::fs; use stdx::format_to; -use crate::project_dir; +use crate::project_root; pub fn big_struct() -> String { let n = 1_000; @@ -32,11 +32,11 @@ struct S{} {{ } pub fn glorious_old_parser() -> String { - let path = project_dir().join("bench_data/glorious_old_parser"); + let path = project_root().join("bench_data/glorious_old_parser"); fs::read_to_string(&path).unwrap() } pub fn numerous_macro_rules() -> String { - let path = project_dir().join("bench_data/numerous_macro_rules"); + let path = project_root().join("bench_data/numerous_macro_rules"); fs::read_to_string(&path).unwrap() } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 7f122b13e..097a54139 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -288,14 +288,14 @@ pub fn skip_slow_tests() -> bool { if should_skip { eprintln!("ignoring slow test") } else { - let path = project_dir().join("./target/.slow_tests_cookie"); + let path = project_root().join("./target/.slow_tests_cookie"); fs::write(&path, ".").unwrap(); } should_skip } /// Returns the path to the root directory of `rust-analyzer` project. -pub fn project_dir() -> PathBuf { +pub fn project_root() -> PathBuf { let dir = env!("CARGO_MANIFEST_DIR"); PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned() } @@ -371,7 +371,7 @@ pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> { } _ => (), } - let display_path = file.strip_prefix(&project_dir()).unwrap_or(file); + let display_path = file.strip_prefix(&project_root()).unwrap_or(file); eprintln!( "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", display_path.display() -- cgit v1.2.3 From 1eb61203b725684fd2c7e25ac7e2d53eef10c64c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 21:13:15 +0300 Subject: Make `code generation` just work Contributors don't need to learn about `cargo xtask codegen` if `cargo test` just does the right thing. --- crates/test_utils/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates/test_utils/src') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 097a54139..dd582c77c 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -18,7 +18,7 @@ use std::{ }; use profile::StopWatch; -use stdx::lines_with_ends; +use stdx::{is_ci, lines_with_ends}; use text_size::{TextRange, TextSize}; pub use dissimilar::diff as __diff; @@ -376,6 +376,9 @@ pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> { "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", display_path.display() ); + if is_ci() { + eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n"); + } if let Some(parent) = file.parent() { let _ = std::fs::create_dir_all(parent); } -- cgit v1.2.3 From c0943f84fdb67025662dbcfc011e415870ee80a6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 21:41:45 +0300 Subject: Cleanup the error message --- crates/test_utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/test_utils/src') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index dd582c77c..6041ab5e4 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -377,7 +377,7 @@ pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> { display_path.display() ); if is_ci() { - eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n"); + eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n"); } if let Some(parent) = file.parent() { let _ = std::fs::create_dir_all(parent); -- cgit v1.2.3