Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 10, 2024
1 parent 705a95d commit 8510995
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
70 changes: 70 additions & 0 deletions crates/deno_task_shell/src/shell/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 13 additions & 13 deletions crates/deno_task_shell/src/shell/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8510995

Please sign in to comment.