feat: the app (#2)

This commit is contained in:
2025-03-24 00:38:41 +02:00
committed by GitHub
parent 3470e255a7
commit e904b1beb3
12 changed files with 416 additions and 123 deletions

75
internal/converter.go Normal file
View File

@@ -0,0 +1,75 @@
package internal
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// TestResult represents a single test result from 'go test -json' output.
type TestResult struct {
Action string `json:"Action"`
Package string `json:"Package"`
Output string `json:"Output"`
}
// ConvertToSARIF converts Go test JSON results to SARIF format.
func ConvertToSARIF(inputFile, outputFile string) error {
// Read the input file
data, err := ioutil.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("failed to read input file: %w", err)
}
// 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]interface{}{
"version": "2.1.0",
"runs": []map[string]interface{}{
{
"tool": map[string]interface{}{
"driver": map[string]interface{}{
"name": "go-test-sarif",
"version": "1.0.0",
},
},
"results": convertResults(testResults),
},
},
}
// Marshal SARIF data to JSON
sarifJSON, err := json.MarshalIndent(sarifData, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal SARIF data: %w", err)
}
// Write the SARIF JSON to the output file
if err := ioutil.WriteFile(outputFile, sarifJSON, 0644); 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]interface{} {
var results []map[string]interface{}
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{}{},
})
}
}
return results
}

View File

@@ -0,0 +1,94 @@
package internal
import (
"os"
"testing"
)
// TestConvertToSARIF_Success tests the successful conversion of a valid Go test JSON output to SARIF format.
func TestConvertToSARIF_Success(t *testing.T) {
// Create a temporary JSON input file with valid test data
inputFile, err := os.CreateTemp("", "test_input_*.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/internal","Output":"Test failed"}]`
if _, err := inputFile.WriteString(inputContent); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err)
}
// Create a temporary SARIF output file
outputFile, err := os.CreateTemp("", "test_output_*.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer os.Remove(outputFile.Name())
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err)
}
// Read and validate the SARIF output
outputContent, err := os.ReadFile(outputFile.Name())
if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err)
}
// Perform basic validation on the SARIF output
if len(outputContent) == 0 {
t.Errorf("SARIF output is empty")
}
// Additional validations can be added here to verify the correctness of the SARIF content
}
// TestConvertToSARIF_InvalidInput tests the function's behavior when provided with invalid JSON input.
func TestConvertToSARIF_InvalidInput(t *testing.T) {
// Create a temporary JSON input file with invalid test data
inputFile, err := os.CreateTemp("", "test_input_invalid_*.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/internal","Output":Test failed}` // Missing quotes around 'Test failed'
if _, err := inputFile.WriteString(inputContent); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err)
}
// Create a temporary SARIF output file
outputFile, err := os.CreateTemp("", "test_output_invalid_*.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer os.Remove(outputFile.Name())
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err == nil {
t.Errorf("Expected an error for invalid JSON input, but got none")
}
}
// TestConvertToSARIF_FileNotFound tests the function's behavior when the input file does not exist.
func TestConvertToSARIF_FileNotFound(t *testing.T) {
// Define a non-existent input file path
inputFile := "non_existent_file.json"
// Create a temporary SARIF output file
outputFile, err := os.CreateTemp("", "test_output_notfound_*.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer os.Remove(outputFile.Name())
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile, outputFile.Name())
if err == nil {
t.Errorf("Expected an error for non-existent input file, but got none")
}
}