Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/bashkit/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3087,9 +3087,16 @@ impl Interpreter {
.collect();
let prev_funcname = self.arrays.insert("FUNCNAME".to_string(), funcname_arr);

// Forward pipeline stdin to function body
let prev_pipeline_stdin = self.pipeline_stdin.take();
self.pipeline_stdin = stdin;

// Execute function body
let mut result = self.execute_command(&func_def.body).await?;

// Restore previous pipeline stdin
self.pipeline_stdin = prev_pipeline_stdin;

// Pop call frame and function counter
self.call_stack.pop();
self.counters.pop_function();
Expand Down
31 changes: 31 additions & 0 deletions crates/bashkit/tests/issue_274_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Regression test for #274: Pipeline stdin not forwarded to user-defined functions

use bashkit::Bash;
use std::path::Path;

#[tokio::test]
async fn issue_274_pipeline_stdin_to_function() {
let mut bash = Bash::new();
let r = bash
.exec("to_upper() { tr '[:lower:]' '[:upper:]'; }\necho hello | to_upper")
.await
.unwrap();
assert_eq!(r.stdout.trim(), "HELLO");
}

#[tokio::test]
async fn issue_274_pipeline_stdin_to_sourced_function() {
let mut bash = Bash::new();
let fs = bash.fs();
fs.write_file(
Path::new("/lib.sh"),
b"to_upper() { tr '[:lower:]' '[:upper:]'; }",
)
.await
.unwrap();
let r = bash
.exec("source /lib.sh\necho hello world | to_upper")
.await
.unwrap();
assert_eq!(r.stdout.trim(), "HELLO WORLD");
}
Loading