mirror of
https://github.com/ivuorinen/go-test-sarif.git
synced 2026-02-11 08:48:40 +00:00
v1.0.0
This commit is contained in:
68
cmd/main.go
68
cmd/main.go
@@ -1,25 +1,65 @@
|
||||
// main package contains the cli functionality
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/ivuorinen/go-test-sarif-action/internal"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("Usage: go-test-sarif <input.json> <output.sarif>")
|
||||
os.Exit(1)
|
||||
}
|
||||
var (
|
||||
version = "dev"
|
||||
commit = "none"
|
||||
date = "unknown"
|
||||
builtBy = "unknown"
|
||||
)
|
||||
|
||||
inputFile := os.Args[1]
|
||||
outputFile := os.Args[2]
|
||||
|
||||
err := internal.ConvertToSARIF(inputFile, outputFile)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
func printVersion(w io.Writer) {
|
||||
_, _ = fmt.Fprintf(w, "go-test-sarif %s\n", version)
|
||||
_, _ = fmt.Fprintf(w, " commit: %s\n", commit)
|
||||
_, _ = fmt.Fprintf(w, " built at: %s\n", date)
|
||||
_, _ = fmt.Fprintf(w, " built by: %s\n", builtBy)
|
||||
}
|
||||
|
||||
func printUsage(w io.Writer) {
|
||||
_, _ = fmt.Fprintln(w, "Usage: go-test-sarif <input.json> <output.sarif>")
|
||||
_, _ = fmt.Fprintln(w, " go-test-sarif --version")
|
||||
}
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
fs := flag.NewFlagSet("go-test-sarif", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
|
||||
var versionFlag bool
|
||||
fs.BoolVar(&versionFlag, "version", false, "Display version information")
|
||||
fs.BoolVar(&versionFlag, "v", false, "Display version information (short)")
|
||||
if err := fs.Parse(args[1:]); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if versionFlag {
|
||||
printVersion(stdout)
|
||||
return 0
|
||||
}
|
||||
|
||||
if fs.NArg() < 2 {
|
||||
printUsage(stderr)
|
||||
return 1
|
||||
}
|
||||
|
||||
inputFile := fs.Arg(0)
|
||||
outputFile := fs.Arg(1)
|
||||
|
||||
if err := internal.ConvertToSARIF(inputFile, outputFile); err != nil {
|
||||
_, _ = fmt.Fprintf(stderr, "Error: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(run(os.Args, os.Stdout, os.Stderr))
|
||||
}
|
||||
|
||||
147
cmd/main_test.go
Normal file
147
cmd/main_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
setupFunc func() (string, string, func())
|
||||
wantExit int
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
}{
|
||||
{
|
||||
name: "version flag long",
|
||||
args: []string{"go-test-sarif", "--version"},
|
||||
wantExit: 0,
|
||||
wantStdout: "go-test-sarif dev",
|
||||
},
|
||||
{
|
||||
name: "version flag short",
|
||||
args: []string{"go-test-sarif", "-v"},
|
||||
wantExit: 0,
|
||||
wantStdout: "go-test-sarif dev",
|
||||
},
|
||||
{
|
||||
name: "missing arguments",
|
||||
args: []string{"go-test-sarif"},
|
||||
wantExit: 1,
|
||||
wantStderr: "Usage: go-test-sarif",
|
||||
},
|
||||
{
|
||||
name: "only one argument",
|
||||
args: []string{"go-test-sarif", "input.json"},
|
||||
wantExit: 1,
|
||||
wantStderr: "Usage: go-test-sarif",
|
||||
},
|
||||
{
|
||||
name: "valid conversion",
|
||||
args: []string{"go-test-sarif", "input.json", "output.sarif"},
|
||||
setupFunc: setupValidTestFiles,
|
||||
wantExit: 0,
|
||||
},
|
||||
{
|
||||
name: "invalid input file",
|
||||
args: []string{"go-test-sarif", "nonexistent.json", "output.sarif"},
|
||||
wantExit: 1,
|
||||
wantStderr: "Error:",
|
||||
},
|
||||
{
|
||||
name: "invalid flag",
|
||||
args: []string{"go-test-sarif", "--invalid"},
|
||||
wantExit: 1,
|
||||
wantStderr: "flag provided but not defined",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var cleanup func()
|
||||
if tt.setupFunc != nil {
|
||||
inputFile, outputFile, cleanupFunc := tt.setupFunc()
|
||||
cleanup = cleanupFunc
|
||||
// Replace placeholders with actual file paths
|
||||
for i, arg := range tt.args {
|
||||
switch arg {
|
||||
case "input.json":
|
||||
tt.args[i] = inputFile
|
||||
case "output.sarif":
|
||||
tt.args[i] = outputFile
|
||||
}
|
||||
}
|
||||
}
|
||||
if cleanup != nil {
|
||||
defer cleanup()
|
||||
}
|
||||
|
||||
stdout := &bytes.Buffer{}
|
||||
stderr := &bytes.Buffer{}
|
||||
|
||||
exitCode := run(tt.args, stdout, stderr)
|
||||
|
||||
if exitCode != tt.wantExit {
|
||||
t.Errorf("run() exit code = %v, want %v", exitCode, tt.wantExit)
|
||||
}
|
||||
|
||||
if tt.wantStdout != "" && !strings.Contains(stdout.String(), tt.wantStdout) {
|
||||
t.Errorf("stdout = %q, want to contain %q", stdout.String(), tt.wantStdout)
|
||||
}
|
||||
|
||||
if tt.wantStderr != "" && !strings.Contains(stderr.String(), tt.wantStderr) {
|
||||
t.Errorf("stderr = %q, want to contain %q", stderr.String(), tt.wantStderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintVersion(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
printVersion(buf)
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "go-test-sarif dev") {
|
||||
t.Errorf("printVersion() = %q, want to contain %q", output, "go-test-sarif dev")
|
||||
}
|
||||
if !strings.Contains(output, "commit: none") {
|
||||
t.Errorf("printVersion() = %q, want to contain %q", output, "commit: none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintUsage(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
printUsage(buf)
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "Usage: go-test-sarif <input.json> <output.sarif>") {
|
||||
t.Errorf("printUsage() = %q, want to contain usage information", output)
|
||||
}
|
||||
}
|
||||
|
||||
func setupValidTestFiles() (string, string, func()) {
|
||||
tmpDir, err := os.MkdirTemp("", "go-test-sarif-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
inputFile := filepath.Join(tmpDir, "test-input.json")
|
||||
outputFile := filepath.Join(tmpDir, "test-output.sarif")
|
||||
|
||||
// Create a valid test JSON file
|
||||
testJSON := `{"Time":"2023-01-01T00:00:00Z","Action":"pass","Package":"example.com/test","Test":"TestExample","Elapsed":0.1}`
|
||||
if err := os.WriteFile(inputFile, []byte(testJSON), 0644); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cleanup := func() {
|
||||
_ = os.RemoveAll(tmpDir)
|
||||
}
|
||||
|
||||
return inputFile, outputFile, cleanup
|
||||
}
|
||||
Reference in New Issue
Block a user