Skip to content

Commit 9da617b

Browse files
committed
chore(ut): add unit test cases.
1 parent c740640 commit 9da617b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

cli/ls_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2019 voidint <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
// this software and associated documentation files (the "Software"), to deal in
5+
// the Software without restriction, including without limitation the rights to
6+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
// the Software, and to permit persons to whom the Software is furnished to do so,
8+
// subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
20+
package cli
21+
22+
import (
23+
"errors"
24+
"io/fs"
25+
"os"
26+
"path/filepath"
27+
"testing"
28+
29+
"github.com/stretchr/testify/assert"
30+
)
31+
32+
func TestListLocalVersions(t *testing.T) {
33+
tmpDir := t.TempDir()
34+
35+
os.Mkdir(filepath.Join(tmpDir, "1.25rc1"), 0755)
36+
os.Mkdir(filepath.Join(tmpDir, "1.24rc3"), 0755)
37+
os.Mkdir(filepath.Join(tmpDir, "1.18beta2"), 0755)
38+
os.Mkdir(filepath.Join(tmpDir, "1.19beta1"), 0755)
39+
os.Mkdir(filepath.Join(tmpDir, "1.24.4"), 0755)
40+
os.Mkdir(filepath.Join(tmpDir, "1.20.14"), 0755)
41+
os.Mkdir(filepath.Join(tmpDir, "1.20"), 0755)
42+
os.Mkdir(filepath.Join(tmpDir, "invalid_version"), 0755)
43+
os.WriteFile(filepath.Join(tmpDir, "not_a_directory"), []byte{}, 0644)
44+
45+
tests := []struct {
46+
name string
47+
dirPath string
48+
vnames []string
49+
checkErr func(error) bool
50+
}{
51+
{"Directory does not exist", "/not/exist/path", nil, func(err error) bool {
52+
var pathError *fs.PathError
53+
return errors.As(err, &pathError)
54+
}},
55+
56+
{"The file path is not a directory", filepath.Join(tmpDir, "not_a_directory"), nil, func(err error) bool {
57+
var pathError *fs.PathError
58+
return errors.As(err, &pathError)
59+
}},
60+
61+
{"正常版本目录", tmpDir, []string{"1.18beta2", "1.19beta1", "1.20", "1.20.14", "1.24rc3", "1.24.4", "1.25rc1"}, func(err error) bool {
62+
return err == nil
63+
}},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
got, err := listLocalVersions(tt.dirPath)
69+
assert.True(t, tt.checkErr(err))
70+
71+
var actualNames []string
72+
for _, v := range got {
73+
actualNames = append(actualNames, v.Name())
74+
}
75+
assert.Equal(t, tt.vnames, actualNames)
76+
})
77+
}
78+
}

cli/use_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2019 voidint <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
// this software and associated documentation files (the "Software"), to deal in
5+
// the Software without restriction, including without limitation the rights to
6+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
// the Software, and to permit persons to whom the Software is furnished to do so,
8+
// subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
20+
package cli
21+
22+
import (
23+
"testing"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestGetGoDirective(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
input []byte
32+
expected string
33+
}{
34+
{
35+
name: "normal go directive",
36+
input: []byte("module github.com/voidint/g\n\ngo 1.20\n"),
37+
expected: "1.20",
38+
},
39+
{
40+
name: "normal go directive",
41+
input: []byte("module github.com/voidint/g\n\ngo 1.24.0\n"),
42+
expected: "1.24.0",
43+
},
44+
{
45+
name: "normal go directive",
46+
input: []byte("module github.com/voidint/g\n\ngo 1.24.4\n"),
47+
expected: "1.24.4",
48+
},
49+
{
50+
name: "normal go directive",
51+
input: []byte("module github.com/voidint/g\n\ngo 1.25rc1\n"),
52+
expected: "1.25rc1",
53+
},
54+
{
55+
name: "no go directive",
56+
input: []byte("module github.com/voidint/g\n"),
57+
expected: "",
58+
},
59+
{
60+
name: "malformed directive",
61+
input: []byte("go1.20"),
62+
expected: "",
63+
},
64+
{
65+
name: "empty input",
66+
input: []byte(""),
67+
expected: "",
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
got := getGoDirective(tt.input)
74+
assert.Equal(t, tt.expected, got)
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)