mirror of
https://github.com/ivuorinen/go-test-sarif.git
synced 2026-01-26 03:04:09 +00:00
chore: tweaks, error handling, etc.
This commit is contained in:
@@ -13,6 +13,3 @@ trim_trailing_whitespace = true
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
|
||||
[{*.markdown,*.md}]
|
||||
indent_size = 4
|
||||
tab_width = 4
|
||||
|
||||
2
.github/README.md
vendored
2
.github/README.md
vendored
@@ -1,7 +1,5 @@
|
||||
# go-test-sarif
|
||||
|
||||
[](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.
|
||||
|
||||
## 🚀 Features
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
// Package internal contains internal helper functions for the Go Test SARIF converter.
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 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.
|
||||
func ConvertToSARIF(inputFile, outputFile string) error {
|
||||
// Read the input file
|
||||
data, err := ioutil.ReadFile(inputFile)
|
||||
data, err := os.ReadFile(inputFile)
|
||||
if err != nil {
|
||||
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
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -60,14 +61,14 @@ func ConvertToSARIF(inputFile, outputFile string) error {
|
||||
|
||||
// convertResults transforms test results into SARIF result objects.
|
||||
func convertResults(testResults []TestResult) []map[string]interface{} {
|
||||
var results []map[string]interface{}
|
||||
var results []map[string]any
|
||||
for _, tr := range testResults {
|
||||
if tr.Action == "fail" {
|
||||
results = append(results, map[string]interface{}{
|
||||
"ruleId": "go-test-failure",
|
||||
"message": map[string]string{"text": tr.Output},
|
||||
"level": "error",
|
||||
"locations": []map[string]interface{}{},
|
||||
results = append(results, map[string]any{
|
||||
"ruleId": "go-test-failure",
|
||||
"message": map[string]string{"text": tr.Output},
|
||||
"level": "error",
|
||||
"locations": []map[string]any{},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,12 @@ func TestConvertToSARIF_Success(t *testing.T) {
|
||||
if err != nil {
|
||||
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"}]`
|
||||
if _, err := inputFile.WriteString(inputContent); err != nil {
|
||||
@@ -24,7 +29,12 @@ func TestConvertToSARIF_Success(t *testing.T) {
|
||||
if err != nil {
|
||||
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
|
||||
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
|
||||
@@ -53,7 +63,12 @@ func TestConvertToSARIF_InvalidInput(t *testing.T) {
|
||||
if err != nil {
|
||||
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'
|
||||
if _, err := inputFile.WriteString(inputContent); err != nil {
|
||||
@@ -65,7 +80,12 @@ func TestConvertToSARIF_InvalidInput(t *testing.T) {
|
||||
if err != nil {
|
||||
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
|
||||
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
|
||||
@@ -84,7 +104,12 @@ func TestConvertToSARIF_FileNotFound(t *testing.T) {
|
||||
if err != nil {
|
||||
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
|
||||
err = ConvertToSARIF(inputFile, outputFile.Name())
|
||||
|
||||
Reference in New Issue
Block a user