aboutsummaryrefslogtreecommitdiff
path: root/tools/src
diff options
context:
space:
mode:
Diffstat (limited to 'tools/src')
-rw-r--r--tools/src/lib.rs14
-rw-r--r--tools/src/main.rs44
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;
3use std::hash; 3use std::hash;
4use itertools::Itertools; 4use itertools::Itertools;
5 5
6#[derive(Debug, Eq)] 6#[derive(Debug)]
7pub struct Test { 7pub struct Test {
8 pub name: String, 8 pub name: String,
9 pub text: String, 9 pub text: String,
10} 10}
11 11
12impl PartialEq for Test {
13 fn eq(&self, other: &Test) -> bool {
14 self.name.eq(&other.name)
15 }
16}
17
18impl hash::Hash for Test {
19 fn hash<H: hash::Hasher>(&self, state: &mut H) {
20 self.name.hash(state)
21 }
22}
23
24pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { 12pub 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]
9extern crate commandspec; 9extern crate commandspec;
10 10
11use std::{collections::{HashSet, HashMap}, fs, path::Path}; 11use std::{collections::{HashMap}, fs, path::{Path, PathBuf}};
12use clap::{App, Arg, SubCommand}; 12use clap::{App, Arg, SubCommand};
13use tools::{collect_tests, Test}; 13use 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
120fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> { 126fn 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
141fn existing_tests(dir: &Path) -> Result<HashSet<Test>> { 147fn 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}