From 851099573bf16d3f6291fb3c923e85f2702e4e0e Mon Sep 17 00:00:00 2001 From: prsabahrami Date: Tue, 10 Sep 2024 14:20:32 -0400 Subject: [PATCH] Fixes --- crates/deno_task_shell/src/shell/test.rs | 70 +++++++++++++++++++ .../deno_task_shell/src/shell/test_builder.rs | 26 +++---- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/crates/deno_task_shell/src/shell/test.rs b/crates/deno_task_shell/src/shell/test.rs index 226e7b6..90eae50 100644 --- a/crates/deno_task_shell/src/shell/test.rs +++ b/crates/deno_task_shell/src/shell/test.rs @@ -236,6 +236,76 @@ async fn sequential_lists() { .run() .await; } +#[tokio::test] +async fn pipeline() { + TestBuilder::new() + .command(r#"echo 1 | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1\n") + .run() + .await; + + TestBuilder::new() + .command(r#"echo 1 | echo 2 && echo 3"#) + .assert_stdout("2\n3\n") + .run() + .await; + + TestBuilder::new() + .command(r#"echo $(sleep 0.1 && echo 2 & echo 1) | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1 2\n") + .run() + .await; + + TestBuilder::new() + .command(r#"echo 2 | echo 1 | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1\n") + .run() + .await; + + TestBuilder::new() + .command(r#"deno eval 'console.log(1); console.error(2);' | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1\n") + .assert_stderr("2\n") + .run() + .await; + + // stdout and stderr pipeline + + TestBuilder::new() + .command(r#"deno eval 'console.log(1); console.error(2);' |& deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1\n2\n") + .run() + .await; + + TestBuilder::new() + // add bit of a delay while outputting stdout so that it doesn't race with stderr + .command(r#"deno eval 'console.log(1); console.error(2);' | deno eval 'setTimeout(async () => { await Deno.stdin.readable.pipeTo(Deno.stderr.writable) }, 10)' |& deno eval 'await Deno.stdin.readable.pipeTo(Deno.stderr.writable)'"#) + // still outputs 2 because the first command didn't pipe stderr + .assert_stderr("2\n1\n") + .run() + .await; + + // |& pipeline should still pipe stdout + TestBuilder::new() + .command(r#"echo 1 |& deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)'"#) + .assert_stdout("1\n") + .run() + .await; + + // pipeline with redirect + TestBuilder::new() + .command(r#"echo 1 | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable)' > output.txt"#) + .assert_file_equals("output.txt", "1\n") + .run() + .await; + + // pipeline with stderr redirect + TestBuilder::new() + .command(r#"echo 1 | deno eval 'await Deno.stdin.readable.pipeTo(Deno.stderr.writable)' 2> output.txt"#) + .assert_file_equals("output.txt", "1\n") + .run() + .await; +} #[tokio::test] async fn redirects_input() { diff --git a/crates/deno_task_shell/src/shell/test_builder.rs b/crates/deno_task_shell/src/shell/test_builder.rs index 9dece43..387cc26 100644 --- a/crates/deno_task_shell/src/shell/test_builder.rs +++ b/crates/deno_task_shell/src/shell/test_builder.rs @@ -149,19 +149,6 @@ impl TestBuilder { self } - pub fn assert_file_equals( - &mut self, - path: &str, - file_text: &str, - ) -> &mut Self { - self.ensure_temp_dir(); - self.assertions.push(TestAssertion::FileTextEquals( - path.to_string(), - file_text.to_string(), - )); - self - } - pub fn file(&mut self, path: &str, text: &str) -> &mut Self { let temp_dir = self.get_temp_dir(); fs::write(temp_dir.cwd.join(path), text).unwrap(); @@ -199,6 +186,19 @@ impl TestBuilder { self } + pub fn assert_file_equals( + &mut self, + path: &str, + file_text: &str, + ) -> &mut Self { + self.ensure_temp_dir(); + self.assertions.push(TestAssertion::FileTextEquals( + path.to_string(), + file_text.to_string(), + )); + self + } + pub async fn run(&mut self) { let list = parse(&self.command).unwrap(); let cwd = if let Some(temp_dir) = &self.temp_dir {