aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tools/src/main.rs')
-rw-r--r--crates/tools/src/main.rs127
1 files changed, 5 insertions, 122 deletions
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index d6eabce6c..c3e293911 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -1,30 +1,13 @@
1use std::{ 1use clap::{App, SubCommand};
2 collections::HashMap,
3 fs,
4 path::{Path, PathBuf},
5};
6
7use clap::{App, Arg, SubCommand};
8use failure::bail;
9 2
10use tools::{ 3use tools::{
11 collect_tests, generate,install_format_hook, run, run_rustfmt, 4 generate, gen_tests, install_format_hook, run, run_rustfmt,
12 Mode, Overwrite, Result, Test, Verify, project_root, run_fuzzer 5 Overwrite, Result, run_fuzzer,
13}; 6};
14 7
15const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar";
16const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok";
17const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err";
18
19fn main() -> Result<()> { 8fn main() -> Result<()> {
20 let matches = App::new("tasks") 9 let matches = App::new("tasks")
21 .setting(clap::AppSettings::SubcommandRequiredElseHelp) 10 .setting(clap::AppSettings::SubcommandRequiredElseHelp)
22 .arg(
23 Arg::with_name("verify")
24 .long("--verify")
25 .help("Verify that generated code is up-to-date")
26 .global(true),
27 )
28 .subcommand(SubCommand::with_name("gen-syntax")) 11 .subcommand(SubCommand::with_name("gen-syntax"))
29 .subcommand(SubCommand::with_name("gen-tests")) 12 .subcommand(SubCommand::with_name("gen-tests"))
30 .subcommand(SubCommand::with_name("install-code")) 13 .subcommand(SubCommand::with_name("install-code"))
@@ -32,19 +15,14 @@ fn main() -> Result<()> {
32 .subcommand(SubCommand::with_name("format-hook")) 15 .subcommand(SubCommand::with_name("format-hook"))
33 .subcommand(SubCommand::with_name("fuzz-tests")) 16 .subcommand(SubCommand::with_name("fuzz-tests"))
34 .get_matches(); 17 .get_matches();
35 let mode = if matches.is_present("verify") {
36 Verify
37 } else {
38 Overwrite
39 };
40 match matches 18 match matches
41 .subcommand_name() 19 .subcommand_name()
42 .expect("Subcommand must be specified") 20 .expect("Subcommand must be specified")
43 { 21 {
44 "install-code" => install_code_extension()?, 22 "install-code" => install_code_extension()?,
45 "gen-tests" => gen_tests(mode)?, 23 "gen-tests" => gen_tests(Overwrite)?,
46 "gen-syntax" => generate(Overwrite)?, 24 "gen-syntax" => generate(Overwrite)?,
47 "format" => run_rustfmt(mode)?, 25 "format" => run_rustfmt(Overwrite)?,
48 "format-hook" => install_format_hook()?, 26 "format-hook" => install_format_hook()?,
49 "fuzz-tests" => run_fuzzer()?, 27 "fuzz-tests" => run_fuzzer()?,
50 _ => unreachable!(), 28 _ => unreachable!(),
@@ -52,101 +30,6 @@ fn main() -> Result<()> {
52 Ok(()) 30 Ok(())
53} 31}
54 32
55fn gen_tests(mode: Mode) -> Result<()> {
56 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
57 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> {
58 let tests_dir = project_root().join(into);
59 if !tests_dir.is_dir() {
60 fs::create_dir_all(&tests_dir)?;
61 }
62 // ok is never actually read, but it needs to be specified to create a Test in existing_tests
63 let existing = existing_tests(&tests_dir, true)?;
64 for t in existing.keys().filter(|&t| !tests.contains_key(t)) {
65 panic!("Test is deleted: {}", t);
66 }
67
68 let mut new_idx = existing.len() + 1;
69 for (name, test) in tests {
70 let path = match existing.get(name) {
71 Some((path, _test)) => path.clone(),
72 None => {
73 let file_name = format!("{:04}_{}.rs", new_idx, name);
74 new_idx += 1;
75 tests_dir.join(file_name)
76 }
77 };
78 teraron::update(&path, &test.text, mode)?;
79 }
80 Ok(())
81 }
82 install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?;
83 install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode)
84}
85
86#[derive(Default, Debug)]
87struct Tests {
88 pub ok: HashMap<String, Test>,
89 pub err: HashMap<String, Test>,
90}
91
92fn tests_from_dir(dir: &Path) -> Result<Tests> {
93 let mut res = Tests::default();
94 for entry in ::walkdir::WalkDir::new(dir) {
95 let entry = entry.unwrap();
96 if !entry.file_type().is_file() {
97 continue;
98 }
99 if entry.path().extension().unwrap_or_default() != "rs" {
100 continue;
101 }
102 process_file(&mut res, entry.path())?;
103 }
104 let grammar_rs = dir.parent().unwrap().join("grammar.rs");
105 process_file(&mut res, &grammar_rs)?;
106 return Ok(res);
107 fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
108 let text = fs::read_to_string(path)?;
109
110 for (_, test) in collect_tests(&text) {
111 if test.ok {
112 if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
113 bail!("Duplicate test: {}", old_test.name)
114 }
115 } else {
116 if let Some(old_test) = res.err.insert(test.name.clone(), test) {
117 bail!("Duplicate test: {}", old_test.name)
118 }
119 }
120 }
121 Ok(())
122 }
123}
124
125fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> {
126 let mut res = HashMap::new();
127 for file in fs::read_dir(dir)? {
128 let file = file?;
129 let path = file.path();
130 if path.extension().unwrap_or_default() != "rs" {
131 continue;
132 }
133 let name = {
134 let file_name = path.file_name().unwrap().to_str().unwrap();
135 file_name[5..file_name.len() - 3].to_string()
136 };
137 let text = fs::read_to_string(&path)?;
138 let test = Test {
139 name: name.clone(),
140 text,
141 ok,
142 };
143 if let Some(old) = res.insert(name, (path, test)) {
144 println!("Duplicate test: {:?}", old);
145 }
146 }
147 Ok(res)
148}
149
150fn install_code_extension() -> Result<()> { 33fn install_code_extension() -> Result<()> {
151 run("cargo install --path crates/ra_lsp_server --force", ".")?; 34 run("cargo install --path crates/ra_lsp_server --force", ".")?;
152 if cfg!(windows) { 35 if cfg!(windows) {