Skip to content

Commit

Permalink
Put sarif rule and results at begin of tests in order to reduce repet…
Browse files Browse the repository at this point in the history
…ition in want of unit tests
  • Loading branch information
diogo-fjrocha committed Oct 23, 2024
1 parent fffb640 commit e4af4e5
Showing 1 changed file with 125 additions and 143 deletions.
268 changes: 125 additions & 143 deletions lib/reporting/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"
)

// test input results
var (
ruleID1 = "ruleID1"
ruleID2 = "ruleID2"
Expand All @@ -30,32 +31,141 @@ var (
ValidationStatus: secrets.ValidResult,
RuleDescription: "Rule Description",
}
// this result has a different rule than result1
result2 = &secrets.Secret{
ID: "ID2",
Source: "file2",
RuleID: "ruleID1",
RuleID: ruleID2,
StartLine: 10,
EndLine: 10,
LineContent: "line content2",
StartColumn: 41,
EndColumn: 160,
Value: "value 2",
ValidationStatus: secrets.InvalidResult,
RuleDescription: "Rule Description",
RuleDescription: "Rule Description2",
}
// this result has a different rule than 1 and 2
// this result has the same rule as result1
result3 = &secrets.Secret{
ID: "ID3",
Source: "file3",
RuleID: ruleID2,
RuleID: ruleID1,
StartLine: 16,
EndLine: 16,
LineContent: "line content3",
StartColumn: 11,
EndColumn: 130,
Value: "value 3",
ValidationStatus: secrets.UnknownResult,
RuleDescription: "Rule Description2",
RuleDescription: "Rule Description",
}
)

// test expected outputs
var (
// sarif rules
rule1Sarif = &SarifRule{
ID: ruleID1,
FullDescription: &Message{
Text: result1.RuleDescription,
},
}
rule2Sarif = &SarifRule{
ID: ruleID2,
FullDescription: &Message{
Text: result2.RuleDescription,
},
}
// sarif results
result1Sarif = Results{
Message: Message{
Text: messageText(result1.RuleID, result1.Source),
},
RuleId: ruleID1,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result1.Source,
},
Region: Region{
StartLine: result1.StartLine,
StartColumn: result1.StartColumn,
EndLine: result1.EndLine,
EndColumn: result1.EndColumn,
Snippet: Snippet{
Text: result1.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result1.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result1.ValidationStatus),
},
}
result2Sarif = Results{
Message: Message{
Text: messageText(result2.RuleID, result2.Source),
},
RuleId: ruleID2,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result2.Source,
},
Region: Region{
StartLine: result2.StartLine,
StartColumn: result2.StartColumn,
EndLine: result2.EndLine,
EndColumn: result2.EndColumn,
Snippet: Snippet{
Text: result2.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result2.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result2.ValidationStatus),
},
}
result3Sarif = Results{
Message: Message{
Text: messageText(result3.RuleID, result3.Source),
},
RuleId: ruleID1,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result3.Source,
},
Region: Region{
StartLine: result3.StartLine,
StartColumn: result3.StartColumn,
EndLine: result3.EndLine,
EndColumn: result3.EndColumn,
Snippet: Snippet{
Text: result3.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result3.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result3.ValidationStatus),
},
}
)

Expand Down Expand Up @@ -121,7 +231,7 @@ func TestGetOutputSarif(t *testing.T) {
TotalSecretsFound: 2,
Results: map[string][]*secrets.Secret{
"secret1": {result1},
"secret2": {result2},
"secret3": {result3},
},
},
wantErr: false,
Expand All @@ -132,87 +242,25 @@ func TestGetOutputSarif(t *testing.T) {
Name: "report",
SemanticVersion: "1",
Rules: []*SarifRule{
{ID: "ruleID1",
FullDescription: &Message{
Text: result1.RuleDescription,
},
},
rule1Sarif,
},
},
},
Results: []Results{
{
Message: Message{
Text: messageText(result1.RuleID, result1.Source),
},
RuleId: ruleID1,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result1.Source,
},
Region: Region{
StartLine: result1.StartLine,
StartColumn: result1.StartColumn,
EndLine: result1.EndLine,
EndColumn: result1.EndColumn,
Snippet: Snippet{
Text: result1.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result1.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result1.ValidationStatus),
},
},
{
Message: Message{
Text: messageText(result2.RuleID, result2.Source),
},
RuleId: ruleID1,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result2.Source,
},
Region: Region{
StartLine: result2.StartLine,
StartColumn: result2.StartColumn,
EndLine: result2.EndLine,
EndColumn: result2.EndColumn,
Snippet: Snippet{
Text: result2.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result2.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result2.ValidationStatus),
},
},
result1Sarif,
result3Sarif,
},
},
},
},
{
name: "two_results_same_rule_want_two_rules_in_report",
name: "two_results_two_rules_want_two_rules_in_report",
arg: Report{
TotalItemsScanned: 2,
TotalSecretsFound: 2,
Results: map[string][]*secrets.Secret{
"secret1": {result1},
"secret2": {result3},
"secret2": {result2},
},
},
wantErr: false,
Expand All @@ -223,80 +271,14 @@ func TestGetOutputSarif(t *testing.T) {
Name: "report",
SemanticVersion: "1",
Rules: []*SarifRule{
{ID: ruleID1,
FullDescription: &Message{
Text: result1.RuleDescription,
},
},
{ID: ruleID2,
FullDescription: &Message{
Text: result3.RuleDescription,
},
},
rule1Sarif,
rule2Sarif,
},
},
},
Results: []Results{
{
Message: Message{
Text: messageText(result1.RuleID, result1.Source),
},
RuleId: ruleID1,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result1.Source,
},
Region: Region{
StartLine: result1.StartLine,
StartColumn: result1.StartColumn,
EndLine: result1.EndLine,
EndColumn: result1.EndColumn,
Snippet: Snippet{
Text: result1.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result1.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result1.ValidationStatus),
},
},
{
Message: Message{
Text: messageText(result3.RuleID, result3.Source),
},
RuleId: ruleID2,
Locations: []Locations{
{
PhysicalLocation: PhysicalLocation{
ArtifactLocation: ArtifactLocation{
URI: result3.Source,
},
Region: Region{
StartLine: result3.StartLine,
StartColumn: result3.StartColumn,
EndLine: result3.EndLine,
EndColumn: result3.EndColumn,
Snippet: Snippet{
Text: result3.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(result3.LineContent),
},
},
},
},
},
},
Properties: Properties{
"validationStatus": string(result3.ValidationStatus),
},
},
result1Sarif,
result2Sarif,
},
},
},
Expand Down

0 comments on commit e4af4e5

Please sign in to comment.