aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/rust-analyzer/src/config.rs15
-rw-r--r--crates/syntax/src/tests.rs6
-rw-r--r--crates/test_utils/src/bench_fixture.rs6
-rw-r--r--crates/test_utils/src/lib.rs44
4 files changed, 53 insertions, 18 deletions
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 4dbabdba7..078c83f75 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -859,12 +859,12 @@ fn manual(fields: &[(&'static str, &'static str, &[&str], &str)]) -> String {
859mod tests { 859mod tests {
860 use std::fs; 860 use std::fs;
861 861
862 use test_utils::project_dir; 862 use test_utils::{ensure_file_contents, project_root};
863 863
864 use super::*; 864 use super::*;
865 865
866 #[test] 866 #[test]
867 fn schema_in_sync_with_package_json() { 867 fn generate_package_json_config() {
868 let s = Config::json_schema(); 868 let s = Config::json_schema();
869 let schema = format!("{:#}", s); 869 let schema = format!("{:#}", s);
870 let mut schema = schema 870 let mut schema = schema
@@ -877,7 +877,7 @@ mod tests {
877 .to_string(); 877 .to_string();
878 schema.push_str(",\n"); 878 schema.push_str(",\n");
879 879
880 let package_json_path = project_dir().join("editors/code/package.json"); 880 let package_json_path = project_root().join("editors/code/package.json");
881 let mut package_json = fs::read_to_string(&package_json_path).unwrap(); 881 let mut package_json = fs::read_to_string(&package_json_path).unwrap();
882 882
883 let start_marker = " \"$generated-start\": false,\n"; 883 let start_marker = " \"$generated-start\": false,\n";
@@ -885,19 +885,18 @@ mod tests {
885 885
886 let start = package_json.find(start_marker).unwrap() + start_marker.len(); 886 let start = package_json.find(start_marker).unwrap() + start_marker.len();
887 let end = package_json.find(end_marker).unwrap(); 887 let end = package_json.find(end_marker).unwrap();
888
888 let p = remove_ws(&package_json[start..end]); 889 let p = remove_ws(&package_json[start..end]);
889 let s = remove_ws(&schema); 890 let s = remove_ws(&schema);
890
891 if !p.contains(&s) { 891 if !p.contains(&s) {
892 package_json.replace_range(start..end, &schema); 892 package_json.replace_range(start..end, &schema);
893 fs::write(&package_json_path, &mut package_json).unwrap(); 893 ensure_file_contents(&package_json_path, &package_json)
894 panic!("new config, updating package.json")
895 } 894 }
896 } 895 }
897 896
898 #[test] 897 #[test]
899 fn schema_in_sync_with_docs() { 898 fn generate_config_documentation() {
900 let docs_path = project_dir().join("docs/user/generated_config.adoc"); 899 let docs_path = project_root().join("docs/user/generated_config.adoc");
901 let current = fs::read_to_string(&docs_path).unwrap(); 900 let current = fs::read_to_string(&docs_path).unwrap();
902 let expected = ConfigData::manual(); 901 let expected = ConfigData::manual();
903 902
diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs
index b2c06e24f..ba0ccfaed 100644
--- a/crates/syntax/src/tests.rs
+++ b/crates/syntax/src/tests.rs
@@ -7,7 +7,7 @@ use std::{
7use ast::NameOwner; 7use ast::NameOwner;
8use expect_test::expect_file; 8use expect_test::expect_file;
9use rayon::prelude::*; 9use rayon::prelude::*;
10use test_utils::{bench, bench_fixture, project_dir, skip_slow_tests}; 10use test_utils::{bench, bench_fixture, project_root, skip_slow_tests};
11 11
12use crate::{ast, fuzz, tokenize, AstNode, SourceFile, SyntaxError, TextRange, TextSize, Token}; 12use crate::{ast, fuzz, tokenize, AstNode, SourceFile, SyntaxError, TextRange, TextSize, Token};
13 13
@@ -153,7 +153,7 @@ fn reparse_fuzz_tests() {
153/// Test that Rust-analyzer can parse and validate the rust-analyzer 153/// Test that Rust-analyzer can parse and validate the rust-analyzer
154#[test] 154#[test]
155fn self_hosting_parsing() { 155fn self_hosting_parsing() {
156 let dir = project_dir().join("crates"); 156 let dir = project_root().join("crates");
157 let files = walkdir::WalkDir::new(dir) 157 let files = walkdir::WalkDir::new(dir)
158 .into_iter() 158 .into_iter()
159 .filter_entry(|entry| { 159 .filter_entry(|entry| {
@@ -193,7 +193,7 @@ fn self_hosting_parsing() {
193} 193}
194 194
195fn test_data_dir() -> PathBuf { 195fn test_data_dir() -> PathBuf {
196 project_dir().join("crates/syntax/test_data") 196 project_root().join("crates/syntax/test_data")
197} 197}
198 198
199fn assert_errors_are_present(errors: &[SyntaxError], path: &Path) { 199fn assert_errors_are_present(errors: &[SyntaxError], path: &Path) {
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}