Skip to content

Commit 60db4b6

Browse files
authored
add unit tests (#12)
1 parent 078b838 commit 60db4b6

File tree

15 files changed

+1335
-23
lines changed

15 files changed

+1335
-23
lines changed

internal/astgrep/analyzer.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// AnalyzePackageMatches analyzes AST-Grep output for a specific package
1111
func AnalyzePackageMatches(matches []ASTGrepMatch, pkg string) *PackageDetectionResult {
1212
logger.Log.Debug("analysing astgrep result")
13+
1314
result := &PackageDetectionResult{
1415
PackageName: pkg,
1516
Found: false,
@@ -24,14 +25,13 @@ func AnalyzePackageMatches(matches []ASTGrepMatch, pkg string) *PackageDetection
2425
}
2526

2627
result.RuleId = matches[0].RuleId
27-
2828
fileSet := make(map[string]bool)
2929
versionSet := make(map[string]bool)
3030
var firstMatch *Location
3131

32+
// Process all matches
3233
for _, match := range matches {
33-
matchPackage := match.GetPackageName()
34-
if !strings.Contains(matchPackage, pkg) {
34+
if !isPackageMatch(match, pkg) {
3535
continue
3636
}
3737

@@ -40,19 +40,18 @@ func AnalyzePackageMatches(matches []ASTGrepMatch, pkg string) *PackageDetection
4040
loc := match.GetLocation()
4141
firstMatch = &loc
4242
}
43+
4344
fileSet[match.File] = true
4445
if version := match.GetVersion(); version != "" {
4546
versionSet[version] = true
4647
}
4748
}
4849

49-
for file := range fileSet {
50-
result.UniqueFiles = append(result.UniqueFiles, file)
51-
}
52-
for version := range versionSet {
53-
result.Versions = append(result.Versions, version)
54-
}
50+
// Convert sets to slices
51+
result.UniqueFiles = mapKeysToSlice(fileSet)
52+
result.Versions = mapKeysToSlice(versionSet)
5553

54+
// Set result flags
5655
result.Found = result.TotalMatches > 0
5756
result.FirstMatch = firstMatch
5857
result.HasMultipleFiles = len(result.UniqueFiles) > 1
@@ -61,3 +60,17 @@ func AnalyzePackageMatches(matches []ASTGrepMatch, pkg string) *PackageDetection
6160
logger.Log.Debug("completed analysis, returning updated result")
6261
return result
6362
}
63+
64+
// isPackageMatch checks if a match is for the specified package
65+
func isPackageMatch(match ASTGrepMatch, pkg string) bool {
66+
return strings.Contains(match.GetPackageName(), pkg)
67+
}
68+
69+
// mapKeysToSlice converts map keys to a slice
70+
func mapKeysToSlice(m map[string]bool) []string {
71+
keys := make([]string, 0, len(m))
72+
for key := range m {
73+
keys = append(keys, key)
74+
}
75+
return keys
76+
}

internal/astgrep/analyzer_test.go

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
package astgrep
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/sudeshgutta/secure-scan-action/internal/logger"
8+
)
9+
10+
func TestAnalyzePackageMatches_EmptyMatches(t *testing.T) {
11+
// Initialize logger for tests
12+
logger.Init()
13+
14+
matches := []ASTGrepMatch{}
15+
pkg := "test-package"
16+
17+
result := AnalyzePackageMatches(matches, pkg)
18+
19+
if result.PackageName != pkg {
20+
t.Errorf("Expected PackageName to be '%s', got '%s'", pkg, result.PackageName)
21+
}
22+
23+
if result.Found {
24+
t.Error("Expected Found to be false for empty matches")
25+
}
26+
27+
if result.TotalMatches != 0 {
28+
t.Errorf("Expected TotalMatches to be 0, got %d", result.TotalMatches)
29+
}
30+
31+
if len(result.UniqueFiles) != 0 {
32+
t.Errorf("Expected UniqueFiles to be empty, got %v", result.UniqueFiles)
33+
}
34+
35+
if len(result.Versions) != 0 {
36+
t.Errorf("Expected Versions to be empty, got %v", result.Versions)
37+
}
38+
39+
if result.FirstMatch != nil {
40+
t.Error("Expected FirstMatch to be nil for empty matches")
41+
}
42+
43+
if result.HasMultipleFiles {
44+
t.Error("Expected HasMultipleFiles to be false for empty matches")
45+
}
46+
47+
if result.HasVersions {
48+
t.Error("Expected HasVersions to be false for empty matches")
49+
}
50+
}
51+
52+
func TestAnalyzePackageMatches_SingleMatch(t *testing.T) {
53+
// Initialize logger for tests
54+
logger.Init()
55+
56+
matches := []ASTGrepMatch{
57+
{
58+
Text: "github.com/test/package",
59+
File: "test.go",
60+
RuleId: "test-rule",
61+
Range: Range{
62+
Start: Position{Line: 10, Column: 5},
63+
},
64+
},
65+
}
66+
pkg := "github.com/test/package"
67+
68+
result := AnalyzePackageMatches(matches, pkg)
69+
70+
if result.PackageName != pkg {
71+
t.Errorf("Expected PackageName to be '%s', got '%s'", pkg, result.PackageName)
72+
}
73+
74+
if !result.Found {
75+
t.Error("Expected Found to be true")
76+
}
77+
78+
if result.TotalMatches != 1 {
79+
t.Errorf("Expected TotalMatches to be 1, got %d", result.TotalMatches)
80+
}
81+
82+
if len(result.UniqueFiles) != 1 {
83+
t.Errorf("Expected UniqueFiles to have 1 file, got %d", len(result.UniqueFiles))
84+
}
85+
86+
if result.UniqueFiles[0] != "test.go" {
87+
t.Errorf("Expected UniqueFiles[0] to be 'test.go', got '%s'", result.UniqueFiles[0])
88+
}
89+
90+
if result.RuleId != "test-rule" {
91+
t.Errorf("Expected RuleId to be 'test-rule', got '%s'", result.RuleId)
92+
}
93+
94+
if result.FirstMatch == nil {
95+
t.Error("Expected FirstMatch to be set")
96+
} else {
97+
if result.FirstMatch.File != "test.go" {
98+
t.Errorf("Expected FirstMatch.File to be 'test.go', got '%s'", result.FirstMatch.File)
99+
}
100+
if result.FirstMatch.Line != 10 {
101+
t.Errorf("Expected FirstMatch.Line to be 10, got %d", result.FirstMatch.Line)
102+
}
103+
if result.FirstMatch.Column != 5 {
104+
t.Errorf("Expected FirstMatch.Column to be 5, got %d", result.FirstMatch.Column)
105+
}
106+
}
107+
108+
if result.HasMultipleFiles {
109+
t.Error("Expected HasMultipleFiles to be false for single file")
110+
}
111+
112+
if result.HasVersions {
113+
t.Error("Expected HasVersions to be false for package without version")
114+
}
115+
}
116+
117+
func TestAnalyzePackageMatches_MultipleMatches(t *testing.T) {
118+
// Initialize logger for tests
119+
logger.Init()
120+
121+
matches := []ASTGrepMatch{
122+
{
123+
Text: "github.com/test/package/v1",
124+
File: "file1.go",
125+
RuleId: "test-rule",
126+
Range: Range{
127+
Start: Position{Line: 10, Column: 5},
128+
},
129+
},
130+
{
131+
Text: "github.com/test/package/v1",
132+
File: "file2.go",
133+
RuleId: "test-rule",
134+
Range: Range{
135+
Start: Position{Line: 20, Column: 10},
136+
},
137+
},
138+
{
139+
Text: "github.com/test/package/v2",
140+
File: "file1.go",
141+
RuleId: "test-rule",
142+
Range: Range{
143+
Start: Position{Line: 30, Column: 15},
144+
},
145+
},
146+
}
147+
pkg := "github.com/test/package"
148+
149+
result := AnalyzePackageMatches(matches, pkg)
150+
151+
if result.PackageName != pkg {
152+
t.Errorf("Expected PackageName to be '%s', got '%s'", pkg, result.PackageName)
153+
}
154+
155+
if !result.Found {
156+
t.Error("Expected Found to be true")
157+
}
158+
159+
if result.TotalMatches != 3 {
160+
t.Errorf("Expected TotalMatches to be 3, got %d", result.TotalMatches)
161+
}
162+
163+
if len(result.UniqueFiles) != 2 {
164+
t.Errorf("Expected UniqueFiles to have 2 files, got %d", len(result.UniqueFiles))
165+
}
166+
167+
// Check that both files are present
168+
files := make(map[string]bool)
169+
for _, file := range result.UniqueFiles {
170+
files[file] = true
171+
}
172+
if !files["file1.go"] || !files["file2.go"] {
173+
t.Error("Expected both file1.go and file2.go to be in UniqueFiles")
174+
}
175+
176+
if len(result.Versions) != 2 {
177+
t.Errorf("Expected Versions to have 2 versions, got %d", len(result.Versions))
178+
}
179+
180+
// Check that both versions are present
181+
versions := make(map[string]bool)
182+
for _, version := range result.Versions {
183+
versions[version] = true
184+
}
185+
if !versions["v1"] || !versions["v2"] {
186+
t.Error("Expected both v1 and v2 to be in Versions")
187+
}
188+
189+
if !result.HasMultipleFiles {
190+
t.Error("Expected HasMultipleFiles to be true for multiple files")
191+
}
192+
193+
if !result.HasVersions {
194+
t.Error("Expected HasVersions to be true for packages with versions")
195+
}
196+
}
197+
198+
func TestAnalyzePackageMatches_PackageNameMismatch(t *testing.T) {
199+
// Initialize logger for tests
200+
logger.Init()
201+
202+
matches := []ASTGrepMatch{
203+
{
204+
Text: "github.com/other/package",
205+
File: "test.go",
206+
RuleId: "test-rule",
207+
Range: Range{
208+
Start: Position{Line: 10, Column: 5},
209+
},
210+
},
211+
}
212+
pkg := "github.com/test/package"
213+
214+
result := AnalyzePackageMatches(matches, pkg)
215+
216+
if result.PackageName != pkg {
217+
t.Errorf("Expected PackageName to be '%s', got '%s'", pkg, result.PackageName)
218+
}
219+
220+
if result.Found {
221+
t.Error("Expected Found to be false for package name mismatch")
222+
}
223+
224+
if result.TotalMatches != 0 {
225+
t.Errorf("Expected TotalMatches to be 0, got %d", result.TotalMatches)
226+
}
227+
228+
if len(result.UniqueFiles) != 0 {
229+
t.Errorf("Expected UniqueFiles to be empty, got %v", result.UniqueFiles)
230+
}
231+
232+
if len(result.Versions) != 0 {
233+
t.Errorf("Expected Versions to be empty, got %v", result.Versions)
234+
}
235+
}
236+
237+
func TestAnalyzePackageMatches_PartialPackageNameMatch(t *testing.T) {
238+
// Initialize logger for tests
239+
logger.Init()
240+
241+
matches := []ASTGrepMatch{
242+
{
243+
Text: "github.com/test/package/subpackage",
244+
File: "test.go",
245+
RuleId: "test-rule",
246+
Range: Range{
247+
Start: Position{Line: 10, Column: 5},
248+
},
249+
},
250+
}
251+
pkg := "github.com/test/package"
252+
253+
result := AnalyzePackageMatches(matches, pkg)
254+
255+
if result.PackageName != pkg {
256+
t.Errorf("Expected PackageName to be '%s', got '%s'", pkg, result.PackageName)
257+
}
258+
259+
if !result.Found {
260+
t.Error("Expected Found to be true for partial package name match")
261+
}
262+
263+
if result.TotalMatches != 1 {
264+
t.Errorf("Expected TotalMatches to be 1, got %d", result.TotalMatches)
265+
}
266+
}
267+
268+
func TestAnalyzePackageMatches_ScanTimestamp(t *testing.T) {
269+
// Initialize logger for tests
270+
logger.Init()
271+
272+
matches := []ASTGrepMatch{
273+
{
274+
Text: "github.com/test/package",
275+
File: "test.go",
276+
RuleId: "test-rule",
277+
Range: Range{
278+
Start: Position{Line: 10, Column: 5},
279+
},
280+
},
281+
}
282+
pkg := "github.com/test/package"
283+
284+
before := time.Now()
285+
result := AnalyzePackageMatches(matches, pkg)
286+
after := time.Now()
287+
288+
if result.ScanTimestamp.Before(before) || result.ScanTimestamp.After(after) {
289+
t.Error("Expected ScanTimestamp to be set to current time")
290+
}
291+
}
292+
293+
func TestAnalyzePackageMatches_EmptyVersions(t *testing.T) {
294+
// Initialize logger for tests
295+
logger.Init()
296+
297+
matches := []ASTGrepMatch{
298+
{
299+
Text: "github.com/test/package",
300+
File: "test.go",
301+
RuleId: "test-rule",
302+
Range: Range{
303+
Start: Position{Line: 10, Column: 5},
304+
},
305+
},
306+
}
307+
pkg := "github.com/test/package"
308+
309+
result := AnalyzePackageMatches(matches, pkg)
310+
311+
if result.HasVersions {
312+
t.Error("Expected HasVersions to be false for package without version")
313+
}
314+
315+
if len(result.Versions) != 0 {
316+
t.Errorf("Expected Versions to be empty, got %v", result.Versions)
317+
}
318+
}

0 commit comments

Comments
 (0)