diff options
Diffstat (limited to 'tools/src/main.rs')
-rw-r--r-- | tools/src/main.rs | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/tools/src/main.rs b/tools/src/main.rs index 84a0cf1b6..3acb6e7ed 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs | |||
@@ -8,7 +8,7 @@ extern crate tools; | |||
8 | #[macro_use] | 8 | #[macro_use] |
9 | extern crate commandspec; | 9 | extern crate commandspec; |
10 | 10 | ||
11 | use std::{collections::{HashSet, HashMap}, fs, path::Path}; | 11 | use std::{collections::{HashMap}, fs, path::{Path, PathBuf}}; |
12 | use clap::{App, Arg, SubCommand}; | 12 | use clap::{App, Arg, SubCommand}; |
13 | use tools::{collect_tests, Test}; | 13 | use tools::{collect_tests, Test}; |
14 | 14 | ||
@@ -104,21 +104,27 @@ fn gen_tests(verify: bool) -> Result<()> { | |||
104 | } | 104 | } |
105 | let existing = existing_tests(inline_tests_dir)?; | 105 | let existing = existing_tests(inline_tests_dir)?; |
106 | 106 | ||
107 | for t in existing.difference(&tests) { | 107 | for t in existing.keys().filter(|&t| !tests.contains_key(t)) { |
108 | panic!("Test is deleted: {}\n{}", t.name, t.text); | 108 | panic!("Test is deleted: {}", t); |
109 | } | 109 | } |
110 | 110 | ||
111 | let new_tests = tests.difference(&existing); | 111 | let mut new_idx = existing.len() + 2; |
112 | for (i, t) in new_tests.enumerate() { | 112 | for (name, test) in tests { |
113 | let name = format!("{:04}_{}.rs", existing.len() + i + 1, t.name); | 113 | let path = match existing.get(&name) { |
114 | let path = inline_tests_dir.join(name); | 114 | Some((path, _test)) => path.clone(), |
115 | update(&path, &t.text, verify)?; | 115 | None => { |
116 | let file_name = format!("{:04}_{}.rs", new_idx, name); | ||
117 | new_idx += 1; | ||
118 | inline_tests_dir.join(file_name) | ||
119 | } | ||
120 | }; | ||
121 | update(&path, &test.text, verify)?; | ||
116 | } | 122 | } |
117 | Ok(()) | 123 | Ok(()) |
118 | } | 124 | } |
119 | 125 | ||
120 | fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> { | 126 | fn tests_from_dir(dir: &Path) -> Result<HashMap<String, Test>> { |
121 | let mut res = HashSet::new(); | 127 | let mut res = HashMap::new(); |
122 | for entry in ::walkdir::WalkDir::new(dir) { | 128 | for entry in ::walkdir::WalkDir::new(dir) { |
123 | let entry = entry.unwrap(); | 129 | let entry = entry.unwrap(); |
124 | if !entry.file_type().is_file() { | 130 | if !entry.file_type().is_file() { |
@@ -130,7 +136,7 @@ fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> { | |||
130 | let text = fs::read_to_string(entry.path())?; | 136 | let text = fs::read_to_string(entry.path())?; |
131 | 137 | ||
132 | for (_, test) in collect_tests(&text) { | 138 | for (_, test) in collect_tests(&text) { |
133 | if let Some(old_test) = res.replace(test) { | 139 | if let Some(old_test) = res.insert(test.name.clone(), test) { |
134 | bail!("Duplicate test: {}", old_test.name) | 140 | bail!("Duplicate test: {}", old_test.name) |
135 | } | 141 | } |
136 | } | 142 | } |
@@ -138,18 +144,24 @@ fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> { | |||
138 | Ok(res) | 144 | Ok(res) |
139 | } | 145 | } |
140 | 146 | ||
141 | fn existing_tests(dir: &Path) -> Result<HashSet<Test>> { | 147 | fn existing_tests(dir: &Path) -> Result<HashMap<String, (PathBuf, Test)>> { |
142 | let mut res = HashSet::new(); | 148 | let mut res = HashMap::new(); |
143 | for file in fs::read_dir(dir)? { | 149 | for file in fs::read_dir(dir)? { |
144 | let file = file?; | 150 | let file = file?; |
145 | let path = file.path(); | 151 | let path = file.path(); |
146 | if path.extension().unwrap_or_default() != "rs" { | 152 | if path.extension().unwrap_or_default() != "rs" { |
147 | continue; | 153 | continue; |
148 | } | 154 | } |
149 | let name = path.file_name().unwrap().to_str().unwrap(); | 155 | let name = { |
150 | let name = name["0000_".len()..name.len() - 3].to_string(); | 156 | let file_name = path.file_name().unwrap().to_str().unwrap(); |
157 | file_name[5..file_name.len() - 3].to_string() | ||
158 | }; | ||
151 | let text = fs::read_to_string(&path)?; | 159 | let text = fs::read_to_string(&path)?; |
152 | res.insert(Test { name, text }); | 160 | let test = Test { name: name.clone(), text }; |
161 | match res.insert(name, (path, test)) { | ||
162 | Some(old) => println!("Duplicate test: {:?}", old), | ||
163 | None => (), | ||
164 | } | ||
153 | } | 165 | } |
154 | Ok(res) | 166 | Ok(res) |
155 | } | 167 | } |