/
license_test.go
226 lines (191 loc) · 5.49 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package license
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestNewLicense(t *testing.T) {
l := New("MyLicense", "Some license text.")
if l.Type != "MyLicense" {
t.Fatalf("bad license type: %s", l.Type)
}
if l.Text != "Some license text." {
t.Fatalf("bad license text: %s", l.Text)
}
}
func TestNewFromFile(t *testing.T) {
lf := filepath.Join("fixtures", "licenses", "MIT")
lh, err := os.Open(lf)
if err != nil {
t.Fatalf("err: %s", err)
}
defer lh.Close()
licenseText, err := ioutil.ReadAll(lh)
if err != nil {
t.Fatalf("err: %s", err)
}
l, err := NewFromFile(lf)
if err != nil {
t.Fatalf("err: %s", err)
}
if l.Type != "MIT" {
t.Fatalf("unexpected license type: %s", l.Type)
}
if l.Text != string(licenseText) {
t.Fatalf("unexpected license text: %s", l.Text)
}
if l.File != lf {
t.Fatalf("unexpected file path: %s", l.File)
}
// Fails properly if the file doesn't exist
if _, err := NewFromFile("/tmp/go-license-nonexistent"); err == nil {
t.Fatalf("expected error loading non-existent file")
}
// Fails properly if license type from file is not guessable
f, err := ioutil.TempFile("", "go-license")
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
defer os.Remove(f.Name())
f.WriteString("No license data")
if _, err := NewFromFile(f.Name()); err == nil {
t.Fatalf("expected error guessing license type from non-license file")
}
}
func TestNewFromDir(t *testing.T) {
d, err := ioutil.TempDir("", "go-license")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(d)
// Fails properly if the directory contains no license files
if _, err := NewFromDir(d); err == nil {
t.Fatalf("expected error loading empty directory")
}
fPath := filepath.Join(d, "LICENSE")
f, err := os.Create(fPath)
if err != nil {
t.Fatalf("err: %s", err)
}
lh, err := os.Open(filepath.Join("fixtures", "licenses", "MIT"))
if err != nil {
t.Fatalf("err: %s", err)
}
defer lh.Close()
licenseText, err := ioutil.ReadAll(lh)
if err != nil {
t.Fatalf("err: %s", err)
}
if _, err := f.Write(licenseText); err != nil {
t.Fatalf("err: %s", err)
}
l, err := NewFromDir(d)
if err != nil {
t.Fatalf("err: %s", err)
}
if l.Type != "MIT" {
t.Fatalf("unexpected license type: %s", l.Type)
}
if l.Text != string(licenseText) {
t.Fatalf("unexpected license text: %s", l.Text)
}
if l.File != fPath {
t.Fatalf("unexpected file path: %s", l.File)
}
// Fails properly if the directory does not exist
if _, err := NewFromDir("go-license-nonexistent"); err == nil {
t.Fatalf("expected error loading non-existent directory")
}
// Fails properly if the directory specified is actually a file
if _, err := NewFromDir(fPath); err == nil {
t.Fatalf("expected error loading file as directory")
}
}
func TestLicenseRecognized(t *testing.T) {
// Known licenses are recognized
l := New("MIT", "The MIT License (MIT)")
if !l.Recognized() {
t.Fatalf("license was not recognized")
}
// Unknown licenses are not recognized
l = New("None", "No license text")
if l.Recognized() {
t.Fatalf("fake license was recognized")
}
}
func TestLicenseTypes(t *testing.T) {
for _, ltype := range KnownLicenses {
file := filepath.Join("fixtures", "licenses", ltype)
fh, err := os.Open(file)
if err != nil {
t.Fatalf("err: %s", err)
}
lbytes, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatalf("err :%s", err)
}
ltext := string(lbytes)
l := New("", ltext)
if err := l.GuessType(); err != nil {
t.Fatalf("err: %s", err)
}
if l.Type != ltype {
t.Fatalf("\nexpected: %s\ngot: %s", ltype, l.Type)
}
}
}
func TestLicenseTypes_Abbreviated(t *testing.T) {
// Abbreviated Apache 2.0 license is recognized
l := New("", "http://www.apache.org/licenses/LICENSE-2.0")
if err := l.GuessType(); err != nil {
t.Fatalf("err: %s", err)
}
if l.Type != LicenseApache20 {
t.Fatalf("\nexpected: %s\ngot: %s", LicenseApache20, l.Type)
}
}
func TestMatchLicenseFile(t *testing.T) {
// should always return the original test file (mixed case), and
// not the license file version (typically upper case)
licenses := []string{"copying.txt", "COPYING", "License"}
tests := []struct {
files []string
want []string
}{
{[]string{".", "junk", "COPYING"}, []string{"COPYING"}},
{[]string{"junk", "copy"}, []string{}},
{[]string{"LICENSE", "foo"}, []string{"LICENSE"}},
{[]string{"LICENSE.junk", "foo"}, []string{}},
{[]string{"something", "Copying.txt"}, []string{"Copying.txt"}},
{[]string{"COPYING", "junk", "Copying.txt"}, []string{"COPYING", "Copying.txt"}},
}
for pos, tt := range tests {
got := matchLicenseFile(licenses, tt.files)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Test %d: expected %v, got %v", pos, tt.want, got)
}
}
}
func TestGetLicenseFile(t *testing.T) {
// should always return the original test file (mixed case), and
// not the license file version (typically upper case)
licenses := []string{"copying.txt", "COPYING", "License"}
tests := []struct {
files []string
want string
err error
}{
{[]string{".", "junk", "COPYING"}, "COPYING", nil}, // 1 match
{[]string{"junk", "copy"}, "", ErrNoLicenseFile}, // 0 match
{[]string{"COPYING", "junk", "Copying.txt"}, "", ErrMultipleLicenses}, // 2 match
}
for pos, tt := range tests {
got, err := getLicenseFile(licenses, tt.files)
if got != tt.want || !reflect.DeepEqual(err, tt.err) {
t.Errorf("Test %d: expected %q with error '%v', got %q with '%v'", pos, tt.want, tt.err, got, err)
}
}
}