aboutsummaryrefslogtreecommitdiff
path: root/crates/tools
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-16 18:53:19 +0100
committerAleksey Kladov <[email protected]>2018-10-16 18:53:19 +0100
commitb43bcd43c604126c2b250a30cc56459be754572a (patch)
tree75f6dacf0c0a8a1c19c7f1ac8b369e6f49048115 /crates/tools
parent0b6d4983de85fcf9f84ed50c9973a1df612e5038 (diff)
fix tests
Diffstat (limited to 'crates/tools')
-rw-r--r--crates/tools/src/lib.rs35
-rw-r--r--crates/tools/src/main.rs19
-rw-r--r--crates/tools/tests/cli.rs15
3 files changed, 23 insertions, 46 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 63ede5315..444745be5 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,13 +1,15 @@
1extern crate itertools; 1extern crate itertools;
2#[macro_use]
3extern crate failure; 2extern crate failure;
3extern crate teraron;
4 4
5use itertools::Itertools;
6use std::{ 5use std::{
7 fs,
8 path::{Path, PathBuf}, 6 path::{Path, PathBuf},
9}; 7};
10 8
9use itertools::Itertools;
10
11pub use teraron::{Mode, Verify, Overwrite};
12
11pub type Result<T> = ::std::result::Result<T, failure::Error>; 13pub type Result<T> = ::std::result::Result<T, failure::Error>;
12 14
13pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; 15pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
@@ -54,22 +56,23 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
54 res 56 res
55} 57}
56 58
57pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { 59pub fn generate(mode: Mode) -> Result<()> {
58 match fs::read_to_string(path) { 60 let grammar = project_root().join(GRAMMAR);
59 Ok(ref old_contents) if old_contents == contents => { 61 let syntax_kinds = project_root().join(SYNTAX_KINDS);
60 return Ok(()); 62 let ast = project_root().join(AST);
61 } 63 teraron::generate(
62 _ => (), 64 &syntax_kinds,
63 } 65 &grammar,
64 if verify { 66 mode,
65 bail!("`{}` is not up-to-date", path.display()); 67 )?;
66 } 68 teraron::generate(
67 eprintln!("updating {}", path.display()); 69 &ast,
68 fs::write(path, contents)?; 70 &grammar,
71 mode,
72 )?;
69 Ok(()) 73 Ok(())
70} 74}
71 75
72
73pub fn project_root() -> PathBuf { 76pub fn project_root() -> PathBuf {
74 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) 77 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
75 .parent() 78 .parent()
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index 1bbc43123..965bc7729 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -13,9 +13,8 @@ use std::{
13 process::Command, 13 process::Command,
14}; 14};
15use tools::{ 15use tools::{
16 collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR, 16 collect_tests, Result, Test, generate, Mode, Overwrite, Verify,
17}; 17};
18use teraron::{Mode, Verify, Overwrite};
19 18
20const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 19const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
21const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 20const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -41,21 +40,7 @@ fn main() -> Result<()> {
41 match matches.subcommand() { 40 match matches.subcommand() {
42 ("install-code", _) => install_code_extension()?, 41 ("install-code", _) => install_code_extension()?,
43 ("gen-tests", _) => gen_tests(mode)?, 42 ("gen-tests", _) => gen_tests(mode)?,
44 ("gen-kinds", _) => { 43 ("gen-kinds", _) => generate(Overwrite)?,
45 let grammar = project_root().join(GRAMMAR);
46 let syntax_kinds = project_root().join(SYNTAX_KINDS);
47 let ast = project_root().join(AST);
48 teraron::generate(
49 &syntax_kinds,
50 &grammar,
51 mode,
52 )?;
53 teraron::generate(
54 &ast,
55 &grammar,
56 mode,
57 )?;
58 }
59 _ => unreachable!(), 44 _ => unreachable!(),
60 } 45 }
61 Ok(()) 46 Ok(())
diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs
index 16899bb5f..0bb5d15d8 100644
--- a/crates/tools/tests/cli.rs
+++ b/crates/tools/tests/cli.rs
@@ -1,23 +1,12 @@
1extern crate tools; 1extern crate tools;
2 2
3use tools::{ 3use tools::{
4 project_root, render_template, update, AST, AST_TEMPLATE, SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, 4 generate, Verify
5}; 5};
6 6
7#[test] 7#[test]
8fn verify_template_generation() { 8fn verify_template_generation() {
9 if let Err(error) = update( 9 if let Err(error) = generate(Verify) {
10 &project_root().join(SYNTAX_KINDS),
11 &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE)).unwrap(),
12 true,
13 ) {
14 panic!("{}. Please update it by running `cargo gen-kinds`", error);
15 }
16 if let Err(error) = update(
17 &project_root().join(AST),
18 &render_template(&project_root().join(AST_TEMPLATE)).unwrap(),
19 true,
20 ) {
21 panic!("{}. Please update it by running `cargo gen-kinds`", error); 10 panic!("{}. Please update it by running `cargo gen-kinds`", error);
22 } 11 }
23} 12}