Skip to content

Commit fffb582

Browse files
authored
test: Add unit tests for config operations, common utilities (#295)
* test: Add unit tests for config operations, common utilities, and CLI commands, including test data. Signed-off-by: SharanRP <z8903830@gmail.com> * chore: Remove trailing blank line from ops_test.go. Signed-off-by: SharanRP <z8903830@gmail.com> --------- Signed-off-by: SharanRP <z8903830@gmail.com>
1 parent 7649136 commit fffb582

File tree

5 files changed

+887
-0
lines changed

5 files changed

+887
-0
lines changed

pkg/cmd/version/version_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package version
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestVersionCmd_Properties(t *testing.T) {
8+
t.Run("Use field is correct", func(t *testing.T) {
9+
if VersionCmd.Use != "version" {
10+
t.Errorf("VersionCmd.Use = %q, want %q", VersionCmd.Use, "version")
11+
}
12+
})
13+
14+
t.Run("Short description is set", func(t *testing.T) {
15+
if VersionCmd.Short == "" {
16+
t.Error("VersionCmd.Short should not be empty")
17+
}
18+
expected := "Displays the version of litmusctl"
19+
if VersionCmd.Short != expected {
20+
t.Errorf("VersionCmd.Short = %q, want %q", VersionCmd.Short, expected)
21+
}
22+
})
23+
24+
t.Run("Command is runnable", func(t *testing.T) {
25+
if VersionCmd.Run == nil {
26+
t.Error("VersionCmd.Run should not be nil")
27+
}
28+
})
29+
}
30+
31+
func TestUpdateCmd_Properties(t *testing.T) {
32+
t.Run("Use field is correct", func(t *testing.T) {
33+
if UpdateCmd.Use != "update" {
34+
t.Errorf("UpdateCmd.Use = %q, want %q", UpdateCmd.Use, "update")
35+
}
36+
})
37+
38+
t.Run("Short description is set", func(t *testing.T) {
39+
if UpdateCmd.Short == "" {
40+
t.Error("UpdateCmd.Short should not be empty")
41+
}
42+
expected := "Changes the version of litmusctl"
43+
if UpdateCmd.Short != expected {
44+
t.Errorf("UpdateCmd.Short = %q, want %q", UpdateCmd.Short, expected)
45+
}
46+
})
47+
48+
t.Run("Requires exactly one argument", func(t *testing.T) {
49+
if UpdateCmd.Args == nil {
50+
t.Error("UpdateCmd.Args should not be nil")
51+
}
52+
})
53+
54+
t.Run("Command is runnable", func(t *testing.T) {
55+
if UpdateCmd.Run == nil {
56+
t.Error("UpdateCmd.Run should not be nil")
57+
}
58+
})
59+
}
60+
61+
func TestVersionCmd_Subcommands(t *testing.T) {
62+
t.Run("UpdateCmd is subcommand of VersionCmd", func(t *testing.T) {
63+
found := false
64+
for _, cmd := range VersionCmd.Commands() {
65+
if cmd.Use == "update" {
66+
found = true
67+
break
68+
}
69+
}
70+
if !found {
71+
t.Error("UpdateCmd should be a subcommand of VersionCmd")
72+
}
73+
})
74+
75+
t.Run("VersionCmd has exactly one subcommand", func(t *testing.T) {
76+
cmdCount := len(VersionCmd.Commands())
77+
if cmdCount != 1 {
78+
t.Errorf("VersionCmd has %d subcommands, want 1", cmdCount)
79+
}
80+
})
81+
}
82+
83+
func TestVersionCmd_Help(t *testing.T) {
84+
t.Run("Command has help available", func(t *testing.T) {
85+
if VersionCmd.UsageString() == "" {
86+
t.Error("Usage string should not be empty")
87+
}
88+
})
89+
}
90+
91+
func TestUpdateCmd_ArgsValidation(t *testing.T) {
92+
tests := []struct {
93+
name string
94+
args []string
95+
wantErr bool
96+
}{
97+
{
98+
name: "no arguments",
99+
args: []string{},
100+
wantErr: true,
101+
},
102+
{
103+
name: "one argument (valid)",
104+
args: []string{"0.23.0"},
105+
wantErr: false,
106+
},
107+
{
108+
name: "two arguments",
109+
args: []string{"0.23.0", "extra"},
110+
wantErr: true,
111+
},
112+
}
113+
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
err := UpdateCmd.Args(UpdateCmd, tt.args)
117+
if (err != nil) != tt.wantErr {
118+
t.Errorf("UpdateCmd.Args() with %v error = %v, wantErr %v", tt.args, err, tt.wantErr)
119+
}
120+
})
121+
}
122+
}

0 commit comments

Comments
 (0)