Add {:#} format printing to justfile for --show

This commit is contained in:
Casey Rodarmor 2016-10-28 16:32:13 -07:00
parent a8a5c342e7
commit 01df3d5e4a
6 changed files with 67 additions and 27 deletions

View File

@ -1,7 +1,7 @@
test-all: test test-integration test-all: test test-integration
test: test:
cargo test --lib --test integration cargo test --lib
test-integration: build test-integration: build
cargo test --test integration cargo test --test integration

7
notes
View File

@ -2,11 +2,10 @@ notes
----- -----
- integration testing - 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) . run app with command line options and test full output (stderr and stdout)
. exercise all features and all command line options . 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 . underline problem token in error messages
- figure out argument passing: - figure out argument passing:
@ -61,6 +60,8 @@ notes
enhancements: enhancements:
- save result of commands in variables - save result of commands in variables
- multi line strings
- raw strings
- iteration: {{x for x in y}} - iteration: {{x for x in y}}
- allow calling recipes in a justfile in a different directory: - allow calling recipes in a justfile in a different directory:
. just ../foo # ../justfile:foo . just ../foo # ../justfile:foo

View File

@ -108,7 +108,7 @@ pub fn app() {
if let Some(name) = matches.value_of("show") { if let Some(name) = matches.value_of("show") {
match justfile.get(name) { match justfile.get(name) {
Some(recipe) => { Some(recipe) => {
warn!("{}", recipe); println!("{}", recipe);
process::exit(0); process::exit(0);
} }
None => die!("justfile contains no recipe \"{}\"", name) None => die!("justfile contains no recipe \"{}\"", name)

View File

@ -120,21 +120,45 @@ c: b";
); );
} }
// #[test] #[test]
// fn show() { fn select() {
// let text = let text =
// r#"hello = "foo" "b:
// recipe: @echo b
// echo {{hello}}"#; a:
// integration_test( @echo a
// "show", d:
// &["--show", "recipe"], @echo d
// text, c:
// 0, @echo c";
// "foo\n", 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] #[test]
fn status() { fn status() {

View File

@ -238,10 +238,17 @@ impl<'a> Display for Recipe<'a> {
if j == 0 { if j == 0 {
try!(write!(f, " ")); try!(write!(f, " "));
} }
match *piece { if f.alternate() {
Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)), match *piece {
Fragment::Expression{ref expression, value: None} => try!(write!(f, "{}{} # ? {}", "{{", expression, "}}")), Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)),
Fragment::Expression{ref expression, value: Some(ref string)} => try!(write!(f, "{}{} # \"{}\"{}", "{{", expression, string, "}}")), 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() { 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> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut items = self.recipes.len() + self.assignments.len(); let mut items = self.recipes.len() + self.assignments.len();
for (name, expression) in &self.assignments { 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; items -= 1;
if items != 0 { if items != 0 {
try!(write!(f, "\n")); try!(write!(f, "\n"));
} }
} }
for recipe in self.recipes.values() { for recipe in self.recipes.values() {
try!(write!(f, "{}", recipe)); if f.alternate() {
try!(write!(f, "{:#}", recipe));
} else {
try!(write!(f, "{}", recipe));
}
items -= 1; items -= 1;
if items != 0 { if items != 0 {
try!(write!(f, "\n")); try!(write!(f, "\n"));

View File

@ -62,7 +62,7 @@ fn parse_success(text: &str) -> Justfile {
fn parse_summary(input: &str, output: &str) { fn parse_summary(input: &str, output: &str) {
let justfile = parse_success(input); let justfile = parse_success(input);
let s = justfile.to_string(); let s = format!("{:#}", justfile);
if s != output { if s != output {
println!("got:\n\"{}\"\n", s); println!("got:\n\"{}\"\n", s);
println!("\texpected:\n\"{}\"", output); println!("\texpected:\n\"{}\"", output);