forked from google/go-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
225 lines (205 loc) · 7.14 KB
/
e2e_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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main_test
import (
"bytes"
"errors"
"flag"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
"github.com/google/go-cmp/cmp"
)
var update = flag.Bool("update", false, "update golden files")
func TestReportCommandE2E(t *testing.T) {
tests := []struct {
workdir string
args []string // additional arguments to pass to report command.
goldenFilePath string
}{
{"testdata/modules/hello01", nil, "licenses.csv"},
{"testdata/modules/cli02", nil, "licenses.csv"},
{"testdata/modules/vendored03", nil, "licenses.csv"},
{"testdata/modules/replace04", nil, "licenses.csv"},
{"testdata/modules/complex", nil, "licenses.csv"},
{"testdata/modules/hello01", []string{"--template", "licenses.tpl"}, "licenses.md"},
{"testdata/modules/template01", []string{"--template", "licenses.tpl"}, "licenses.md"},
}
originalWorkDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chdir(originalWorkDir) })
// This builds go-licenses CLI to temporary dir.
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
goLicensesPath := filepath.Join(tempDir, "go-licenses")
cmd := exec.Command("go", "build", "-o", goLicensesPath)
_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}
t.Logf("Built go-licenses binary in %s.", goLicensesPath)
for _, tt := range tests {
t.Run(tt.workdir, func(t *testing.T) {
err := os.Chdir(filepath.Join(originalWorkDir, tt.workdir))
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("go", "mod", "download")
log, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("downloading go modules:\n%s", string(log))
}
args := append([]string{"report", "."}, tt.args...)
cmd = exec.Command(goLicensesPath, args...)
// Capture stderr to buffer.
var stderr bytes.Buffer
cmd.Stderr = &stderr
t.Logf("%s $ go-licenses report .", tt.workdir)
output, err := cmd.Output()
if err != nil {
t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
t.Fatalf("running go-licenses report: %s. Full log shown above.", err)
}
got := string(output)
if *update {
err := os.WriteFile(tt.goldenFilePath, output, 0600)
if err != nil {
t.Fatalf("writing golden file: %s", err)
}
}
goldenBytes, err := os.ReadFile(tt.goldenFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
t.Fatalf("reading golden file: %s. Create a golden file by running `go test --update .`", err)
}
t.Fatalf("reading golden file: %s", err)
}
golden := string(goldenBytes)
if got != golden {
t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
t.Fatalf("result of go-licenses report does not match the golden file.\n"+
"Diff -golden +got:\n%s\n"+
"Update the golden by running `go test --update .`",
cmp.Diff(golden, got))
}
})
}
}
func TestCheckCommandE2E(t *testing.T) {
tests := []struct {
workdir string
args []string // additional arguments to pass to report command.
goldenFilePath string
wantExitCode int
}{
{"testdata/modules/hello01", nil, "output-check-forbidden.txt", 0},
{"testdata/modules/hello01", []string{"--disallowed_types=forbidden,notice"}, "output-check-notice-forbidden.txt", 1},
{"testdata/modules/cli02", nil, "output-check-forbidden.txt", 0},
{"testdata/modules/cli02", []string{"--disallowed_types=forbidden,notice"}, "output-check-notice-forbidden.txt", 1},
{"testdata/modules/cli02", []string{"--allowed_licenses=Apache-2.0"}, "output-check-license-names-1.txt", 1},
{"testdata/modules/cli02", []string{"--allowed_licenses=Apache-2.0,MIT"}, "output-check-license-names-2.txt", 1},
{"testdata/modules/cli02", []string{"--allowed_licenses= Apache-2.0, MIT"}, "output-check-license-names-2.txt", 1},
{"testdata/modules/nolicense05", nil, "output-check.txt", 1},
{"testdata/modules/complex", nil, "output-check-complex.txt", 0},
}
originalWorkDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chdir(originalWorkDir) })
// This builds go-licenses CLI to temporary dir.
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
goLicensesPath := filepath.Join(tempDir, "go-licenses")
cmd := exec.Command("go", "build", "-o", goLicensesPath)
_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}
t.Logf("Built go-licenses binary in %s.", goLicensesPath)
for _, tt := range tests {
t.Run(tt.workdir, func(t *testing.T) {
err := os.Chdir(filepath.Join(originalWorkDir, tt.workdir))
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("go", "mod", "download")
log, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("downloading go modules:\n%s", string(log))
}
// Extra flags to disable logging.
// Logs are not deterministic for this golden test.
args := append([]string{"--logtostderr=false", "--stderrthreshold=10", "check", "."}, tt.args...)
cmd = exec.Command(goLicensesPath, args...)
// Capture stderr to buffer.
var stderr bytes.Buffer
cmd.Stderr = &stderr
exitCode := 0
t.Logf("%s $ go-licenses check .", tt.workdir)
output, err := cmd.Output()
if err != nil {
exitCode = -1
if exitError, ok := err.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
}
}
if len(output) != 0 {
t.Fatalf("unexpected output running go-licenses check: %s.", string(output))
}
if exitCode != tt.wantExitCode {
t.Fatalf("unexpected exit code running go-licenses check, expected %d but got %d", tt.wantExitCode, exitCode)
}
got := filterOutput(stderr.String())
if *update {
err := os.WriteFile(tt.goldenFilePath, []byte(got), 0600)
if err != nil {
t.Fatalf("writing golden file: %s", err)
}
}
goldenBytes, err := os.ReadFile(tt.goldenFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
t.Fatalf("reading golden file: %s. Create a golden file by running `go test --update .`", err)
}
t.Fatalf("reading golden file: %s", err)
}
golden := string(goldenBytes)
if got != golden {
t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
t.Fatalf("result of go-licenses check does not match the golden file.\n"+
"Diff -golden +got:\n%s\n"+
"Update the golden by running `go test --update .`",
cmp.Diff(golden, got))
}
})
}
}
func filterOutput(output string) string {
output = regexp.MustCompile(`(?m)W\d+.*\n`).
ReplaceAllString(output, "")
output = regexp.MustCompile(`(?m)^/.*\n`).
ReplaceAllString(output, "")
return output
}