forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golist_test.go
116 lines (109 loc) · 2.32 KB
/
golist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package swag
import (
"context"
"errors"
"fmt"
"go/build"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListPackages(t *testing.T) {
cases := []struct {
name string
args []string
searchDir string
except error
}{
{
name: "errorArgs",
args: []string{"-abc"},
searchDir: "testdata/golist",
except: fmt.Errorf("exit status 2"),
},
{
name: "normal",
args: []string{"-deps"},
searchDir: "testdata/golist",
except: nil,
},
{
name: "list error",
args: []string{"-deps"},
searchDir: "testdata/golist_not_exist",
except: errors.New("searchDir not exist"),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, err := listPackages(context.TODO(), c.searchDir, nil, c.args...)
if c.except != nil {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
func TestGetAllGoFileInfoFromDepsByList(t *testing.T) {
p := New(ParseUsingGoList(true))
pwd, err := os.Getwd()
assert.NoError(t, err)
cases := []struct {
name string
buildPackage *build.Package
ignoreInternal bool
except error
}{
{
name: "normal",
buildPackage: &build.Package{
Name: "main",
ImportPath: "github.com/swaggo/swag/testdata/golist",
Dir: "testdata/golist",
GoFiles: []string{"main.go"},
CgoFiles: []string{"api/api.go"},
},
except: nil,
},
{
name: "ignore internal",
buildPackage: &build.Package{
Goroot: true,
},
ignoreInternal: true,
except: nil,
},
{
name: "gofiles error",
buildPackage: &build.Package{
Dir: "testdata/golist_not_exist",
GoFiles: []string{"main.go"},
},
except: errors.New("file not exist"),
},
{
name: "cgofiles error",
buildPackage: &build.Package{
Dir: "testdata/golist_not_exist",
CgoFiles: []string{"main.go"},
},
except: errors.New("file not exist"),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.ignoreInternal {
p.ParseInternal = false
}
c.buildPackage.Dir = filepath.Join(pwd, c.buildPackage.Dir)
err := p.getAllGoFileInfoFromDepsByList(c.buildPackage, ParseModels)
if c.except != nil {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}