diff options
Diffstat (limited to 'crates/ra_tools/src/lib.rs')
-rw-r--r-- | crates/ra_tools/src/lib.rs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index de8c472d1..c02dee953 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs | |||
@@ -11,8 +11,6 @@ use std::{ | |||
11 | 11 | ||
12 | use itertools::Itertools; | 12 | use itertools::Itertools; |
13 | 13 | ||
14 | pub use teraron::{Mode, Overwrite, Verify}; | ||
15 | |||
16 | pub use self::codegen::generate; | 14 | pub use self::codegen::generate; |
17 | 15 | ||
18 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; | 16 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; |
@@ -26,6 +24,13 @@ pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs.te | |||
26 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; | 24 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; |
27 | const TOOLCHAIN: &str = "stable"; | 25 | const TOOLCHAIN: &str = "stable"; |
28 | 26 | ||
27 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
28 | pub enum Mode { | ||
29 | Overwrite, | ||
30 | Verify, | ||
31 | } | ||
32 | pub use Mode::*; | ||
33 | |||
29 | #[derive(Debug)] | 34 | #[derive(Debug)] |
30 | pub struct Test { | 35 | pub struct Test { |
31 | pub name: String, | 36 | pub name: String, |
@@ -222,7 +227,7 @@ pub fn gen_tests(mode: Mode) -> Result<()> { | |||
222 | tests_dir.join(file_name) | 227 | tests_dir.join(file_name) |
223 | } | 228 | } |
224 | }; | 229 | }; |
225 | teraron::update(&path, &test.text, mode)?; | 230 | update(&path, &test.text, mode)?; |
226 | } | 231 | } |
227 | Ok(()) | 232 | Ok(()) |
228 | } | 233 | } |
@@ -306,3 +311,20 @@ fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test | |||
306 | } | 311 | } |
307 | Ok(res) | 312 | Ok(res) |
308 | } | 313 | } |
314 | |||
315 | /// A helper to update file on disk if it has changed. | ||
316 | /// With verify = false, | ||
317 | pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | ||
318 | match fs::read_to_string(path) { | ||
319 | Ok(ref old_contents) if old_contents == contents => { | ||
320 | return Ok(()); | ||
321 | } | ||
322 | _ => (), | ||
323 | } | ||
324 | if mode == Verify { | ||
325 | Err(format!("`{}` is not up-to-date", path.display()))?; | ||
326 | } | ||
327 | eprintln!("updating {}", path.display()); | ||
328 | fs::write(path, contents)?; | ||
329 | Ok(()) | ||
330 | } | ||