From 01df3d5e4aa659455aafdeede0c05a1bed35c872 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 28 Oct 2016 16:32:13 -0700 Subject: [PATCH] Add {:#} format printing to justfile for --show --- justfile | 2 +- notes | 7 +++--- src/app.rs | 2 +- src/integration.rs | 54 +++++++++++++++++++++++++++++++++------------- src/lib.rs | 27 +++++++++++++++++------ src/tests.rs | 2 +- 6 files changed, 67 insertions(+), 27 deletions(-) diff --git a/justfile b/justfile index 4a9e9e0..f23a44f 100644 --- a/justfile +++ b/justfile @@ -1,7 +1,7 @@ test-all: test test-integration test: - cargo test --lib --test integration + cargo test --lib test-integration: build cargo test --test integration diff --git a/notes b/notes index fa48b98..86a958b 100644 --- a/notes +++ b/notes @@ -2,11 +2,10 @@ notes ----- - integration testing - . --show should not display variable and expression values + . get value of variable --evaluate --variable + . --info that prints whole justfile . run app with command line options and test full output (stderr and stdout) . exercise all features and all command line options - . test that a few error messages are correct - . test full output . underline problem token in error messages - figure out argument passing: @@ -61,6 +60,8 @@ notes enhancements: - save result of commands in variables +- multi line strings +- raw strings - iteration: {{x for x in y}} - allow calling recipes in a justfile in a different directory: . just ../foo # ../justfile:foo diff --git a/src/app.rs b/src/app.rs index 241aa28..6e621f2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -108,7 +108,7 @@ pub fn app() { if let Some(name) = matches.value_of("show") { match justfile.get(name) { Some(recipe) => { - warn!("{}", recipe); + println!("{}", recipe); process::exit(0); } None => die!("justfile contains no recipe \"{}\"", name) diff --git a/src/integration.rs b/src/integration.rs index 1376358..16a0aa3 100644 --- a/src/integration.rs +++ b/src/integration.rs @@ -120,21 +120,45 @@ c: b"; ); } -// #[test] -// fn show() { -// let text = -// r#"hello = "foo" -// recipe: -// echo {{hello}}"#; -// integration_test( -// "show", -// &["--show", "recipe"], -// text, -// 0, -// "foo\n", -// "", -// ); -// } +#[test] +fn select() { + let text = +"b: + @echo b +a: + @echo a +d: + @echo d +c: + @echo c"; + integration_test( + "select", + &["d", "c"], + text, + 0, + "d\nc\n", + "", + ); +} + +#[test] +fn show() { + let text = +r#"hello = "foo" +bar = hello + hello +recipe: + echo {{hello + "bar" + bar}}"#; + integration_test( + "show", + &["--show", "recipe"], + text, + 0, + r#"recipe: + echo {{hello + "bar" + bar}} +"#, + "", + ); +} #[test] fn status() { diff --git a/src/lib.rs b/src/lib.rs index ab11826..fdce1ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -238,10 +238,17 @@ impl<'a> Display for Recipe<'a> { if j == 0 { try!(write!(f, " ")); } - match *piece { - Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)), - Fragment::Expression{ref expression, value: None} => try!(write!(f, "{}{} # ? {}", "{{", expression, "}}")), - Fragment::Expression{ref expression, value: Some(ref string)} => try!(write!(f, "{}{} # \"{}\"{}", "{{", expression, string, "}}")), + if f.alternate() { + match *piece { + Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)), + Fragment::Expression{ref expression, value: None} => try!(write!(f, "{}{} # ? {}", "{{", expression, "}}")), + Fragment::Expression{ref expression, value: Some(ref string)} => try!(write!(f, "{}{} # \"{}\"{}", "{{", expression, string, "}}")), + } + } else { + match *piece { + Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)), + Fragment::Expression{ref expression, ..} => try!(write!(f, "{}{}{}", "{{", expression, "}}")), + } } } if i + 1 < self.lines.len() { @@ -630,14 +637,22 @@ impl<'a> Display for Justfile<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let mut items = self.recipes.len() + self.assignments.len(); for (name, expression) in &self.assignments { - try!(write!(f, "{} = {} # \"{}\"", name, expression, self.values.get(name).unwrap())); + if f.alternate() { + try!(write!(f, "{} = {} # \"{}\"", name, expression, self.values.get(name).unwrap())); + } else { + try!(write!(f, "{} = {}", name, expression)); + } items -= 1; if items != 0 { try!(write!(f, "\n")); } } for recipe in self.recipes.values() { - try!(write!(f, "{}", recipe)); + if f.alternate() { + try!(write!(f, "{:#}", recipe)); + } else { + try!(write!(f, "{}", recipe)); + } items -= 1; if items != 0 { try!(write!(f, "\n")); diff --git a/src/tests.rs b/src/tests.rs index 924b446..d7f9196 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -62,7 +62,7 @@ fn parse_success(text: &str) -> Justfile { fn parse_summary(input: &str, output: &str) { let justfile = parse_success(input); - let s = justfile.to_string(); + let s = format!("{:#}", justfile); if s != output { println!("got:\n\"{}\"\n", s); println!("\texpected:\n\"{}\"", output);