Skip to content

Commit 2957faa

Browse files
authored
test:refactor(cmd): test verifies behavior from cobra.Command layer
Previous iteration was running method directly
1 parent f138b06 commit 2957faa

File tree

1 file changed

+55
-58
lines changed

1 file changed

+55
-58
lines changed

pkg/kubernetes-mcp-server/cmd/root_test.go

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,85 +23,82 @@ func captureOutput(f func() error) (string, error) {
2323
return string(out), err
2424
}
2525

26-
func TestVersion(t *testing.T) {
27-
in := &bytes.Buffer{}
26+
func testStream() (genericiooptions.IOStreams, *bytes.Buffer) {
2827
out := &bytes.Buffer{}
29-
errOut := io.Discard
30-
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
31-
rootCmd.Version = true
32-
rootCmd.Run()
33-
if out.String() != "0.0.0\n" {
34-
t.Fatalf("Expected version 0.0.0, got %s", out.String())
28+
return genericiooptions.IOStreams{
29+
In: &bytes.Buffer{},
30+
Out: out,
31+
ErrOut: io.Discard,
32+
}, out
33+
}
34+
35+
func TestVersion(t *testing.T) {
36+
ioStreams, out := testStream()
37+
rootCmd := NewMCPServer(ioStreams)
38+
rootCmd.SetArgs([]string{"--version"})
39+
if err := rootCmd.Execute(); out.String() != "0.0.0\n" {
40+
t.Fatalf("Expected version 0.0.0, got %s %v", out.String(), err)
3541
}
3642
}
3743

3844
func TestProfile(t *testing.T) {
45+
t.Run("available", func(t *testing.T) {
46+
ioStreams, _ := testStream()
47+
rootCmd := NewMCPServer(ioStreams)
48+
rootCmd.SetArgs([]string{"--help"})
49+
o, err := captureOutput(rootCmd.Execute) // --help doesn't use logger/klog, cobra prints directly to stdout
50+
if !strings.Contains(o, "MCP profile to use (one of: full) ") {
51+
t.Fatalf("Expected all available profiles, got %s %v", o, err)
52+
}
53+
})
3954
t.Run("default", func(t *testing.T) {
40-
in := &bytes.Buffer{}
41-
out := &bytes.Buffer{}
42-
errOut := io.Discard
43-
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
44-
rootCmd.Version = true
45-
rootCmd.LogLevel = 1
46-
rootCmd.Complete()
47-
rootCmd.Run()
48-
if !strings.Contains(out.String(), "- Profile: full") {
49-
t.Fatalf("Expected profile 'full', got %s", out)
55+
ioStreams, out := testStream()
56+
rootCmd := NewMCPServer(ioStreams)
57+
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
58+
if err := rootCmd.Execute(); !strings.Contains(out.String(), "- Profile: full") {
59+
t.Fatalf("Expected profile 'full', got %s %v", out, err)
5060
}
5161
})
5262
}
5363

5464
func TestListOutput(t *testing.T) {
5565
t.Run("available", func(t *testing.T) {
56-
in := &bytes.Buffer{}
57-
out := io.Discard
58-
errOut := io.Discard
59-
rootCmd := NewMCPServer(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
66+
ioStreams, _ := testStream()
67+
rootCmd := NewMCPServer(ioStreams)
6068
rootCmd.SetArgs([]string{"--help"})
61-
o, err := captureOutput(rootCmd.Execute)
69+
o, err := captureOutput(rootCmd.Execute) // --help doesn't use logger/klog, cobra prints directly to stdout
6270
if !strings.Contains(o, "Output format for resource list operations (one of: yaml, table)") {
63-
t.Fatalf("Expected all available outputs, got %s %v", out, err)
71+
t.Fatalf("Expected all available outputs, got %s %v", o, err)
6472
}
6573
})
6674
t.Run("defaults to table", func(t *testing.T) {
67-
in := &bytes.Buffer{}
68-
out := &bytes.Buffer{}
69-
errOut := io.Discard
70-
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
71-
rootCmd.Version = true
72-
rootCmd.LogLevel = 1
73-
rootCmd.Complete()
74-
rootCmd.Run()
75-
if !strings.Contains(out.String(), "- ListOutput: table") {
76-
t.Fatalf("Expected list-output 'table', got %s", out)
75+
ioStreams, out := testStream()
76+
rootCmd := NewMCPServer(ioStreams)
77+
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
78+
if err := rootCmd.Execute(); !strings.Contains(out.String(), "- ListOutput: table") {
79+
t.Fatalf("Expected list-output 'table', got %s %v", out, err)
7780
}
7881
})
7982
}
8083

81-
func TestDefaultReadOnly(t *testing.T) {
82-
in := &bytes.Buffer{}
83-
out := &bytes.Buffer{}
84-
errOut := io.Discard
85-
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
86-
rootCmd.Version = true
87-
rootCmd.LogLevel = 1
88-
rootCmd.Complete()
89-
rootCmd.Run()
90-
if !strings.Contains(out.String(), " - Read-only mode: false") {
91-
t.Fatalf("Expected read-only mode false, got %s", out)
92-
}
84+
func TestReadOnly(t *testing.T) {
85+
t.Run("defaults to false", func(t *testing.T) {
86+
ioStreams, out := testStream()
87+
rootCmd := NewMCPServer(ioStreams)
88+
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
89+
if err := rootCmd.Execute(); !strings.Contains(out.String(), " - Read-only mode: false") {
90+
t.Fatalf("Expected read-only mode false, got %s %v", out, err)
91+
}
92+
})
9393
}
9494

95-
func TestDefaultDisableDestructive(t *testing.T) {
96-
in := &bytes.Buffer{}
97-
out := &bytes.Buffer{}
98-
errOut := io.Discard
99-
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
100-
rootCmd.Version = true
101-
rootCmd.LogLevel = 1
102-
rootCmd.Complete()
103-
rootCmd.Run()
104-
if !strings.Contains(out.String(), " - Disable destructive tools: false") {
105-
t.Fatalf("Expected disable destructive false, got %s", out)
106-
}
95+
func TestDisableDestructive(t *testing.T) {
96+
t.Run("defaults to false", func(t *testing.T) {
97+
ioStreams, out := testStream()
98+
rootCmd := NewMCPServer(ioStreams)
99+
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
100+
if err := rootCmd.Execute(); !strings.Contains(out.String(), " - Disable destructive tools: false") {
101+
t.Fatalf("Expected disable destructive false, got %s %v", out, err)
102+
}
103+
})
107104
}

0 commit comments

Comments
 (0)