mirror of
https://github.com/ivuorinen/go-test-sarif.git
synced 2026-03-02 11:56:05 +00:00
feat: focus repo on cli tool only (#19)
* fix: update dependencies * fix: include package failures and enable trivy
This commit is contained in:
@@ -1,76 +1,61 @@
|
||||
// Package internal contains internal helper functions for the Go Test SARIF converter.
|
||||
// Package internal provides the SARIF conversion utilities.
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/owenrumney/go-sarif/v2/sarif"
|
||||
)
|
||||
|
||||
// TestResult represents a single test result from 'go test -json' output.
|
||||
type TestResult struct {
|
||||
// TestEvent represents a single line of `go test -json` output.
|
||||
type TestEvent struct {
|
||||
Action string `json:"Action"`
|
||||
Package string `json:"Package"`
|
||||
Output string `json:"Output"`
|
||||
Test string `json:"Test,omitempty"`
|
||||
Output string `json:"Output,omitempty"`
|
||||
}
|
||||
|
||||
// ConvertToSARIF converts Go test JSON results to SARIF format.
|
||||
// ConvertToSARIF converts Go test JSON events to the SARIF format.
|
||||
func ConvertToSARIF(inputFile, outputFile string) error {
|
||||
// Read the input file
|
||||
data, err := os.ReadFile(inputFile)
|
||||
f, err := os.Open(inputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read input file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Parse the JSON data
|
||||
var testResults []TestResult
|
||||
if err := json.Unmarshal(data, &testResults); err != nil {
|
||||
return fmt.Errorf("invalid JSON format: %w", err)
|
||||
}
|
||||
|
||||
// Convert test results to SARIF format
|
||||
sarifData := map[string]any{
|
||||
"version": "2.1.0",
|
||||
"runs": []map[string]any{
|
||||
{
|
||||
"tool": map[string]any{
|
||||
"driver": map[string]any{
|
||||
"name": "go-test-sarif",
|
||||
"version": "1.0.0",
|
||||
},
|
||||
},
|
||||
"results": convertResults(testResults),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Marshal SARIF data to JSON
|
||||
sarifJSON, err := json.MarshalIndent(sarifData, "", " ")
|
||||
report, err := sarif.New(sarif.Version210)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal SARIF data: %w", err)
|
||||
return fmt.Errorf("failed to create SARIF report: %w", err)
|
||||
}
|
||||
|
||||
// Write the SARIF JSON to the output file
|
||||
if err := os.WriteFile(outputFile, sarifJSON, 0644); err != nil {
|
||||
run := sarif.NewRunWithInformationURI("go-test-sarif", "https://golang.org/cmd/go/#hdr-Test_packages")
|
||||
rule := run.AddRule("go-test-failure").WithDescription("go test failure")
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
var event TestEvent
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil {
|
||||
return fmt.Errorf("invalid JSON format: %w", err)
|
||||
}
|
||||
if event.Action == "fail" && (event.Test != "" || event.Package != "") {
|
||||
res := sarif.NewRuleResult(rule.ID).
|
||||
WithLevel("error").
|
||||
WithMessage(sarif.NewTextMessage(event.Output))
|
||||
run.AddResult(res)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("failed to scan input file: %w", err)
|
||||
}
|
||||
|
||||
report.AddRun(run)
|
||||
if err := report.WriteFile(outputFile); err != nil {
|
||||
return fmt.Errorf("failed to write SARIF output file: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("SARIF report generated: %s\n", outputFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertResults transforms test results into SARIF result objects.
|
||||
func convertResults(testResults []TestResult) []map[string]any {
|
||||
var results []map[string]any
|
||||
for _, tr := range testResults {
|
||||
if tr.Action == "fail" {
|
||||
results = append(results, map[string]any{
|
||||
"ruleId": "go-test-failure",
|
||||
"message": map[string]string{"text": tr.Output},
|
||||
"level": "error",
|
||||
"locations": []map[string]any{},
|
||||
})
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestConvertToSARIF_Success(t *testing.T) {
|
||||
}
|
||||
}(inputFile.Name())
|
||||
|
||||
inputContent := `[{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Output":"Test failed"}]`
|
||||
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":"Test failed"}` + "\n"
|
||||
if _, err := inputFile.WriteString(inputContent); err != nil {
|
||||
t.Fatalf("Failed to write to temp input file: %v", err)
|
||||
}
|
||||
@@ -70,7 +70,8 @@ func TestConvertToSARIF_InvalidInput(t *testing.T) {
|
||||
}
|
||||
}(inputFile.Name())
|
||||
|
||||
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Output":Test failed}` // Missing quotes around 'Test failed'
|
||||
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":` +
|
||||
`Test failed}` + "\n" // Missing quotes around 'Test failed'
|
||||
if _, err := inputFile.WriteString(inputContent); err != nil {
|
||||
t.Fatalf("Failed to write to temp input file: %v", err)
|
||||
}
|
||||
@@ -117,3 +118,35 @@ func TestConvertToSARIF_FileNotFound(t *testing.T) {
|
||||
t.Errorf("Expected an error for non-existent input file, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConvertToSARIF_PackageFailure ensures package-level failures are included in the SARIF output.
|
||||
func TestConvertToSARIF_PackageFailure(t *testing.T) {
|
||||
inputFile, err := os.CreateTemp("", "test_input_pkgfail_*.json")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp input file: %v", err)
|
||||
}
|
||||
defer os.Remove(inputFile.Name())
|
||||
|
||||
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif-action","Output":"FAIL"}` + "\n"
|
||||
if _, err := inputFile.WriteString(inputContent); err != nil {
|
||||
t.Fatalf("Failed to write to temp input file: %v", err)
|
||||
}
|
||||
|
||||
outputFile, err := os.CreateTemp("", "test_output_pkgfail_*.sarif")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp output file: %v", err)
|
||||
}
|
||||
defer os.Remove(outputFile.Name())
|
||||
|
||||
if err := ConvertToSARIF(inputFile.Name(), outputFile.Name()); err != nil {
|
||||
t.Errorf("ConvertToSARIF returned an error: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(outputFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read SARIF output file: %v", err)
|
||||
}
|
||||
if len(data) == 0 {
|
||||
t.Errorf("SARIF output is empty")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user