aboutsummaryrefslogtreecommitdiff
path: root/xtask/tests/tidy-tests
diff options
context:
space:
mode:
authorGalilée 'Bill' Enguehard <[email protected]>2020-05-21 22:27:38 +0100
committerGalilée 'Bill' Enguehard <[email protected]>2020-05-21 22:27:38 +0100
commit7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch)
tree866c4db826c959e79c63a6727bdb9f2c61e6fc4f /xtask/tests/tidy-tests
parentdb926218b2082077750291f8426ddd28b284cd08 (diff)
parent59732df8d40dfadc6dcf5951265416576399712a (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'xtask/tests/tidy-tests')
-rw-r--r--xtask/tests/tidy-tests/cli.rs32
-rw-r--r--xtask/tests/tidy-tests/main.rs153
2 files changed, 0 insertions, 185 deletions
diff --git a/xtask/tests/tidy-tests/cli.rs b/xtask/tests/tidy-tests/cli.rs
deleted file mode 100644
index f5b00a8b8..000000000
--- a/xtask/tests/tidy-tests/cli.rs
+++ /dev/null
@@ -1,32 +0,0 @@
1use xtask::{
2 codegen::{self, Mode},
3 run_rustfmt,
4};
5
6#[test]
7fn generated_grammar_is_fresh() {
8 if let Err(error) = codegen::generate_syntax(Mode::Verify) {
9 panic!("{}. Please update it by running `cargo xtask codegen`", error);
10 }
11}
12
13#[test]
14fn generated_tests_are_fresh() {
15 if let Err(error) = codegen::generate_parser_tests(Mode::Verify) {
16 panic!("{}. Please update tests by running `cargo xtask codegen`", error);
17 }
18}
19
20#[test]
21fn generated_assists_are_fresh() {
22 if let Err(error) = codegen::generate_assists_docs(Mode::Verify) {
23 panic!("{}. Please update assists by running `cargo xtask codegen`", error);
24 }
25}
26
27#[test]
28fn check_code_formatting() {
29 if let Err(error) = run_rustfmt(Mode::Verify) {
30 panic!("{}. Please format the code by running `cargo format`", error);
31 }
32}
diff --git a/xtask/tests/tidy-tests/main.rs b/xtask/tests/tidy-tests/main.rs
deleted file mode 100644
index ead642acc..000000000
--- a/xtask/tests/tidy-tests/main.rs
+++ /dev/null
@@ -1,153 +0,0 @@
1mod cli;
2
3use std::{
4 collections::HashMap,
5 path::{Path, PathBuf},
6};
7
8use xtask::{not_bash::fs2, project_root, rust_files};
9
10#[test]
11fn rust_files_are_tidy() {
12 let mut tidy_docs = TidyDocs::default();
13 for path in rust_files(&project_root().join("crates")) {
14 let text = fs2::read_to_string(&path).unwrap();
15 check_todo(&path, &text);
16 check_trailing_ws(&path, &text);
17 tidy_docs.visit(&path, &text);
18 }
19 tidy_docs.finish();
20}
21
22fn check_todo(path: &Path, text: &str) {
23 let whitelist = &[
24 // This file itself is whitelisted since this test itself contains matches.
25 "tests/cli.rs",
26 // Some of our assists generate `todo!()` so those files are whitelisted.
27 "doc_tests/generated.rs",
28 "handlers/add_missing_impl_members.rs",
29 "handlers/add_function.rs",
30 // To support generating `todo!()` in assists, we have `expr_todo()` in ast::make.
31 "ast/make.rs",
32 ];
33 if whitelist.iter().any(|p| path.ends_with(p)) {
34 return;
35 }
36 if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {
37 panic!(
38 "\nTODO markers or todo! macros should not be committed to the master branch,\n\
39 use FIXME instead\n\
40 {}\n",
41 path.display(),
42 )
43 }
44}
45
46fn check_trailing_ws(path: &Path, text: &str) {
47 if is_exclude_dir(path, &["test_data"]) {
48 return;
49 }
50 for (line_number, line) in text.lines().enumerate() {
51 if line.chars().last().map(char::is_whitespace) == Some(true) {
52 panic!("Trailing whitespace in {} at line {}", path.display(), line_number)
53 }
54 }
55}
56
57#[derive(Default)]
58struct TidyDocs {
59 missing_docs: Vec<String>,
60 contains_fixme: Vec<PathBuf>,
61}
62
63impl TidyDocs {
64 fn visit(&mut self, path: &Path, text: &str) {
65 // Test hopefully don't really need comments, and for assists we already
66 // have special comments which are source of doc tests and user docs.
67 if is_exclude_dir(path, &["tests", "test_data", "handlers"]) {
68 return;
69 }
70
71 if is_exclude_file(path) {
72 return;
73 }
74
75 let first_line = match text.lines().next() {
76 Some(it) => it,
77 None => return,
78 };
79
80 if first_line.starts_with("//!") {
81 if first_line.contains("FIXME") {
82 self.contains_fixme.push(path.to_path_buf())
83 }
84 } else {
85 self.missing_docs.push(path.display().to_string());
86 }
87
88 fn is_exclude_file(d: &Path) -> bool {
89 let file_names = ["tests.rs"];
90
91 d.file_name()
92 .unwrap_or_default()
93 .to_str()
94 .map(|f_n| file_names.iter().any(|name| *name == f_n))
95 .unwrap_or(false)
96 }
97 }
98
99 fn finish(self) {
100 if !self.missing_docs.is_empty() {
101 panic!(
102 "\nMissing docs strings\n\n\
103 modules:\n{}\n\n",
104 self.missing_docs.join("\n")
105 )
106 }
107
108 let whitelist = [
109 "ra_db",
110 "ra_hir",
111 "ra_hir_expand",
112 "ra_ide",
113 "ra_mbe",
114 "ra_parser",
115 "ra_prof",
116 "ra_project_model",
117 "ra_syntax",
118 "ra_text_edit",
119 "ra_tt",
120 "ra_hir_ty",
121 ];
122
123 let mut has_fixmes =
124 whitelist.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>();
125 'outer: for path in self.contains_fixme {
126 for krate in whitelist.iter() {
127 if path.components().any(|it| it.as_os_str() == *krate) {
128 has_fixmes.insert(krate, true);
129 continue 'outer;
130 }
131 }
132 panic!("FIXME doc in a fully-documented crate: {}", path.display())
133 }
134
135 for (krate, has_fixme) in has_fixmes.iter() {
136 if !has_fixme {
137 panic!("crate {} is fully documented, remove it from the white list", krate)
138 }
139 }
140 }
141}
142
143fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
144 let mut cur_path = p;
145 while let Some(path) = cur_path.parent() {
146 if dirs_to_exclude.iter().any(|dir| path.ends_with(dir)) {
147 return true;
148 }
149 cur_path = path;
150 }
151
152 false
153}