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>
This commit is contained in:
2025-07-15 17:49:15 +03:00
committed by GitHub
parent a09f3e358b
commit ffb5da2a8e
12 changed files with 95 additions and 98 deletions

View File

@@ -24,7 +24,11 @@ func ConvertToSARIF(inputFile, outputFile string) error {
if err != nil {
return fmt.Errorf("failed to read input file: %w", err)
}
defer f.Close()
defer func() {
if cerr := f.Close(); cerr != nil {
fmt.Fprintf(os.Stderr, "failed to close input file: %v\n", cerr)
}
}()
report, err := sarif.New(sarif.Version210)
if err != nil {

View File

@@ -2,147 +2,83 @@ 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) {
// 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())
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 := inputFile.WriteString(inputContent); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err)
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write 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())
outputPath := filepath.Join(dir, "output.sarif")
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err != nil {
if err := ConvertToSARIF(inputPath, outputPath); err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err)
}
// Read and validate the SARIF output
outputContent, err := os.ReadFile(outputFile.Name())
outputContent, err := os.ReadFile(outputPath)
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())
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 := inputFile.WriteString(inputContent); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err)
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write 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())
outputPath := filepath.Join(dir, "output.sarif")
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err == nil {
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) {
// 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())
dir := t.TempDir()
outputPath := filepath.Join(dir, "output.sarif")
// Run the ConvertToSARIF function
err = ConvertToSARIF(inputFile, outputFile.Name())
if err == nil {
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) {
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())
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 := inputFile.WriteString(inputContent); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err)
if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write 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())
outputPath := filepath.Join(dir, "output.sarif")
if err := ConvertToSARIF(inputFile.Name(), outputFile.Name()); err != nil {
if err := ConvertToSARIF(inputPath, outputPath); err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err)
}
data, err := os.ReadFile(outputFile.Name())
data, err := os.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err)
}