-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain_test.go
More file actions
95 lines (85 loc) · 2.66 KB
/
Copy pathmain_test.go
File metadata and controls
95 lines (85 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"testing"
"github.com/flamingo-stack/openframe-cli/internal/shared/executor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestExitCode proves the top level propagates a failed command's exit code when
// it is a valid Unix code, and otherwise falls back to a generic 1.
func TestExitCode(t *testing.T) {
assert.Equal(t, 1, exitCode(nil))
assert.Equal(t, 1, exitCode(errors.New("plain error")))
// A CommandError's code is preserved, bare and wrapped.
assert.Equal(t, 125, exitCode(&executor.CommandError{ExitCode: 125}))
assert.Equal(t, 125, exitCode(fmt.Errorf("cluster create failed: %w", &executor.CommandError{ExitCode: 125})))
// Out-of-range / non-failure codes fall back to 1.
assert.Equal(t, 1, exitCode(&executor.CommandError{ExitCode: 0}))
assert.Equal(t, 1, exitCode(&executor.CommandError{ExitCode: -1}))
assert.Equal(t, 1, exitCode(&executor.CommandError{ExitCode: 4294967295}))
}
func TestMainIntegration(t *testing.T) {
// Build test binary. The .exe suffix is mandatory on Windows: os/exec
// resolves executables via PATHEXT, so an extensionless binary is
// "not found in %PATH%" even by explicit path.
testBinary := "openframe-test-main"
if runtime.GOOS == "windows" {
testBinary += ".exe"
}
buildCmd := exec.Command("go", "build", "-o", testBinary, ".")
require.NoError(t, buildCmd.Run())
defer os.Remove(testBinary)
tests := []struct {
name string
args []string
wantErr bool
contains string
}{
{
name: "help",
args: []string{"--help"},
wantErr: false,
contains: "OpenFrame CLI",
},
{
name: "version",
args: []string{"--version"},
wantErr: false,
contains: "dev",
},
{
name: "invalid flag",
args: []string{"--invalid"},
wantErr: true,
contains: "unknown flag",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmd := exec.Command("./"+testBinary, tc.args...)
// On native Windows the binary forwards the whole CLI into WSL,
// which the test environment need not have (the e2e workflow leg
// sets WSL up; the unit-test leg does not). These subtests exercise
// pure cobra surface (help/version/flag parsing), so bypass the
// forward and run natively everywhere.
cmd.Env = append(os.Environ(), "OPENFRAME_NO_WSL_FORWARD=1")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
output := stdout.String() + stderr.String()
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Contains(t, output, tc.contains)
})
}
}