-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
82 lines (73 loc) · 1.74 KB
/
main_test.go
File metadata and controls
82 lines (73 loc) · 1.74 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
package main
import (
"os"
"strings"
"testing"
"github.com/ploycloud/ploy-server-cli/cmd"
"github.com/ploycloud/ploy-server-cli/src/commands"
"github.com/ploycloud/ploy-server-cli/src/common"
"github.com/stretchr/testify/assert"
)
func TestMain(t *testing.T) {
// Save original args and osExit
oldArgs := os.Args
oldOsExit := osExit
// Restore original args and osExit after the test
defer func() {
os.Args = oldArgs
osExit = oldOsExit
}()
// Mock osExit
var exitCode int
osExit = func(code int) {
exitCode = code
panic("osExit") // Use panic to stop execution
}
tests := []struct {
name string
args []string
expectedOutput string
expectedExit int
}{
{
name: "No arguments",
args: []string{"ploy"},
expectedOutput: "Ploy CLI is a powerful tool",
expectedExit: 0,
},
{
name: "Valid command",
args: []string{"ploy", "version"},
expectedOutput: common.CurrentCliVersion,
expectedExit: 0,
},
{
name: "Invalid command",
args: []string{"ploy", "invalidcommand"},
expectedOutput: "unknown command \"invalidcommand\" for \"ploy\"",
expectedExit: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
exitCode = 0
output := commands.CaptureOutput(func() {
defer func() {
if r := recover(); r != nil {
if r != "osExit" {
panic(r) // re-panic if it's not our expected panic
}
}
}()
err := cmd.Execute()
if err != nil {
osExit(1)
}
})
t.Logf("Full output:\n%s", output)
assert.Contains(t, strings.ToLower(output), strings.ToLower(tt.expectedOutput))
assert.Equal(t, tt.expectedExit, exitCode)
})
}
}