From fabd96552f3f690db8ff7651f6354057139f0557 Mon Sep 17 00:00:00 2001 From: Velikiy Kirill Date: Mon, 15 Sep 2025 14:41:35 +0300 Subject: [PATCH] Implement write_to_stdin() in std::os::process (#2482) * SubProcess: Add write_to_stdin * SubProcess: Change unit test for windows support --- lib/std/os/subprocess.c3 | 6 ++++++ test/unit/stdlib/os/subprocess.c3 | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/std/os/subprocess.c3 b/lib/std/os/subprocess.c3 index a9be419c3..a9e610b49 100644 --- a/lib/std/os/subprocess.c3 +++ b/lib/std/os/subprocess.c3 @@ -531,3 +531,9 @@ fn bool? SubProcess.is_running(&self) return false; $endif } + +fn usz? SubProcess.write_to_stdin(&self, char[] buffer) +{ + if (!self.stdin_file) return FAILED_TO_OPEN_STDIN?; + return os::native_fwrite(self.stdin_file, buffer); +} diff --git a/test/unit/stdlib/os/subprocess.c3 b/test/unit/stdlib/os/subprocess.c3 index 3265a3f6d..841457a58 100644 --- a/test/unit/stdlib/os/subprocess.c3 +++ b/test/unit/stdlib/os/subprocess.c3 @@ -142,4 +142,22 @@ fn void test_inherit_stdio_redirect() assert(stdout_bytes == 0, "Should read 0 bytes from stdout"); assert(stderr_bytes == 0, "Should read 0 bytes from stderr"); -} \ No newline at end of file +} + +fn void test_write_to_stdin() @test { +$if env::WIN32: + SubProcess? subprocess = process::create({`powershell`, `-C`, `Read-Host`}, {.inherit_environment = true}); +$else + SubProcess? subprocess = process::create({"tee"}, {.read_async = true}); +$endif + test::eq(@ok(subprocess), true); + usz? write = subprocess!!.write_to_stdin(")"); + test::eq(@ok(write), true); + test::eq(write!!, 1); + char buf; + test::eq(@ok(subprocess!!.join()), true); + usz? read = subprocess!!.read_stdout(&buf, 1); + test::eq(@ok(read), true); + test::eq(read!!, 1); + test::eq(buf, ')'); +}