diff --git a/.editorconfig b/.editorconfig index b391a74..b9c1dda 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,6 +13,3 @@ trim_trailing_whitespace = true [*.go] indent_style = tab -[{*.markdown,*.md}] -indent_size = 4 -tab_width = 4 diff --git a/.github/README.md b/.github/README.md index 5e7df1c..1ee84b9 100644 --- a/.github/README.md +++ b/.github/README.md @@ -1,7 +1,5 @@ # 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. ## 🚀 Features diff --git a/internal/converter.go b/internal/converter.go index 9088f83..fb8da46 100644 --- a/internal/converter.go +++ b/internal/converter.go @@ -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{}, }) } } diff --git a/internal/converter_test.go b/internal/converter_test.go index 4698056..c3153fd 100644 --- a/internal/converter_test.go +++ b/internal/converter_test.go @@ -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())