aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-08 18:52:08 +0000
committerGitHub <[email protected]>2021-03-08 18:52:08 +0000
commit8b7e82b012c417ec40a896203ad79f20cf5530ef (patch)
tree85413766cc45708d77dfa9cb5a3a00767af8461a /crates/test_utils
parent071dde1c1da10e3580bded99dc2d529074356536 (diff)
parentd2bb2268d3a9cc0e2a6970c85c45724af5eb255c (diff)
Merge #7918
7918: Generalize file ensuring infrastructure r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/bench_fixture.rs6
-rw-r--r--crates/test_utils/src/lib.rs44
2 files changed, 43 insertions, 7 deletions
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;
4 4
5use stdx::format_to; 5use stdx::format_to;
6 6
7use crate::project_dir; 7use crate::project_root;
8 8
9pub fn big_struct() -> String { 9pub fn big_struct() -> String {
10 let n = 1_000; 10 let n = 1_000;
@@ -32,11 +32,11 @@ struct S{} {{
32} 32}
33 33
34pub fn glorious_old_parser() -> String { 34pub fn glorious_old_parser() -> String {
35 let path = project_dir().join("bench_data/glorious_old_parser"); 35 let path = project_root().join("bench_data/glorious_old_parser");
36 fs::read_to_string(&path).unwrap() 36 fs::read_to_string(&path).unwrap()
37} 37}
38 38
39pub fn numerous_macro_rules() -> String { 39pub fn numerous_macro_rules() -> String {
40 let path = project_dir().join("bench_data/numerous_macro_rules"); 40 let path = project_root().join("bench_data/numerous_macro_rules");
41 fs::read_to_string(&path).unwrap() 41 fs::read_to_string(&path).unwrap()
42} 42}
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 27b05e34b..6041ab5e4 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -14,11 +14,11 @@ mod fixture;
14use std::{ 14use std::{
15 convert::{TryFrom, TryInto}, 15 convert::{TryFrom, TryInto},
16 env, fs, 16 env, fs,
17 path::PathBuf, 17 path::{Path, PathBuf},
18}; 18};
19 19
20use profile::StopWatch; 20use profile::StopWatch;
21use stdx::lines_with_ends; 21use stdx::{is_ci, lines_with_ends};
22use text_size::{TextRange, TextSize}; 22use text_size::{TextRange, TextSize};
23 23
24pub use dissimilar::diff as __diff; 24pub use dissimilar::diff as __diff;
@@ -288,14 +288,14 @@ pub fn skip_slow_tests() -> bool {
288 if should_skip { 288 if should_skip {
289 eprintln!("ignoring slow test") 289 eprintln!("ignoring slow test")
290 } else { 290 } else {
291 let path = project_dir().join("./target/.slow_tests_cookie"); 291 let path = project_root().join("./target/.slow_tests_cookie");
292 fs::write(&path, ".").unwrap(); 292 fs::write(&path, ".").unwrap();
293 } 293 }
294 should_skip 294 should_skip
295} 295}
296 296
297/// Returns the path to the root directory of `rust-analyzer` project. 297/// Returns the path to the root directory of `rust-analyzer` project.
298pub fn project_dir() -> PathBuf { 298pub fn project_root() -> PathBuf {
299 let dir = env!("CARGO_MANIFEST_DIR"); 299 let dir = env!("CARGO_MANIFEST_DIR");
300 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned() 300 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
301} 301}
@@ -353,3 +353,39 @@ pub fn bench(label: &'static str) -> impl Drop {
353 353
354 Bencher { sw: StopWatch::start(), label } 354 Bencher { sw: StopWatch::start(), label }
355} 355}
356
357/// Checks that the `file` has the specified `contents`. If that is not the
358/// case, updates the file and then fails the test.
359pub fn ensure_file_contents(file: &Path, contents: &str) {
360 if let Err(()) = try_ensure_file_contents(file, contents) {
361 panic!("Some files were not up-to-date");
362 }
363}
364
365/// Checks that the `file` has the specified `contents`. If that is not the
366/// case, updates the file and return an Error.
367pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
368 match std::fs::read_to_string(file) {
369 Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
370 return Ok(())
371 }
372 _ => (),
373 }
374 let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
375 eprintln!(
376 "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
377 display_path.display()
378 );
379 if is_ci() {
380 eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n");
381 }
382 if let Some(parent) = file.parent() {
383 let _ = std::fs::create_dir_all(parent);
384 }
385 std::fs::write(file, contents).unwrap();
386 Err(())
387}
388
389fn normalize_newlines(s: &str) -> String {
390 s.replace("\r\n", "\n")
391}