Files
go-test-sarif/internal/converter_test.go
Ismo Vuorinen ffb5da2a8e feat: add golangci-lint and restore megalinter config (#22)
* feat: add golangci-lint and restore megalinter config

* chore: fix linting, go mod tidy

* fix(ci): add golangci-lint action setup to test.yml

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore: tweaks

* fix(ci): tweaks

* fix(ci): disable go_golangci_lint in megalinter

* chore: yamllint rules

* chore(ci): tweak yml

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-07-15 17:49:15 +03:00

89 lines
2.8 KiB
Go

package internal
import (
"os"
"path/filepath"
"testing"
)
// TestConvertToSARIF_Success tests the successful conversion of a valid Go test JSON output to SARIF format.
func TestConvertToSARIF_Success(t *testing.T) {
dir := t.TempDir()
inputPath := filepath.Join(dir, "input.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":"Test failed"}` + "\n"
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write input file: %v", err)
}
outputPath := filepath.Join(dir, "output.sarif")
if err := ConvertToSARIF(inputPath, outputPath); err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err)
}
outputContent, err := os.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err)
}
if len(outputContent) == 0 {
t.Errorf("SARIF output is empty")
}
}
// TestConvertToSARIF_InvalidInput tests the function's behavior when provided with invalid JSON input.
func TestConvertToSARIF_InvalidInput(t *testing.T) {
dir := t.TempDir()
inputPath := filepath.Join(dir, "invalid.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":` +
`Test failed}` + "\n" // Missing quotes around 'Test failed'
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write input file: %v", err)
}
outputPath := filepath.Join(dir, "output.sarif")
if err := ConvertToSARIF(inputPath, outputPath); 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) {
inputFile := "non_existent_file.json"
dir := t.TempDir()
outputPath := filepath.Join(dir, "output.sarif")
if err := ConvertToSARIF(inputFile, outputPath); 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) {
dir := t.TempDir()
inputPath := filepath.Join(dir, "input.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif-action","Output":"FAIL"}` + "\n"
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write input file: %v", err)
}
outputPath := filepath.Join(dir, "output.sarif")
if err := ConvertToSARIF(inputPath, outputPath); err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err)
}
data, err := os.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err)
}
if len(data) == 0 {
t.Errorf("SARIF output is empty")
}
}