Skip to content

Commit 74dce45

Browse files
committed
add more unit testing
1 parent c1356e5 commit 74dce45

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

cmd/extension_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"io"
2122
"os"
2223
"testing"
2324

@@ -29,6 +30,7 @@ import (
2930
func TestExtensionCmd(t *testing.T) {
3031
t.Run("minimum one arg", func(t *testing.T) {
3132
command := createExtensionCommand(nil)
33+
command.SetOut(io.Discard)
3234
err := command.Execute()
3335
assert.Error(t, err)
3436
})
@@ -57,6 +59,7 @@ func TestExtensionCmd(t *testing.T) {
5759
assert.NoError(t, err)
5860

5961
command := createExtensionCommand(d)
62+
command.SetOut(io.Discard)
6063
command.SetArgs([]string{"git", "--output", tmpDownloadDir, "--registry", registry})
6164
err = command.Execute()
6265
assert.NoError(t, err)

cmd/mock_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"context"
21+
"github.com/linuxsuren/api-testing/pkg/server"
22+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
23+
"github.com/stretchr/testify/assert"
24+
"io"
25+
"testing"
26+
"time"
27+
)
28+
29+
func TestMockCommand(t *testing.T) {
30+
tt := []struct {
31+
name string
32+
args []string
33+
verify func(t *testing.T, err error)
34+
}{
35+
{
36+
name: "mock",
37+
args: []string{"mock"},
38+
verify: func(t *testing.T, err error) {
39+
assert.Error(t, err)
40+
},
41+
},
42+
{
43+
name: "mock with file",
44+
args: []string{"mock", "testdata/stores.yaml", "--port=0"},
45+
verify: func(t *testing.T, err error) {
46+
assert.NoError(t, err)
47+
},
48+
},
49+
}
50+
for _, tc := range tt {
51+
t.Run(tc.name, func(t *testing.T) {
52+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, server.NewFakeHTTPServer())
53+
root.SetOut(io.Discard)
54+
root.SetArgs(tc.args)
55+
ctx, cancel := context.WithCancel(context.TODO())
56+
root.SetContext(ctx)
57+
go func() {
58+
time.Sleep(time.Second * 2)
59+
cancel()
60+
}()
61+
err := root.Execute()
62+
tc.verify(t, err)
63+
})
64+
}
65+
}

cmd/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func TestPreRunE(t *testing.T) {
270270
func TestPrinter(t *testing.T) {
271271
buf := new(bytes.Buffer)
272272
c := &cobra.Command{}
273-
c.SetOutput(buf)
273+
c.SetOut(buf)
274274

275275
println(c, nil, "foo")
276276
assert.Empty(t, buf.String())

0 commit comments

Comments
 (0)