diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-08 18:52:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-08 18:52:08 +0000 |
commit | 8b7e82b012c417ec40a896203ad79f20cf5530ef (patch) | |
tree | 85413766cc45708d77dfa9cb5a3a00767af8461a /crates/test_utils | |
parent | 071dde1c1da10e3580bded99dc2d529074356536 (diff) | |
parent | d2bb2268d3a9cc0e2a6970c85c45724af5eb255c (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.rs | 6 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 44 |
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 | ||
5 | use stdx::format_to; | 5 | use stdx::format_to; |
6 | 6 | ||
7 | use crate::project_dir; | 7 | use crate::project_root; |
8 | 8 | ||
9 | pub fn big_struct() -> String { | 9 | pub fn big_struct() -> String { |
10 | let n = 1_000; | 10 | let n = 1_000; |
@@ -32,11 +32,11 @@ struct S{} {{ | |||
32 | } | 32 | } |
33 | 33 | ||
34 | pub fn glorious_old_parser() -> String { | 34 | pub 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 | ||
39 | pub fn numerous_macro_rules() -> String { | 39 | pub 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; | |||
14 | use std::{ | 14 | use 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 | ||
20 | use profile::StopWatch; | 20 | use profile::StopWatch; |
21 | use stdx::lines_with_ends; | 21 | use stdx::{is_ci, lines_with_ends}; |
22 | use text_size::{TextRange, TextSize}; | 22 | use text_size::{TextRange, TextSize}; |
23 | 23 | ||
24 | pub use dissimilar::diff as __diff; | 24 | pub 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. |
298 | pub fn project_dir() -> PathBuf { | 298 | pub 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. | ||
359 | pub 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. | ||
367 | pub 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 | |||
389 | fn normalize_newlines(s: &str) -> String { | ||
390 | s.replace("\r\n", "\n") | ||
391 | } | ||