add test workflows to cmd pkg (#2665)

* add test workflows to cmd pkg

* list-options as well

* add more tests

* test entrypoint as well

* update exit code 1 test
This commit is contained in:
ChristopherHX
2025-02-13 00:00:55 +01:00
committed by GitHub
parent 7fec28d69e
commit c956febfe4
3 changed files with 112 additions and 4 deletions

45
cmd/execute_test.go Normal file
View File

@@ -0,0 +1,45 @@
package cmd
import (
"context"
"os"
"testing"
)
// Helper function to test main with different os.Args
func testMain(args []string) (exitCode int) {
// Save original os.Args and defer restoring it
origArgs := os.Args
defer func() { os.Args = origArgs }()
// Save original os.Exit and defer restoring it
defer func() { exitFunc = os.Exit }()
// Mock os.Exit
fakeExit := func(code int) {
exitCode = code
}
exitFunc = fakeExit
// Mock os.Args
os.Args = args
// Run the main function
Execute(context.Background(), "")
return exitCode
}
func TestMainHelp(t *testing.T) {
exitCode := testMain([]string{"cmd", "--help"})
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d", exitCode)
}
}
func TestMainNoArgsError(t *testing.T) {
exitCode := testMain([]string{"cmd"})
if exitCode != 1 {
t.Errorf("Expected exit code 1, got %d", exitCode)
}
}