Debian Codegraph @

cmd/spdx-validate/main_test.go

raw symbols SCIP
1 package main
2
3 import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 )
10
11 // --- Tests for single expression on stdin ---
12
13 func TestValidateExpressions_SingleValid(t *testing.T) {
14 tests := []string{
15 "MIT",
16 "Apache-2.0",
17 "BSD-3-Clause",
18 "Apache-2.0 OR MIT",
19 "MIT AND ISC",
20 "GPL-3.0-only WITH Classpath-exception-2.0",
21 }
22 for _, expr := range tests {
23 t.Run(expr, func(t *testing.T) {
24 r := strings.NewReader(expr + "\n")
25 var w bytes.Buffer
26 ok, err := validateExpressions(r, &w)
27 if err != nil {
28 t.Fatalf("unexpected error: %v", err)
29 }
30 if !ok {
31 t.Errorf("expected valid, got invalid; stderr: %s", w.String())
32 }
33 if w.Len() != 0 {
34 t.Errorf("expected no stderr output, got: %s", w.String())
35 }
36 })
37 }
38 }
39
40 func TestValidateExpressions_SingleInvalid(t *testing.T) {
41 tests := []string{
42 "BOGUS-LICENSE",
43 "NOT-A-REAL-ID",
44 "MIT ANDOR Apache-2.0",
45 }
46 for _, expr := range tests {
47 t.Run(expr, func(t *testing.T) {
48 r := strings.NewReader(expr + "\n")
49 var w bytes.Buffer
50 ok, err := validateExpressions(r, &w)
51 if err == nil {
52 t.Fatal("expected error for single invalid expression, got nil")
53 }
54 if ok {
55 t.Error("expected invalid, got valid")
56 }
57 if !strings.Contains(w.String(), "invalid SPDX expression") {
58 t.Errorf("expected error message in output, got: %s", w.String())
59 }
60 if !strings.Contains(w.String(), expr) {
61 t.Errorf("expected expression %q in output, got: %s", expr, w.String())
62 }
63 })
64 }
65 }
66
67 // --- Tests for multiple expressions ---
68
69 func TestValidateExpressions_AllValid(t *testing.T) {
70 input := "MIT\nApache-2.0\nBSD-3-Clause OR MIT\n"
71 r := strings.NewReader(input)
72 var w bytes.Buffer
73 ok, err := validateExpressions(r, &w)
74 if err != nil {
75 t.Fatalf("unexpected error: %v", err)
76 }
77 if !ok {
78 t.Errorf("expected all valid, got invalid; stderr: %s", w.String())
79 }
80 if w.Len() != 0 {
81 t.Errorf("expected no stderr output, got: %s", w.String())
82 }
83 }
84
85 func TestValidateExpressions_SomeInvalid(t *testing.T) {
86 input := "MIT\nNOT-A-LICENSE\nApache-2.0\nALSO-BOGUS\n"
87 r := strings.NewReader(input)
88 var w bytes.Buffer
89 ok, err := validateExpressions(r, &w)
90 if err != nil {
91 t.Fatalf("unexpected error: %v", err)
92 }
93 if ok {
94 t.Error("expected invalid result, got valid")
95 }
96 output := w.String()
97 if !strings.Contains(output, `"NOT-A-LICENSE"`) {
98 t.Errorf("expected NOT-A-LICENSE in output, got: %s", output)
99 }
100 if !strings.Contains(output, `"ALSO-BOGUS"`) {
101 t.Errorf("expected ALSO-BOGUS in output, got: %s", output)
102 }
103 if !strings.Contains(output, "line 2:") {
104 t.Errorf("expected 'line 2:' in output, got: %s", output)
105 }
106 if !strings.Contains(output, "line 4:") {
107 t.Errorf("expected 'line 4:' in output, got: %s", output)
108 }
109 if !strings.Contains(output, "2 of 4 expressions failed") {
110 t.Errorf("expected summary in output, got: %s", output)
111 }
112 }
113
114 func TestValidateExpressions_AllInvalid(t *testing.T) {
115 input := "BOGUS-1\nBOGUS-2\n"
116 r := strings.NewReader(input)
117 var w bytes.Buffer
118 ok, err := validateExpressions(r, &w)
119 if err == nil {
120 t.Fatal("expected error when all expressions are invalid, got nil")
121 }
122 if ok {
123 t.Error("expected ok=false")
124 }
125 if !strings.Contains(err.Error(), "no valid expressions found") {
126 t.Errorf("expected 'no valid expressions found' error, got: %v", err)
127 }
128 }
129
130 func TestValidateExpressions_EmptyFile(t *testing.T) {
131 r := strings.NewReader("")
132 var w bytes.Buffer
133 ok, err := validateExpressions(r, &w)
134 if err == nil {
135 t.Fatal("expected error for empty file, got nil")
136 }
137 if ok {
138 t.Error("expected ok=false for empty file")
139 }
140 if !strings.Contains(err.Error(), "no valid expressions found") {
141 t.Errorf("expected 'no valid expressions found' error, got: %v", err)
142 }
143 }
144
145 func TestValidateExpressions_SkipsBlankLines(t *testing.T) {
146 input := "\nMIT\n\n\nApache-2.0\n\n"
147 r := strings.NewReader(input)
148 var w bytes.Buffer
149 ok, err := validateExpressions(r, &w)
150 if err != nil {
151 t.Fatalf("unexpected error: %v", err)
152 }
153 if !ok {
154 t.Errorf("expected all valid, got invalid; stderr: %s", w.String())
155 }
156 }
157
158 // --- Integration test using a temp file ---
159
160 func TestValidateExpressions_FromTempFile(t *testing.T) {
161 dir := t.TempDir()
162 path := filepath.Join(dir, "licenses.txt")
163
164 content := "MIT\nApache-2.0\nBSD-2-Clause\n"
165 if err := os.WriteFile(path, []byte(content), 0600); err != nil {
166 t.Fatalf("failed to write temp file: %v", err)
167 }
168
169 // #nosec G304 -- path is created within t.TempDir for this test.
170 f, err := os.Open(path)
171 if err != nil {
172 t.Fatalf("failed to open temp file: %v", err)
173 }
174 defer func() {
175 _ = f.Close()
176 }()
177
178 var w bytes.Buffer
179 ok, err := validateExpressions(f, &w)
180 if err != nil {
181 t.Fatalf("unexpected error: %v", err)
182 }
183 if !ok {
184 t.Errorf("expected all valid from file, got invalid; stderr: %s", w.String())
185 }
186 }
187
188 func TestValidateExpressions_FromTempFileWithFailures(t *testing.T) {
189 dir := t.TempDir()
190 path := filepath.Join(dir, "licenses.txt")
191
192 content := "MIT\nINVALID-1\nApache-2.0\nINVALID-2\nBSD-2-Clause\n"
193 if err := os.WriteFile(path, []byte(content), 0600); err != nil {
194 t.Fatalf("failed to write temp file: %v", err)
195 }
196
197 // #nosec G304 -- path is created within t.TempDir for this test.
198 f, err := os.Open(path)
199 if err != nil {
200 t.Fatalf("failed to open temp file: %v", err)
201 }
202 defer func() {
203 _ = f.Close()
204 }()
205
206 var w bytes.Buffer
207 ok, err := validateExpressions(f, &w)
208 if err != nil {
209 t.Fatalf("unexpected error: %v", err)
210 }
211 if ok {
212 t.Error("expected invalid result from file with bad entries")
213 }
214 output := w.String()
215 if !strings.Contains(output, `"INVALID-1"`) {
216 t.Errorf("expected INVALID-1 in output, got: %s", output)
217 }
218 if !strings.Contains(output, `"INVALID-2"`) {
219 t.Errorf("expected INVALID-2 in output, got: %s", output)
220 }
221 if !strings.Contains(output, "2 of 5 expressions failed") {
222 t.Errorf("expected summary in output, got: %s", output)
223 }
224 }