Skip to content

Commit c956feb

Browse files
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
1 parent 7fec28d commit c956feb

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

cmd/execute_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
)
8+
9+
// Helper function to test main with different os.Args
10+
func testMain(args []string) (exitCode int) {
11+
// Save original os.Args and defer restoring it
12+
origArgs := os.Args
13+
defer func() { os.Args = origArgs }()
14+
15+
// Save original os.Exit and defer restoring it
16+
defer func() { exitFunc = os.Exit }()
17+
18+
// Mock os.Exit
19+
fakeExit := func(code int) {
20+
exitCode = code
21+
}
22+
exitFunc = fakeExit
23+
24+
// Mock os.Args
25+
os.Args = args
26+
27+
// Run the main function
28+
Execute(context.Background(), "")
29+
30+
return exitCode
31+
}
32+
33+
func TestMainHelp(t *testing.T) {
34+
exitCode := testMain([]string{"cmd", "--help"})
35+
if exitCode != 0 {
36+
t.Errorf("Expected exit code 0, got %d", exitCode)
37+
}
38+
}
39+
40+
func TestMainNoArgsError(t *testing.T) {
41+
exitCode := testMain([]string{"cmd"})
42+
if exitCode != 1 {
43+
t.Errorf("Expected exit code 1, got %d", exitCode)
44+
}
45+
}

cmd/root.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ type Flag struct {
4141
Description string `json:"description"`
4242
}
4343

44+
var exitFunc = os.Exit
45+
4446
// Execute is the entry point to running the CLI
4547
func Execute(ctx context.Context, version string) {
4648
input := new(Input)
49+
rootCmd := createRootCommand(ctx, input, version)
50+
51+
if err := rootCmd.Execute(); err != nil {
52+
exitFunc(1)
53+
}
54+
}
55+
56+
func createRootCommand(ctx context.Context, input *Input, version string) *cobra.Command {
4757
rootCmd := &cobra.Command{
4858
Use: "act [event name to run] [flags]\n\nIf no event name passed, will default to \"on: push\"\nIf actions handles only one event it will be used as default instead of \"on: push\"",
4959
Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
@@ -117,10 +127,7 @@ func Execute(ctx context.Context, version string) {
117127
rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)")
118128
rootCmd.PersistentFlags().BoolVar(&input.listOptions, "list-options", false, "Print a json structure of compatible options")
119129
rootCmd.SetArgs(args())
120-
121-
if err := rootCmd.Execute(); err != nil {
122-
os.Exit(1)
123-
}
130+
return rootCmd
124131
}
125132

126133
// Return locations where Act's config can be found in order: XDG spec, .actrc in HOME directory, .actrc in invocation directory

cmd/root_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"path"
56
"testing"
67

@@ -26,3 +27,58 @@ line2
2627
line3
2728
`, secrets["mysecret"])
2829
}
30+
31+
func TestListOptions(t *testing.T) {
32+
rootCmd := createRootCommand(context.Background(), &Input{}, "")
33+
err := newRunCommand(context.Background(), &Input{
34+
listOptions: true,
35+
})(rootCmd, []string{})
36+
assert.NoError(t, err)
37+
}
38+
39+
func TestRun(t *testing.T) {
40+
rootCmd := createRootCommand(context.Background(), &Input{}, "")
41+
err := newRunCommand(context.Background(), &Input{
42+
platforms: []string{"ubuntu-latest=node:16-buster-slim"},
43+
workdir: "../pkg/runner/testdata/",
44+
workflowsPath: "./basic/push.yml",
45+
})(rootCmd, []string{})
46+
assert.NoError(t, err)
47+
}
48+
49+
func TestRunPush(t *testing.T) {
50+
rootCmd := createRootCommand(context.Background(), &Input{}, "")
51+
err := newRunCommand(context.Background(), &Input{
52+
platforms: []string{"ubuntu-latest=node:16-buster-slim"},
53+
workdir: "../pkg/runner/testdata/",
54+
workflowsPath: "./basic/push.yml",
55+
})(rootCmd, []string{"push"})
56+
assert.NoError(t, err)
57+
}
58+
59+
func TestRunPushJsonLogger(t *testing.T) {
60+
rootCmd := createRootCommand(context.Background(), &Input{}, "")
61+
err := newRunCommand(context.Background(), &Input{
62+
platforms: []string{"ubuntu-latest=node:16-buster-slim"},
63+
workdir: "../pkg/runner/testdata/",
64+
workflowsPath: "./basic/push.yml",
65+
jsonLogger: true,
66+
})(rootCmd, []string{"push"})
67+
assert.NoError(t, err)
68+
}
69+
70+
func TestFlags(t *testing.T) {
71+
for _, f := range []string{"graph", "list", "bug-report", "man-page"} {
72+
t.Run("TestFlag-"+f, func(t *testing.T) {
73+
rootCmd := createRootCommand(context.Background(), &Input{}, "")
74+
err := rootCmd.Flags().Set(f, "true")
75+
assert.NoError(t, err)
76+
err = newRunCommand(context.Background(), &Input{
77+
platforms: []string{"ubuntu-latest=node:16-buster-slim"},
78+
workdir: "../pkg/runner/testdata/",
79+
workflowsPath: "./basic/push.yml",
80+
})(rootCmd, []string{})
81+
assert.NoError(t, err)
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)