aboutsummaryrefslogtreecommitdiff
path: root/xtask/tests/tidy-tests/main.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-06 09:25:25 +0100
committerAleksey Kladov <[email protected]>2020-05-06 09:25:34 +0100
commit845d47759ed59993b0c6c39f2f7237e96f4e8eaa (patch)
tree1fe969024c3860802dd4d38392c193929aafdc5d /xtask/tests/tidy-tests/main.rs
parenteea431180eb5267928c8e0d4b8099ab1042e01d1 (diff)
Simplify tidy tests
Diffstat (limited to 'xtask/tests/tidy-tests/main.rs')
-rw-r--r--xtask/tests/tidy-tests/main.rs152
1 files changed, 0 insertions, 152 deletions
diff --git a/xtask/tests/tidy-tests/main.rs b/xtask/tests/tidy-tests/main.rs
deleted file mode 100644
index a3c2e37d1..000000000
--- a/xtask/tests/tidy-tests/main.rs
+++ /dev/null
@@ -1,152 +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 "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_tt",
119 "ra_hir_ty",
120 ];
121
122 let mut has_fixmes =
123 whitelist.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>();
124 'outer: for path in self.contains_fixme {
125 for krate in whitelist.iter() {
126 if path.components().any(|it| it.as_os_str() == *krate) {
127 has_fixmes.insert(krate, true);
128 continue 'outer;
129 }
130 }
131 panic!("FIXME doc in a fully-documented crate: {}", path.display())
132 }
133
134 for (krate, has_fixme) in has_fixmes.iter() {
135 if !has_fixme {
136 panic!("crate {} is fully documented, remove it from the white list", krate)
137 }
138 }
139 }
140}
141
142fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
143 let mut cur_path = p;
144 while let Some(path) = cur_path.parent() {
145 if dirs_to_exclude.iter().any(|dir| path.ends_with(dir)) {
146 return true;
147 }
148 cur_path = path;
149 }
150
151 false
152}