-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsemgrepConfigCreator_test.go
More file actions
97 lines (82 loc) · 2.61 KB
/
semgrepConfigCreator_test.go
File metadata and controls
97 lines (82 loc) · 2.61 KB
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
package tools
import (
"codacy/cli-v2/domain"
"codacy/cli-v2/plugins/tools/semgrep/embedded"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
// TestFilterRulesFromFile tests the FilterRulesFromFile function
func TestFilterRulesFromFile(t *testing.T) {
// Get the actual rules file content
rulesData := embedded.GetSemgrepRules()
// Test case 1: Filter with enabled rules
config := []domain.PatternConfiguration{
{
Enabled: true,
PatternDefinition: domain.PatternDefinition{
Id: "Semgrep_ai.csharp.detect-openai.detect-openai",
Enabled: true,
},
},
}
result, err := FilterRulesFromFile(rulesData, config)
assert.NoError(t, err)
// Parse the result and verify we got filtered rules
var parsedRules semgrepRulesFile
err = yaml.Unmarshal(result, &parsedRules)
assert.NoError(t, err)
assert.Equal(t, 1, len(parsedRules.Rules))
// Test case 2: No enabled rules should return an error
noEnabledConfig := []domain.PatternConfiguration{
{
Enabled: false,
PatternDefinition: domain.PatternDefinition{
Id: "Semgrep_nonexistent",
Enabled: false,
},
},
}
_, err = FilterRulesFromFile(rulesData, noEnabledConfig)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no matching rules found")
// Test case 3: Invalid YAML should return an error
_, err = FilterRulesFromFile([]byte("invalid yaml"), config)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to parse rules file")
}
// TestGetSemgrepConfig tests the GetSemgrepConfig function
func TestGetSemgrepConfig(t *testing.T) {
// Test with valid configuration
config := []domain.PatternConfiguration{
{
Enabled: true,
PatternDefinition: domain.PatternDefinition{
Id: "Semgrep_ai.csharp.detect-openai.detect-openai",
Enabled: true,
},
},
}
result, err := GetSemgrepConfig(config)
assert.NoError(t, err)
var parsedRules semgrepRulesFile
err = yaml.Unmarshal(result, &parsedRules)
assert.NoError(t, err)
assert.Equal(t, 1, len(parsedRules.Rules))
// Test with empty configuration (should return all rules)
result, err = GetSemgrepConfig([]domain.PatternConfiguration{})
assert.NoError(t, err)
err = yaml.Unmarshal(result, &parsedRules)
assert.NoError(t, err)
assert.True(t, len(parsedRules.Rules) > 0)
}
// TestGetDefaultSemgrepConfig tests the GetDefaultSemgrepConfig function
func TestGetDefaultSemgrepConfig(t *testing.T) {
// Test getting default config
result, err := GetDefaultSemgrepConfig()
assert.NoError(t, err)
var parsedRules semgrepRulesFile
err = yaml.Unmarshal(result, &parsedRules)
assert.NoError(t, err)
assert.True(t, len(parsedRules.Rules) > 0)
}