aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r--crates/tools/src/lib.rs106
1 files changed, 103 insertions, 3 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index d404db214..311bcb4d8 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,7 +1,8 @@
1use std::{ 1use std::{
2 fs,
3 collections::HashMap,
2 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
3 process::{Command, Stdio}, 5 process::{Command, Stdio},
4 fs::copy,
5 io::{Error, ErrorKind} 6 io::{Error, ErrorKind}
6}; 7};
7 8
@@ -13,6 +14,10 @@ pub use teraron::{Mode, Overwrite, Verify};
13pub type Result<T> = std::result::Result<T, failure::Error>; 14pub type Result<T> = std::result::Result<T, failure::Error>;
14 15
15pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 16pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
17const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar";
18const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok";
19const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err";
20
16pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; 21pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
17pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; 22pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
18const TOOLCHAIN: &str = "stable"; 23const TOOLCHAIN: &str = "stable";
@@ -130,9 +135,9 @@ pub fn install_format_hook() -> Result<()> {
130 if !result_path.exists() { 135 if !result_path.exists() {
131 run("cargo build --package tools --bin pre-commit", ".")?; 136 run("cargo build --package tools --bin pre-commit", ".")?;
132 if cfg!(windows) { 137 if cfg!(windows) {
133 copy("./target/debug/pre-commit.exe", result_path)?; 138 fs::copy("./target/debug/pre-commit.exe", result_path)?;
134 } else { 139 } else {
135 copy("./target/debug/pre-commit", result_path)?; 140 fs::copy("./target/debug/pre-commit", result_path)?;
136 } 141 }
137 } else { 142 } else {
138 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); 143 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
@@ -156,3 +161,98 @@ pub fn run_fuzzer() -> Result<()> {
156 "./crates/ra_syntax", 161 "./crates/ra_syntax",
157 ) 162 )
158} 163}
164
165pub fn gen_tests(mode: Mode) -> Result<()> {
166 let tests = tests_from_dir(&project_root().join(Path::new(GRAMMAR_DIR)))?;
167 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> {
168 let tests_dir = project_root().join(into);
169 if !tests_dir.is_dir() {
170 fs::create_dir_all(&tests_dir)?;
171 }
172 // ok is never actually read, but it needs to be specified to create a Test in existing_tests
173 let existing = existing_tests(&tests_dir, true)?;
174 for t in existing.keys().filter(|&t| !tests.contains_key(t)) {
175 panic!("Test is deleted: {}", t);
176 }
177
178 let mut new_idx = existing.len() + 1;
179 for (name, test) in tests {
180 let path = match existing.get(name) {
181 Some((path, _test)) => path.clone(),
182 None => {
183 let file_name = format!("{:04}_{}.rs", new_idx, name);
184 new_idx += 1;
185 tests_dir.join(file_name)
186 }
187 };
188 teraron::update(&path, &test.text, mode)?;
189 }
190 Ok(())
191 }
192 install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?;
193 install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode)
194}
195
196#[derive(Default, Debug)]
197struct Tests {
198 pub ok: HashMap<String, Test>,
199 pub err: HashMap<String, Test>,
200}
201
202fn tests_from_dir(dir: &Path) -> Result<Tests> {
203 let mut res = Tests::default();
204 for entry in ::walkdir::WalkDir::new(dir) {
205 let entry = entry.unwrap();
206 if !entry.file_type().is_file() {
207 continue;
208 }
209 if entry.path().extension().unwrap_or_default() != "rs" {
210 continue;
211 }
212 process_file(&mut res, entry.path())?;
213 }
214 let grammar_rs = dir.parent().unwrap().join("grammar.rs");
215 process_file(&mut res, &grammar_rs)?;
216 return Ok(res);
217 fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
218 let text = fs::read_to_string(path)?;
219
220 for (_, test) in collect_tests(&text) {
221 if test.ok {
222 if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
223 bail!("Duplicate test: {}", old_test.name)
224 }
225 } else {
226 if let Some(old_test) = res.err.insert(test.name.clone(), test) {
227 bail!("Duplicate test: {}", old_test.name)
228 }
229 }
230 }
231 Ok(())
232 }
233}
234
235fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> {
236 let mut res = HashMap::new();
237 for file in fs::read_dir(dir)? {
238 let file = file?;
239 let path = file.path();
240 if path.extension().unwrap_or_default() != "rs" {
241 continue;
242 }
243 let name = {
244 let file_name = path.file_name().unwrap().to_str().unwrap();
245 file_name[5..file_name.len() - 3].to_string()
246 };
247 let text = fs::read_to_string(&path)?;
248 let test = Test {
249 name: name.clone(),
250 text,
251 ok,
252 };
253 if let Some(old) = res.insert(name, (path, test)) {
254 println!("Duplicate test: {:?}", old);
255 }
256 }
257 Ok(res)
258}