-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlicense_test.go
125 lines (119 loc) · 2.58 KB
/
license_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
117
118
119
120
121
122
123
124
125
package licensecheck
import (
"errors"
"io/ioutil"
"math"
"testing"
"github.com/golang/mock/gomock"
"github.com/vulsio/licensecheck/shared/mock"
)
func TestScan(t *testing.T) {
ctrl := gomock.NewController(t)
tests := []struct {
name string
in string
result string
confidence float64
wantErr error
pkgName string
version string
scanType int
}{
{
name: "GitHub",
in: "./testdata/github/MIT_sample.txt",
result: "MIT",
confidence: 1,
pkgName: "test",
scanType: GitHub,
},
{
name: "Go",
in: "./testdata/go/input1.html",
result: "MIT",
confidence: 1,
pkgName: "test",
version: "v1.0",
scanType: Go,
},
{
name: "Java",
in: "./testdata/java/input1.xml",
result: "Apache-2.0",
confidence: 0.911111,
pkgName: "test",
version: "v1.0",
scanType: Java,
},
{
name: "node",
in: "./testdata/nodejs/input1.json",
result: "MIT",
confidence: 1,
pkgName: "test",
version: "v1.0",
scanType: Nodejs,
},
{
name: "Python",
in: "./testdata/python/input1.json",
result: "MIT",
confidence: 1,
pkgName: "test",
version: "v1.0",
scanType: Python,
},
{
name: "Ruby",
in: "./testdata/ruby/input1.json",
result: "MIT",
confidence: 1,
pkgName: "test",
version: "v1.0",
scanType: Ruby,
},
{
name: "Rust",
in: "./testdata/rust/input1.json",
result: "MIT",
confidence: 1,
pkgName: "test",
version: "v1.0",
scanType: Rust,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, err := ioutil.ReadFile(tt.in)
if err != nil {
t.Fatal(err)
}
sc := new(Scanner)
cl := mock.NewMockCrawler(ctrl)
cl.EXPECT().Crawl(gomock.Any()).Return(b, nil)
sc.Crawler = cl
result, confidence, err := sc.Scan(tt.pkgName, tt.version, tt.scanType)
if err != nil && !errors.Is(err, tt.wantErr) {
t.Fatal(err)
}
if result != tt.result {
t.Errorf("want: %s, got: %s", tt.result, result)
}
if math.Abs(confidence-tt.confidence) >= 1e-6 {
t.Errorf("want: %f, got: %f", tt.confidence, confidence)
}
})
}
}
func TestScanInvalidArguments(t *testing.T) {
result, confidence, err := new(Scanner).Scan("", "", 999)
if !errors.Is(err, ErrUnKnownScanType) {
t.Error(err)
}
if result != "unknown" {
t.Errorf("want: unknown, got: %s", result)
}
if confidence != 0 {
t.Errorf("want: 0, got: %f", confidence)
}
}