diff options
author | Aleksey Kladov <[email protected]> | 2018-07-31 13:30:11 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-31 13:30:11 +0100 |
commit | 580340d1b879a434302d314ef6cd9f4cae0cd57d (patch) | |
tree | bf13870f08fd24a0d1ab557e14231246d8ea1b0a /tools/src | |
parent | 346f6e4f7d364b009d0feb66162314abfd06c81b (diff) |
mut bindings
Diffstat (limited to 'tools/src')
-rw-r--r-- | tools/src/lib.rs | 14 | ||||
-rw-r--r-- | tools/src/main.rs | 44 |
2 files changed, 29 insertions, 29 deletions
diff --git a/tools/src/lib.rs b/tools/src/lib.rs index 7a0de3e3c..21a9468bc 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs | |||
@@ -3,24 +3,12 @@ extern crate itertools; | |||
3 | use std::hash; | 3 | use std::hash; |
4 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | 5 | ||
6 | #[derive(Debug, Eq)] | 6 | #[derive(Debug)] |
7 | pub struct Test { | 7 | pub struct Test { |
8 | pub name: String, | 8 | pub name: String, |
9 | pub text: String, | 9 | pub text: String, |
10 | } | 10 | } |
11 | 11 | ||
12 | impl PartialEq for Test { | ||
13 | fn eq(&self, other: &Test) -> bool { | ||
14 | self.name.eq(&other.name) | ||
15 | } | ||
16 | } | ||
17 | |||
18 | impl hash::Hash for Test { | ||
19 | fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
20 | self.name.hash(state) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | 12 | pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { |
25 | let mut res = vec![]; | 13 | let mut res = vec![]; |
26 | let prefix = "// "; | 14 | let prefix = "// "; |
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 | } |