chore: tweaks, error handling, etc.

This commit is contained in:
2025-03-26 19:19:43 +02:00
parent 4f8fad5c44
commit 788cdfab42
4 changed files with 40 additions and 19 deletions

View File

@@ -13,6 +13,3 @@ trim_trailing_whitespace = true
[*.go] [*.go]
indent_style = tab indent_style = tab
[{*.markdown,*.md}]
indent_size = 4
tab_width = 4

2
.github/README.md vendored
View File

@@ -1,7 +1,5 @@
# go-test-sarif # go-test-sarif
[![CI](https://github.com/ivuorinen/go-test-sarif/actions/workflows/test.yml/badge.svg)](https://github.com/ivuorinen/go-test-sarif/actions/workflows/test.yml)
`go-test-sarif` is a CLI tool and GitHub Action for converting `go test -json` output into SARIF format, making it compatible with GitHub Security Tab and other SARIF consumers. `go-test-sarif` is a CLI tool and GitHub Action for converting `go test -json` output into SARIF format, making it compatible with GitHub Security Tab and other SARIF consumers.
## 🚀 Features ## 🚀 Features

View File

@@ -1,9 +1,10 @@
// Package internal contains internal helper functions for the Go Test SARIF converter.
package internal package internal
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
) )
// TestResult represents a single test result from 'go test -json' output. // TestResult represents a single test result from 'go test -json' output.
@@ -16,7 +17,7 @@ type TestResult struct {
// ConvertToSARIF converts Go test JSON results to SARIF format. // ConvertToSARIF converts Go test JSON results to SARIF format.
func ConvertToSARIF(inputFile, outputFile string) error { func ConvertToSARIF(inputFile, outputFile string) error {
// Read the input file // Read the input file
data, err := ioutil.ReadFile(inputFile) data, err := os.ReadFile(inputFile)
if err != nil { if err != nil {
return fmt.Errorf("failed to read input file: %w", err) return fmt.Errorf("failed to read input file: %w", err)
} }
@@ -50,7 +51,7 @@ func ConvertToSARIF(inputFile, outputFile string) error {
} }
// Write the SARIF JSON to the output file // Write the SARIF JSON to the output file
if err := ioutil.WriteFile(outputFile, sarifJSON, 0644); err != nil { if err := os.WriteFile(outputFile, sarifJSON, 0644); err != nil {
return fmt.Errorf("failed to write SARIF output file: %w", err) return fmt.Errorf("failed to write SARIF output file: %w", err)
} }
@@ -60,14 +61,14 @@ func ConvertToSARIF(inputFile, outputFile string) error {
// convertResults transforms test results into SARIF result objects. // convertResults transforms test results into SARIF result objects.
func convertResults(testResults []TestResult) []map[string]interface{} { func convertResults(testResults []TestResult) []map[string]interface{} {
var results []map[string]interface{} var results []map[string]any
for _, tr := range testResults { for _, tr := range testResults {
if tr.Action == "fail" { if tr.Action == "fail" {
results = append(results, map[string]interface{}{ results = append(results, map[string]any{
"ruleId": "go-test-failure", "ruleId": "go-test-failure",
"message": map[string]string{"text": tr.Output}, "message": map[string]string{"text": tr.Output},
"level": "error", "level": "error",
"locations": []map[string]interface{}{}, "locations": []map[string]any{},
}) })
} }
} }

View File

@@ -12,7 +12,12 @@ func TestConvertToSARIF_Success(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp input file: %v", err) t.Fatalf("Failed to create temp input file: %v", err)
} }
defer os.Remove(inputFile.Name()) defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp input file: %v", err)
}
}(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","Output":"Test failed"}]`
if _, err := inputFile.WriteString(inputContent); err != nil { if _, err := inputFile.WriteString(inputContent); err != nil {
@@ -24,7 +29,12 @@ func TestConvertToSARIF_Success(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp output file: %v", err) t.Fatalf("Failed to create temp output file: %v", err)
} }
defer os.Remove(outputFile.Name()) defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function // Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name()) err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
@@ -53,7 +63,12 @@ func TestConvertToSARIF_InvalidInput(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp input file: %v", err) t.Fatalf("Failed to create temp input file: %v", err)
} }
defer os.Remove(inputFile.Name()) defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp input file: %v", err)
}
}(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","Output":Test failed}` // Missing quotes around 'Test failed'
if _, err := inputFile.WriteString(inputContent); err != nil { if _, err := inputFile.WriteString(inputContent); err != nil {
@@ -65,7 +80,12 @@ func TestConvertToSARIF_InvalidInput(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp output file: %v", err) t.Fatalf("Failed to create temp output file: %v", err)
} }
defer os.Remove(outputFile.Name()) defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function // Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name()) err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
@@ -84,7 +104,12 @@ func TestConvertToSARIF_FileNotFound(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp output file: %v", err) t.Fatalf("Failed to create temp output file: %v", err)
} }
defer os.Remove(outputFile.Name()) defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function // Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile, outputFile.Name()) err = ConvertToSARIF(inputFile, outputFile.Name())