Files
go-test-sarif/internal/converter_test.go
Ismo Vuorinen 28f7e9be9f feat: focus repo on cli tool only (#19)
* fix: update dependencies

* fix: include package failures and enable trivy
2025-07-14 02:00:27 +03:00

153 lines
4.8 KiB
Go

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 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","Test":"TestExample","Output":"Test failed"}` + "\n"
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 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())
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 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","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)
}
// 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 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())
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 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())
if err == nil {
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")
}
}