mirror of
https://github.com/ivuorinen/go-test-sarif.git
synced 2026-01-26 03:04:09 +00:00
* 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>
89 lines
2.8 KiB
Go
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")
|
|
}
|
|
}
|