From 8c5b8ac3c0ad607e78611dcae2b1743cd99e50d5 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 23 Apr 2026 23:08:58 +0900 Subject: [PATCH] address review: add unit tests for Windows execCommand paths Per review feedback, cover the four Windows branches added in the parent commit: - bare `cmd.exe` resolves via `%COMSPEC%` (with documented fallback) - bare non-cmd shell (`pwsh.exe`) is passed through unchanged - shell value with arguments (`wsl ~`) is split on whitespace - direct command is passed through without modification Co-authored-by: Claude --- src/termio/Exec.zig | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 9419ce97b..87d47807c 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -1775,3 +1775,84 @@ test "execCommand: direct command, config freed" { try testing.expectEqualStrings(result[0], "foo"); try testing.expectEqualStrings(result[1], "bar baz"); } + +test "execCommand windows: bare cmd.exe resolves via COMSPEC" { + if (comptime builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const result = try execCommand(alloc, .{ .shell = "cmd.exe" }, struct { + fn get(_: Allocator) !PasswdEntry { + return .{}; + } + }); + + try testing.expectEqual(1, result.len); + + // Expect COMSPEC if available, otherwise the documented fallback. + const expected = std.process.getEnvVarOwned(alloc, "COMSPEC") catch + try alloc.dupe(u8, "C:\\Windows\\System32\\cmd.exe"); + try testing.expectEqualStrings(expected, result[0]); +} + +test "execCommand windows: bare non-cmd shell is passed through" { + if (comptime builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const result = try execCommand(alloc, .{ .shell = "pwsh.exe" }, struct { + fn get(_: Allocator) !PasswdEntry { + return .{}; + } + }); + + try testing.expectEqual(1, result.len); + try testing.expectEqualStrings("pwsh.exe", result[0]); +} + +test "execCommand windows: shell with args is split on whitespace" { + if (comptime builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const result = try execCommand(alloc, .{ .shell = "wsl ~" }, struct { + fn get(_: Allocator) !PasswdEntry { + return .{}; + } + }); + + try testing.expectEqual(2, result.len); + try testing.expectEqualStrings("wsl", result[0]); + try testing.expectEqualStrings("~", result[1]); +} + +test "execCommand windows: direct command is passed through unchanged" { + if (comptime builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const result = try execCommand(alloc, .{ .direct = &.{ + "C:\\tools\\foo.exe", + "arg with spaces", + } }, struct { + fn get(_: Allocator) !PasswdEntry { + return .{}; + } + }); + + try testing.expectEqual(2, result.len); + try testing.expectEqualStrings("C:\\tools\\foo.exe", result[0]); + try testing.expectEqualStrings("arg with spaces", result[1]); +}