Skip to content

Commit 2df7606

Browse files
committed
init setup
1 parent 5678372 commit 2df7606

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

cli/cli_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cli
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/coder/serpent"
8+
)
9+
10+
// MockPTY provides a simple mock for PTY-like testing
11+
// This is a simplified version inspired by coder/coder's ptytest.
12+
type MockPTY struct {
13+
t *testing.T
14+
stdout strings.Builder
15+
stderr strings.Builder
16+
}
17+
18+
// NewMockPTY creates a new mock PTY for testing
19+
func NewMockPTY(t *testing.T) *MockPTY {
20+
return &MockPTY{t: t}
21+
}
22+
23+
func (m *MockPTY) Attach(inv *serpent.Invocation) {
24+
inv.Stdout = &m.stdout
25+
inv.Stderr = &m.stderr
26+
}
27+
28+
func (m *MockPTY) Stdout() string {
29+
return m.stdout.String()
30+
}
31+
32+
func (m *MockPTY) Stderr() string {
33+
return m.stderr.String()
34+
}
35+
36+
func (m *MockPTY) Clear() {
37+
m.stdout = strings.Builder{}
38+
m.stderr = strings.Builder{}
39+
}
40+
41+
func TestPtySetupWorks(t *testing.T) {
42+
cmd := NewCommand()
43+
inv := cmd.Invoke("--help")
44+
45+
pty := NewMockPTY(t)
46+
pty.Attach(inv)
47+
48+
if err := inv.Run(); err != nil {
49+
t.Fatalf("could not run with simple --help arg: %v", err)
50+
}
51+
52+
// TODO: A snapshot test setup is usually a good idea for CLI messages like this
53+
if !strings.Contains(pty.Stdout(), "Monitor and restrict HTTP/HTTPS requests from processes") {
54+
t.Fatalf("expected help to display summary, got: %s", pty.Stdout())
55+
}
56+
}
57+
58+
func TestCurlGithub(t *testing.T) {
59+
cmd := NewCommand()
60+
inv := cmd.Invoke("--allow", "\"github.com\"", "--", "curl", "https://github.com")
61+
62+
pty := NewMockPTY(t)
63+
pty.Attach(inv)
64+
65+
if err := inv.Run(); err != nil {
66+
t.Fatalf("error curling github: %v", err)
67+
}
68+
}
69+

0 commit comments

Comments
 (0)