-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
171 lines (136 loc) · 4.2 KB
/
main_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"testing"
)
// Mock os.Exit function
var osExit = os.Exit
// Remove remaining pull-related tests
func resetFlags() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func mockLogs() (*bytes.Buffer, func()) {
var buf bytes.Buffer
originalLogOutput := log.Writer()
log.SetOutput(&buf)
return &buf, func() {
log.SetOutput(originalLogOutput)
}
}
func TestMain(m *testing.M) {
// Initialize testing flags
testing.Init()
// Save the original os.Args and restore it after the test
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
// Remove test flags from os.Args
os.Args = []string{originalArgs[0]}
// Run the tests
os.Exit(m.Run())
}
func TestMain_NoCommand_ShowsHelp(t *testing.T) {
logBuffer, restoreLogs := mockLogs()
defer restoreLogs()
// Save the original os.Exit function and restore it after the test
originalExit := osExit
defer func() { osExit = originalExit }()
// Mock os.Exit to prevent it from terminating the test
osExit = func(code int) {}
// Simulate calling the program without any subcommands
os.Args = []string{"mdefaults"}
// Capture the output
output := captureOutput(func() {
run()
})
expectedOutput := "Usage: mdefaults [command]\nCommands:\n pull - Retrieve and update configuration values.\n push - Write configuration values.\nHey, let's call with pull or push.\n"
if output != expectedOutput {
t.Errorf("Expected output:\n%s\nGot:\n%s", expectedOutput, output)
}
_ = logBuffer // Use logBuffer if needed for assertions
}
func TestMain_VersionFlag(t *testing.T) {
resetFlags()
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
os.Args = []string{"cmd", "--version"}
output := captureOutput(func() {
main()
})
if !bytes.Contains([]byte(output), []byte("Version: ")) || !bytes.Contains([]byte(output), []byte("Architecture: ")) {
t.Errorf("Expected version and architecture information to be printed, got: %s", output)
}
}
func TestMain_VFlag(t *testing.T) {
resetFlags()
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
os.Args = []string{"cmd", "-v"}
output := captureOutput(func() {
main()
})
if !bytes.Contains([]byte(output), []byte("Version: ")) || !bytes.Contains([]byte(output), []byte("Architecture: ")) {
t.Errorf("Expected version and architecture information to be printed, got: %s", output)
}
}
func TestMain_WIPMessage(t *testing.T) {
// Save the original os.Exit function and restore it after the test
originalExit := osExit
defer func() { osExit = originalExit }()
// Mock os.Exit to prevent it from terminating the test
osExit = func(code int) {}
// Test cases for different OS
testCases := []struct {
osType string
message string
}{
{"linux", "Work In Progress: This tool uses macOS specific commands and may not function correctly on Linux/Windows."},
{"windows", "Work In Progress: This tool uses macOS specific commands and may not function correctly on Linux/Windows."},
}
for _, tc := range testCases {
t.Run(tc.osType, func(t *testing.T) {
// Capture output
output := captureOutput(func() {
mainWithOSType(tc.osType)
})
// Check if the output contains the expected message
if !bytes.Contains([]byte(output), []byte(tc.message)) {
t.Errorf("Expected message %q, but got %q", tc.message, output)
}
})
}
}
// mainWithOSType is a helper function to simulate different OS types
func mainWithOSType(osType string) {
if osType == "linux" || osType == "windows" {
fmt.Println("Work In Progress: This tool uses macOS specific commands and may not function correctly on Linux/Windows.")
}
initFlags()
flag.Parse()
if versionFlag || vFlag {
fmt.Printf("Version: %s\n", version)
fmt.Printf("Architecture: %s\n", architecture)
return
}
fmt.Printf("Version: %s\n", version)
fmt.Printf("Architecture: %s\n", architecture)
osExit(run())
}
// Helper function to capture output
func captureOutput(f func()) string {
r, w, _ := os.Pipe()
originalStdout := os.Stdout
os.Stdout = w
f()
w.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
fmt.Printf("Failed to copy output: %v", err)
}
os.Stdout = originalStdout
return buf.String()
}