|
|
@@ -1,6 +1,7 @@
|
|
|
package report
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
@@ -48,10 +49,8 @@ func TestWriteSarif(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
for _, test := range tests {
|
|
|
- // create tmp file using os.TempDir()
|
|
|
- tmpfile, err := os.Create(filepath.Join(tmpPath, test.testReportName+".json"))
|
|
|
+ tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
|
|
if err != nil {
|
|
|
- os.Remove(tmpfile.Name())
|
|
|
t.Error(err)
|
|
|
}
|
|
|
viper.Reset()
|
|
|
@@ -76,29 +75,24 @@ func TestWriteSarif(t *testing.T) {
|
|
|
err = writeSarif(cfg, test.findings, tmpfile)
|
|
|
fmt.Println(cfg)
|
|
|
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) {
|
|
|
+ if !bytes.Equal(got, want) {
|
|
|
err = os.WriteFile(strings.Replace(test.expected, ".sarif", ".got.sarif", 1), got, 0644)
|
|
|
if err != nil {
|
|
|
t.Error(err)
|
|
|
@@ -106,6 +100,5 @@ func TestWriteSarif(t *testing.T) {
|
|
|
t.Errorf("got %s, want %s", string(got), string(want))
|
|
|
}
|
|
|
|
|
|
- os.Remove(tmpfile.Name())
|
|
|
}
|
|
|
}
|