Skip to content

Commit d1e2110

Browse files
committed
stdin commander tests
1 parent 38cc8ca commit d1e2110

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package exec_commander
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestStdinCommander_Run_Success(t *testing.T) {
9+
c := NewStdinCommander("hello")
10+
11+
err := c.Run("/bin/cat")
12+
if err != nil {
13+
t.Fatalf("Run returned error: %v", err)
14+
}
15+
}
16+
17+
func TestStdinCommander_Run_Error(t *testing.T) {
18+
c := NewStdinCommander("")
19+
20+
// nonexistent command → guaranteed error
21+
err := c.Run("/definitely-not-exists")
22+
if err == nil {
23+
t.Fatalf("expected error, got nil")
24+
}
25+
}
26+
27+
func TestStdinCommander_Output(t *testing.T) {
28+
c := NewStdinCommander("hello\n")
29+
30+
out, err := c.Output("/bin/cat")
31+
if err != nil {
32+
t.Fatalf("Output returned error: %v", err)
33+
}
34+
35+
if string(out) != "hello\n" {
36+
t.Fatalf("unexpected output: %q", out)
37+
}
38+
}
39+
40+
func TestStdinCommander_Output_Error(t *testing.T) {
41+
c := NewStdinCommander("")
42+
43+
_, err := c.Output("/definitely-not-exists")
44+
if err == nil {
45+
t.Fatalf("expected error, got nil")
46+
}
47+
}
48+
49+
func TestStdinCommander_CombinedOutput(t *testing.T) {
50+
c := NewStdinCommander("hello\n")
51+
52+
out, err := c.CombinedOutput("/bin/cat")
53+
if err != nil {
54+
t.Fatalf("CombinedOutput returned error: %v", err)
55+
}
56+
57+
if string(out) != "hello\n" {
58+
t.Fatalf("unexpected combined output: %q", out)
59+
}
60+
}
61+
62+
func TestStdinCommander_CombinedOutput_Error(t *testing.T) {
63+
c := NewStdinCommander("")
64+
65+
out, err := c.CombinedOutput("/definitely-not-exists")
66+
if err == nil {
67+
t.Fatalf("expected error, got nil (output=%q)", out)
68+
}
69+
}
70+
71+
func TestStdinCommander_StdinConsumed(t *testing.T) {
72+
c := NewStdinCommander("once")
73+
74+
out1, err := c.Output("/bin/cat")
75+
if err != nil {
76+
t.Fatalf("first output failed: %v", err)
77+
}
78+
79+
out2, err := c.Output("/bin/cat")
80+
if err != nil {
81+
t.Fatalf("second output failed: %v", err)
82+
}
83+
84+
if string(out1) != "once" {
85+
t.Fatalf("unexpected first output: %q", out1)
86+
}
87+
if len(bytes.TrimSpace(out2)) != 0 {
88+
t.Fatalf("expected empty stdin on second run, got %q", out2)
89+
}
90+
}

0 commit comments

Comments
 (0)