diff options
Diffstat (limited to 'xtask/tests/tidy-tests/main.rs')
-rw-r--r-- | xtask/tests/tidy-tests/main.rs | 145 |
1 files changed, 144 insertions, 1 deletions
diff --git a/xtask/tests/tidy-tests/main.rs b/xtask/tests/tidy-tests/main.rs index 56d1318d6..2d2d88bec 100644 --- a/xtask/tests/tidy-tests/main.rs +++ b/xtask/tests/tidy-tests/main.rs | |||
@@ -1,2 +1,145 @@ | |||
1 | mod cli; | 1 | mod cli; |
2 | mod docs; | 2 | |
3 | use std::{ | ||
4 | collections::HashMap, | ||
5 | path::{Path, PathBuf}, | ||
6 | }; | ||
7 | |||
8 | use walkdir::{DirEntry, WalkDir}; | ||
9 | use xtask::{not_bash::fs2, project_root}; | ||
10 | |||
11 | #[test] | ||
12 | fn rust_files_are_tidy() { | ||
13 | let mut tidy_docs = TidyDocs::default(); | ||
14 | for path in rust_files() { | ||
15 | let text = fs2::read_to_string(&path).unwrap(); | ||
16 | check_todo(&path, &text); | ||
17 | tidy_docs.visit(&path, &text); | ||
18 | } | ||
19 | tidy_docs.finish(); | ||
20 | } | ||
21 | |||
22 | fn check_todo(path: &Path, text: &str) { | ||
23 | if path.ends_with("tests/cli.rs") { | ||
24 | return; | ||
25 | } | ||
26 | if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") { | ||
27 | panic!( | ||
28 | "\nTODO markers should not be committed to the master branch,\n\ | ||
29 | use FIXME instead\n\ | ||
30 | {}\n", | ||
31 | path.display(), | ||
32 | ) | ||
33 | } | ||
34 | } | ||
35 | |||
36 | #[derive(Default)] | ||
37 | struct TidyDocs { | ||
38 | missing_docs: Vec<String>, | ||
39 | contains_fixme: Vec<PathBuf>, | ||
40 | } | ||
41 | |||
42 | impl TidyDocs { | ||
43 | fn visit(&mut self, path: &Path, text: &str) { | ||
44 | if is_exclude_dir(path) || is_exclude_file(path) { | ||
45 | return; | ||
46 | } | ||
47 | |||
48 | let first_line = match text.lines().next() { | ||
49 | Some(it) => it, | ||
50 | None => return, | ||
51 | }; | ||
52 | |||
53 | if first_line.starts_with("//!") { | ||
54 | if first_line.contains("FIXME") { | ||
55 | self.contains_fixme.push(path.to_path_buf()) | ||
56 | } | ||
57 | } else { | ||
58 | self.missing_docs.push(path.display().to_string()); | ||
59 | } | ||
60 | |||
61 | fn is_exclude_dir(p: &Path) -> bool { | ||
62 | // Test hopefully don't really need comments, and for assists we already | ||
63 | // have special comments which are source of doc tests and user docs. | ||
64 | let exclude_dirs = ["tests", "test_data", "handlers"]; | ||
65 | let mut cur_path = p; | ||
66 | while let Some(path) = cur_path.parent() { | ||
67 | if exclude_dirs.iter().any(|dir| path.ends_with(dir)) { | ||
68 | return true; | ||
69 | } | ||
70 | cur_path = path; | ||
71 | } | ||
72 | |||
73 | false | ||
74 | } | ||
75 | |||
76 | fn is_exclude_file(d: &Path) -> bool { | ||
77 | let file_names = ["tests.rs"]; | ||
78 | |||
79 | d.file_name() | ||
80 | .unwrap_or_default() | ||
81 | .to_str() | ||
82 | .map(|f_n| file_names.iter().any(|name| *name == f_n)) | ||
83 | .unwrap_or(false) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | fn finish(self) { | ||
88 | if !self.missing_docs.is_empty() { | ||
89 | panic!( | ||
90 | "\nMissing docs strings\n\n\ | ||
91 | modules:\n{}\n\n", | ||
92 | self.missing_docs.join("\n") | ||
93 | ) | ||
94 | } | ||
95 | |||
96 | let whitelist = [ | ||
97 | "ra_db", | ||
98 | "ra_hir", | ||
99 | "ra_hir_expand", | ||
100 | "ra_ide", | ||
101 | "ra_mbe", | ||
102 | "ra_parser", | ||
103 | "ra_prof", | ||
104 | "ra_project_model", | ||
105 | "ra_syntax", | ||
106 | "ra_text_edit", | ||
107 | "ra_tt", | ||
108 | "ra_hir_ty", | ||
109 | ]; | ||
110 | |||
111 | let mut has_fixmes = | ||
112 | whitelist.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>(); | ||
113 | 'outer: for path in self.contains_fixme { | ||
114 | for krate in whitelist.iter() { | ||
115 | if path.components().any(|it| it.as_os_str() == *krate) { | ||
116 | has_fixmes.insert(krate, true); | ||
117 | continue 'outer; | ||
118 | } | ||
119 | } | ||
120 | panic!("FIXME doc in a fully-documented crate: {}", path.display()) | ||
121 | } | ||
122 | |||
123 | for (krate, has_fixme) in has_fixmes.iter() { | ||
124 | if !has_fixme { | ||
125 | panic!("crate {} is fully documented, remove it from the white list", krate) | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | |||
131 | fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
132 | let crates = project_root().join("crates"); | ||
133 | let iter = WalkDir::new(crates); | ||
134 | return iter | ||
135 | .into_iter() | ||
136 | .filter_entry(|e| !is_hidden(e)) | ||
137 | .map(|e| e.unwrap()) | ||
138 | .filter(|e| !e.file_type().is_dir()) | ||
139 | .map(|e| e.into_path()) | ||
140 | .filter(|path| path.extension().map(|it| it == "rs").unwrap_or(false)); | ||
141 | |||
142 | fn is_hidden(entry: &DirEntry) -> bool { | ||
143 | entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) | ||
144 | } | ||
145 | } | ||