Skip to content

Commit 512e8b5

Browse files
Add tests for sample CMD examples (#2404)
* added tests for sample cmd application * reverted errors in tests * removed unreverted changes * remove test temp files * removed redundant tests --------- Co-authored-by: Aryan Mehrotra <aryanmehrotra2000@gmail.com>
1 parent 1a91dd7 commit 512e8b5

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

examples/sample-cmd/main_test.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,76 @@ func TestCMDRun_ProgressContextCancelled(t *testing.T) {
9898
ctx, cancel := context.WithCancel(context.Background())
9999
cancel()
100100

101-
// add an already canceled context
101+
// Create a proper context with logger to avoid nil pointer dereference
102+
container := &container.Container{
103+
Logger: logging.NewMockLogger(logging.ERROR),
104+
}
105+
102106
res, err := progress(&gofr.Context{
103-
Context: ctx,
104-
Request: cmd.NewRequest([]string{"command", "spinner"}),
105-
Container: &container.Container{
106-
Logger: logging.NewMockLogger(logging.ERROR),
107-
},
108-
Out: terminal.New(),
107+
Context: ctx,
108+
Request: cmd.NewRequest([]string{"command", "progress"}),
109+
Container: container,
110+
Out: terminal.New(),
111+
ContextLogger: *logging.NewContextLogger(ctx, container.Logger),
109112
})
110113

111114
assert.Empty(t, res)
112115
assert.ErrorIs(t, err, context.Canceled)
113116
}
117+
118+
// TestCMDRunWithInvalidCommand tests that invalid commands return appropriate error
119+
func TestCMDRunWithInvalidCommand(t *testing.T) {
120+
expErr := "No Command Found!\n"
121+
os.Args = []string{"command", "invalid"}
122+
output := testutil.StderrOutputForFunc(main)
123+
124+
assert.Equal(t, expErr, output, "TEST Failed.\n")
125+
}
126+
127+
// TestCMDRunWithEmptyParams tests the params command with empty name parameter
128+
func TestCMDRunWithEmptyParams(t *testing.T) {
129+
expResp := "Hello !\n"
130+
os.Args = []string{"command", "params", "-name="}
131+
output := testutil.StdoutOutputForFunc(main)
132+
133+
assert.Contains(t, output, expResp, "TEST Failed.\n")
134+
}
135+
136+
// TestCMDRunHelpCommand tests the help functionality
137+
func TestCMDRunHelpCommand(t *testing.T) {
138+
testCases := []struct {
139+
args []string
140+
expected []string
141+
}{
142+
{[]string{"command", "help"}, []string{"Available commands:", "hello", "params", "spinner", "progress"}},
143+
{[]string{"command", "-h"}, []string{"Available commands:", "hello", "params", "spinner", "progress"}},
144+
{[]string{"command", "--help"}, []string{"Available commands:", "hello", "params", "spinner", "progress"}},
145+
}
146+
147+
for i, tc := range testCases {
148+
os.Args = tc.args
149+
output := testutil.StdoutOutputForFunc(main)
150+
151+
for _, expected := range tc.expected {
152+
assert.Contains(t, output, expected, "TEST[%d] Failed. Expected to contain: %s\n", i, expected)
153+
}
154+
}
155+
}
156+
157+
// TestCMDRunHelpForSpecificCommand tests help for specific commands
158+
func TestCMDRunHelpForSpecificCommand(t *testing.T) {
159+
testCases := []struct {
160+
args []string
161+
expected string
162+
}{
163+
{[]string{"command", "hello", "-h"}, "hello world option"},
164+
{[]string{"command", "hello", "--help"}, "hello world option"},
165+
}
166+
167+
for i, tc := range testCases {
168+
os.Args = tc.args
169+
output := testutil.StdoutOutputForFunc(main)
170+
171+
assert.Contains(t, output, tc.expected, "TEST[%d] Failed.\n", i)
172+
}
173+
}

0 commit comments

Comments
 (0)