Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/raptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ var (
)

func main() {
mainCore(os.Args[1:], os.Exit)
}

// mainCore contains the main logic and is testable
func mainCore(args []string, exit func(int)) {
logger.Init()
if err := run(os.Args[1:]); err != nil {
if err := run(args); err != nil {
slog.Error("execution failed", "error", err)
os.Exit(1)
exit(1)
}
}

Expand Down
66 changes: 66 additions & 0 deletions cmd/raptor/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,69 @@ jobs:
t.Error("runCommand() should error when a job fails")
}
}

// TestMain_Direct tests the main function directly by manipulating os.Args
// This covers the main() entry point for success paths
func TestMain_Direct(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// Test with help command (doesn't call os.Exit)
os.Args = []string{"raptor", "help"}
main()

// Test with version command (doesn't call os.Exit)
os.Args = []string{"raptor", "version"}
main()

// Test with no args (doesn't call os.Exit)
os.Args = []string{"raptor"}
main()
}

// TestMainCore tests the mainCore function which contains the main entry point logic
func TestMainCore(t *testing.T) {
t.Run("success with no args", func(t *testing.T) {
exitCode := -1
mockExit := func(code int) { exitCode = code }

mainCore([]string{}, mockExit)

if exitCode != -1 {
t.Errorf("expected no exit call, but got exit code %d", exitCode)
}
})

t.Run("success with help command", func(t *testing.T) {
exitCode := -1
mockExit := func(code int) { exitCode = code }

mainCore([]string{"help"}, mockExit)

if exitCode != -1 {
t.Errorf("expected no exit call, but got exit code %d", exitCode)
}
})

t.Run("failure with unknown command", func(t *testing.T) {
exitCode := -1
mockExit := func(code int) { exitCode = code }

mainCore([]string{"unknown"}, mockExit)

if exitCode != 1 {
t.Errorf("expected exit code 1, got %d", exitCode)
}
})

t.Run("failure with missing workflow", func(t *testing.T) {
exitCode := -1
mockExit := func(code int) { exitCode = code }

mainCore([]string{"run"}, mockExit)

if exitCode != 1 {
t.Errorf("expected exit code 1, got %d", exitCode)
}
})
}