From 3db9053455a1fcc6f95a8f65a819462792a717bf Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 7 Jan 2022 04:01:21 -0500 Subject: more tests --- src/parse.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/parse.rs b/src/parse.rs index f838879..5193769 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -288,6 +288,25 @@ impl std::error::Error for Error { mod test { use super::*; + impl From for RedirectTarget { + fn from(fd: std::os::unix::io::RawFd) -> Self { + Self::Fd(fd) + } + } + + impl From for RedirectTarget { + fn from(path: std::path::PathBuf) -> Self { + Self::File(path) + } + } + + #[allow(clippy::fallible_impl_from)] + impl From<&str> for RedirectTarget { + fn from(path: &str) -> Self { + Self::File(path.try_into().unwrap()) + } + } + macro_rules! c { ($input_string:expr, $($pipelines:expr),*) => { Commands { @@ -311,14 +330,38 @@ mod test { Exe { exe: $word, args: vec![], - redirects: vec![], // todo + redirects: vec![], } }; ($word:expr, $($args:expr),*) => { Exe { exe: $word, args: vec![$($args),*], - redirects: vec![], // todo + redirects: vec![], + } + }; + ($word:expr ; $($redirects:expr),*) => { + Exe { + exe: $word, + args: vec![], + redirects: vec![$($redirects),*], + } + }; + ($word:expr, $($args:expr),* ; $($redirects:expr),*) => { + Exe { + exe: $word, + args: vec![$($args),*], + redirects: vec![$($redirects),*], + } + }; + } + + macro_rules! r { + ($from:literal, $to:literal, $dir:ident) => { + Redirect { + from: $from, + to: $to.into(), + dir: Direction::$dir, } }; } @@ -399,6 +442,16 @@ mod test { " foo # this is a comment", c!("foo", p!("foo", e!(w!("foo")))) ); + parse_eq!("foo#comment", c!("foo", p!("foo", e!(w!("foo"))))); + parse_eq!( + "foo;bar|baz;quux#comment", + c!( + "foo;bar|baz;quux", + p!("foo", e!(w!("foo"))), + p!("bar|baz", e!(w!("bar")), e!(w!("baz"))), + p!("quux", e!(w!("quux"))) + ) + ); parse_eq!( "foo | bar ", c!( @@ -434,4 +487,36 @@ mod test { ) ); } + + #[test] + fn test_redirect() { + parse_eq!( + "foo > bar", + c!( + "foo > bar", + p!("foo > bar", e!(w!("foo") ; r!(1, "bar", Out))) + ) + ); + parse_eq!( + "foo /dev/null 2>&1", + c!( + "foo > /dev/null 2>&1", + p!( + "foo > /dev/null 2>&1", + e!(w!("foo") ; r!(1, "/dev/null", Out), r!(2, 1, Out)) + ) + ) + ); + parse_eq!( + "foo >>bar", + c!( + "foo >>bar", + p!("foo >>bar", e!(w!("foo") ; r!(1, "bar", Append))) + ) + ); + } } -- cgit v1.2.3-54-g00ecf