]> git.lizzy.rs Git - rust.git/commitdiff
fix tests
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 16 Oct 2018 17:53:19 +0000 (20:53 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 16 Oct 2018 17:53:19 +0000 (20:53 +0300)
crates/tools/src/lib.rs
crates/tools/src/main.rs
crates/tools/tests/cli.rs

index 63ede53151a403feba7f32dff2c0dbc4fa330c56..444745be5dc4ce7292a31fb34dcd568ef02525d6 100644 (file)
@@ -1,13 +1,15 @@
 extern crate itertools;
-#[macro_use]
 extern crate failure;
+extern crate teraron;
 
-use itertools::Itertools;
 use std::{
-    fs,
     path::{Path, PathBuf},
 };
 
+use itertools::Itertools;
+
+pub use teraron::{Mode, Verify, Overwrite};
+
 pub type Result<T> = ::std::result::Result<T, failure::Error>;
 
 pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
@@ -54,22 +56,23 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
     res
 }
 
-pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
-    match fs::read_to_string(path) {
-        Ok(ref old_contents) if old_contents == contents => {
-            return Ok(());
-        }
-        _ => (),
-    }
-    if verify {
-        bail!("`{}` is not up-to-date", path.display());
-    }
-    eprintln!("updating {}", path.display());
-    fs::write(path, contents)?;
+pub fn generate(mode: Mode) -> Result<()> {
+    let grammar = project_root().join(GRAMMAR);
+    let syntax_kinds = project_root().join(SYNTAX_KINDS);
+    let ast = project_root().join(AST);
+    teraron::generate(
+        &syntax_kinds,
+        &grammar,
+        mode,
+    )?;
+    teraron::generate(
+        &ast,
+        &grammar,
+        mode,
+    )?;
     Ok(())
 }
 
-
 pub fn project_root() -> PathBuf {
     Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
         .parent()
index 1bbc43123e7688be5d1504eca4ac92ae94def4e6..965bc772967a5a76cf33e5f898643649e440c8b0 100644 (file)
@@ -13,9 +13,8 @@
     process::Command,
 };
 use tools::{
-    collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR,
+    collect_tests, Result, Test, generate, Mode, Overwrite, Verify,
 };
-use teraron::{Mode, Verify, Overwrite};
 
 const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
 const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -41,21 +40,7 @@ fn main() -> Result<()> {
     match matches.subcommand() {
         ("install-code", _) => install_code_extension()?,
         ("gen-tests", _) => gen_tests(mode)?,
-        ("gen-kinds", _) => {
-            let grammar = project_root().join(GRAMMAR);
-            let syntax_kinds = project_root().join(SYNTAX_KINDS);
-            let ast = project_root().join(AST);
-            teraron::generate(
-                &syntax_kinds,
-                &grammar,
-                mode,
-            )?;
-            teraron::generate(
-                &ast,
-                &grammar,
-                mode,
-            )?;
-        }
+        ("gen-kinds", _) => generate(Overwrite)?,
         _ => unreachable!(),
     }
     Ok(())
index 16899bb5f434d83092bc5d36847e920365949b41..0bb5d15d8e183f892c1f7ec0e16ec8fe45dba0e3 100644 (file)
@@ -1,23 +1,12 @@
 extern crate tools;
 
 use tools::{
-    project_root, render_template, update, AST, AST_TEMPLATE, SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE,
+    generate, Verify
 };
 
 #[test]
 fn verify_template_generation() {
-    if let Err(error) = update(
-        &project_root().join(SYNTAX_KINDS),
-        &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE)).unwrap(),
-        true,
-    ) {
-        panic!("{}. Please update it by running `cargo gen-kinds`", error);
-    }
-    if let Err(error) = update(
-        &project_root().join(AST),
-        &render_template(&project_root().join(AST_TEMPLATE)).unwrap(),
-        true,
-    ) {
+    if let Err(error) = generate(Verify) {
         panic!("{}. Please update it by running `cargo gen-kinds`", error);
     }
 }