-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add junit report format * add fingerprint to expected test result to fix tests * fix expected junit test report
- Loading branch information
1 parent
bc59944
commit 0dbdde8
Showing
9 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package report | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
) | ||
|
||
func writeJunit(findings []Finding, w io.WriteCloser) error { | ||
testSuites := TestSuites{ | ||
TestSuites: getTestSuites(findings), | ||
} | ||
|
||
io.WriteString(w, xml.Header) | ||
encoder := xml.NewEncoder(w) | ||
encoder.Indent("", "\t") | ||
return encoder.Encode(testSuites) | ||
} | ||
|
||
func getTestSuites(findings []Finding) []TestSuite { | ||
return []TestSuite{ | ||
{ | ||
Failures: strconv.Itoa(len(findings)), | ||
Name: "gitleaks", | ||
Tests: strconv.Itoa(len(findings)), | ||
TestCases: getTestCases(findings), | ||
Time: "", | ||
}, | ||
} | ||
} | ||
|
||
func getTestCases(findings []Finding) []TestCase { | ||
testCases := []TestCase{} | ||
for _, f := range findings { | ||
testCase := TestCase{ | ||
Classname: f.Description, | ||
Failure: getFailure(f), | ||
File: f.File, | ||
Name: getMessage(f), | ||
Time: "", | ||
} | ||
testCases = append(testCases, testCase) | ||
} | ||
return testCases | ||
} | ||
|
||
func getFailure(f Finding) Failure { | ||
return Failure{ | ||
Data: getData(f), | ||
Message: getMessage(f), | ||
Type: f.Description, | ||
} | ||
} | ||
|
||
func getData(f Finding) string { | ||
data, err := json.MarshalIndent(f, "", "\t") | ||
if err != nil { | ||
fmt.Println(err) | ||
return "" | ||
} | ||
return string(data) | ||
} | ||
|
||
func getMessage(f Finding) string { | ||
if f.Commit == "" { | ||
return fmt.Sprintf("%s has detected a secret in file %s, line %s.", f.RuleID, f.File, strconv.Itoa(f.StartLine)) | ||
} | ||
|
||
return fmt.Sprintf("%s has detected a secret in file %s, line %s, at commit %s.", f.RuleID, f.File, strconv.Itoa(f.StartLine), f.Commit) | ||
} | ||
|
||
type TestSuites struct { | ||
XMLName xml.Name `xml:"testsuites"` | ||
TestSuites []TestSuite | ||
} | ||
|
||
type TestSuite struct { | ||
XMLName xml.Name `xml:"testsuite"` | ||
Failures string `xml:"failures,attr"` | ||
Name string `xml:"name,attr"` | ||
Tests string `xml:"tests,attr"` | ||
TestCases []TestCase `xml:"testcase"` | ||
Time string `xml:"time,attr"` | ||
} | ||
|
||
type TestCase struct { | ||
XMLName xml.Name `xml:"testcase"` | ||
Classname string `xml:"classname,attr"` | ||
Failure Failure `xml:"failure"` | ||
File string `xml:"file,attr"` | ||
Name string `xml:"name,attr"` | ||
Time string `xml:"time,attr"` | ||
} | ||
|
||
type Failure struct { | ||
XMLName xml.Name `xml:"failure"` | ||
Data string `xml:",chardata"` | ||
Message string `xml:"message,attr"` | ||
Type string `xml:"type,attr"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package report | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWriteJunit(t *testing.T) { | ||
tests := []struct { | ||
findings []Finding | ||
testReportName string | ||
expected string | ||
wantEmpty bool | ||
}{ | ||
{ | ||
testReportName: "simple", | ||
expected: filepath.Join(expectPath, "report", "junit_simple.xml"), | ||
findings: []Finding{ | ||
{ | ||
|
||
Description: "Test Rule", | ||
RuleID: "test-rule", | ||
Match: "line containing secret", | ||
Secret: "a secret", | ||
StartLine: 1, | ||
EndLine: 2, | ||
StartColumn: 1, | ||
EndColumn: 2, | ||
Message: "opps", | ||
File: "auth.py", | ||
Commit: "0000000000000000", | ||
Author: "John Doe", | ||
Email: "johndoe@gmail.com", | ||
Date: "10-19-2003", | ||
Tags: []string{}, | ||
}, | ||
{ | ||
|
||
Description: "Test Rule", | ||
RuleID: "test-rule", | ||
Match: "line containing secret", | ||
Secret: "a secret", | ||
StartLine: 2, | ||
EndLine: 3, | ||
StartColumn: 1, | ||
EndColumn: 2, | ||
Message: "", | ||
File: "auth.py", | ||
Commit: "", | ||
Author: "", | ||
Email: "", | ||
Date: "", | ||
Tags: []string{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
testReportName: "empty", | ||
expected: filepath.Join(expectPath, "report", "junit_empty.xml"), | ||
findings: []Finding{}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
// create tmp file using os.TempDir() | ||
tmpfile, err := os.Create(filepath.Join(tmpPath, test.testReportName+".xml")) | ||
if err != nil { | ||
os.Remove(tmpfile.Name()) | ||
t.Error(err) | ||
} | ||
err = writeJunit(test.findings, tmpfile) | ||
if err != nil { | ||
os.Remove(tmpfile.Name()) | ||
t.Error(err) | ||
} | ||
got, err := os.ReadFile(tmpfile.Name()) | ||
if err != nil { | ||
os.Remove(tmpfile.Name()) | ||
t.Error(err) | ||
} | ||
if test.wantEmpty { | ||
if len(got) > 0 { | ||
os.Remove(tmpfile.Name()) | ||
t.Errorf("Expected empty file, got %s", got) | ||
} | ||
os.Remove(tmpfile.Name()) | ||
continue | ||
} | ||
want, err := os.ReadFile(test.expected) | ||
if err != nil { | ||
os.Remove(tmpfile.Name()) | ||
t.Error(err) | ||
} | ||
|
||
if string(got) != string(want) { | ||
err = os.WriteFile(strings.Replace(test.expected, ".xml", ".got.xml", 1), got, 0644) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Errorf("got %s, want %s", string(got), string(want)) | ||
} | ||
|
||
os.Remove(tmpfile.Name()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuites> | ||
<testsuite failures="0" name="gitleaks" tests="0" time=""></testsuite> | ||
</testsuites> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuites> | ||
<testsuite failures="2" name="gitleaks" tests="2" time=""> | ||
<testcase classname="Test Rule" file="auth.py" name="test-rule has detected a secret in file auth.py, line 1, at commit 0000000000000000." time=""> | ||
<failure message="test-rule has detected a secret in file auth.py, line 1, at commit 0000000000000000." type="Test Rule">{
	"Description": "Test Rule",
	"StartLine": 1,
	"EndLine": 2,
	"StartColumn": 1,
	"EndColumn": 2,
	"Match": "line containing secret",
	"Secret": "a secret",
	"File": "auth.py",
	"SymlinkFile": "",
	"Commit": "0000000000000000",
	"Entropy": 0,
	"Author": "John Doe",
	"Email": "johndoe@gmail.com",
	"Date": "10-19-2003",
	"Message": "opps",
	"Tags": [],
	"RuleID": "test-rule",
	"Fingerprint": ""
}</failure> | ||
</testcase> | ||
<testcase classname="Test Rule" file="auth.py" name="test-rule has detected a secret in file auth.py, line 2." time=""> | ||
<failure message="test-rule has detected a secret in file auth.py, line 2." type="Test Rule">{
	"Description": "Test Rule",
	"StartLine": 2,
	"EndLine": 3,
	"StartColumn": 1,
	"EndColumn": 2,
	"Match": "line containing secret",
	"Secret": "a secret",
	"File": "auth.py",
	"SymlinkFile": "",
	"Commit": "",
	"Entropy": 0,
	"Author": "",
	"Email": "",
	"Date": "",
	"Message": "",
	"Tags": [],
	"RuleID": "test-rule",
	"Fingerprint": ""
}</failure> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> |