-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
292 lines (261 loc) · 8.61 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Copyright 2024-2025 Su Yang (soulteary)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"bytes"
"errors"
"io"
"os"
"path"
"testing"
Cmd "github.com/soulteary/ssh-config/cmd"
)
func TestRun(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("TestProcess() error = %v", err)
}
jsonContent, err := os.ReadFile(path.Join(pwd, "./testdata/main-test.json"))
if err != nil {
t.Errorf("TestProcess() error = %v", err)
}
sshContent, err := os.ReadFile(path.Join(pwd, "./testdata/main-test.cfg"))
if err != nil {
t.Errorf("TestProcess() error = %v", err)
}
yamlContent, err := os.ReadFile(path.Join(pwd, "./testdata/main-test.yaml"))
if err != nil {
t.Errorf("TestProcess() error = %v", err)
}
tests := []struct {
name string
args Cmd.Args
deps Dependencies
wantErr bool
}{
{
name: "Invalid convert arguments",
args: Cmd.Args{ToYAML: true, ToJSON: true, ToSSH: true},
deps: Dependencies{
Println: func(...interface{}) (int, error) { return 0, nil },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "Pipe mode",
args: Cmd.Args{ToSSH: true},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, nil },
Println: func(...interface{}) (int, error) { return 0, nil },
GetUserInputFromStdin: func() string { return string(yamlContent) },
Process: func(string, string, Cmd.Args) []byte { return sshContent },
CheckUseStdin: func() bool { return true },
},
wantErr: false,
},
{
name: "Invalid IO arguments",
args: Cmd.Args{ToJSON: true, Src: "input.txt", Dest: "output.json"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "File read error",
args: Cmd.Args{ToJSON: true, Src: "input.txt", Dest: "output.json"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return nil, errors.New("read error") },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "File save error",
args: Cmd.Args{ToJSON: true, Src: "input.txt", Dest: "output.json"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return sshContent, nil },
SaveFile: func(string, []byte) error { return errors.New("save error") },
Process: func(string, string, Cmd.Args) []byte { return jsonContent },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "Successful file conversion",
args: Cmd.Args{ToYAML: true, Src: "testdata/main-test.cfg", Dest: "test.yaml"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return sshContent, nil },
SaveFile: func(string, []byte) error { return nil },
Process: func(string, string, Cmd.Args) []byte { return yamlContent },
CheckUseStdin: func() bool { return false },
},
wantErr: false,
},
{
name: "File read error with print",
args: Cmd.Args{ToJSON: true, Src: "testdata/main-test.cfg"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return nil, errors.New("read error") },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "File save error with print",
args: Cmd.Args{ToJSON: true, Src: "testdata/main-test.cfg", Dest: "can-not-save.json"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return sshContent, nil },
SaveFile: func(string, []byte) error { return errors.New("save error") },
Process: func(string, string, Cmd.Args) []byte { return jsonContent },
CheckUseStdin: func() bool { return false },
},
wantErr: true,
},
{
name: "Successful file conversion",
args: Cmd.Args{ToJSON: true, Src: "testdata/main-test.cfg"},
deps: Dependencies{
StdinStat: func() (os.FileInfo, error) { return nil, errors.New("not a pipe") },
Println: func(...interface{}) (int, error) { return 0, nil },
GetContent: func(string) ([]byte, error) { return sshContent, nil },
SaveFile: func(string, []byte) error { return nil },
Process: func(string, string, Cmd.Args) []byte { return jsonContent },
CheckUseStdin: func() bool { return false },
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Run(tt.args, tt.deps)
if (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestMainWithDependencies(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
tests := []struct {
name string
args []string
expectedOutput string
expectedExit int
mockHomeDir func() (string, error)
}{
{
name: "Successful execution",
args: []string{"cmd", "--to-yaml", "-src", "testdata/main-test.json", "-dest", "test.yaml"},
expectedOutput: "File has been saved successfully\nFile path: test.yaml\n",
expectedExit: 0,
mockHomeDir: os.UserHomeDir,
},
{
name: "Error execution",
args: []string{"cmd", "--to-json", "--to-yaml"}, // Invalid args
expectedOutput: "Please specify either -to-yaml or -to-ssh or -to-json\n",
expectedExit: 1,
mockHomeDir: os.UserHomeDir,
},
{
name: "Home directory error",
args: []string{"cmd"}, // No src specified, will try to use home dir
expectedOutput: "Error: getting user home directory: mock home dir error\nError: Source path '.ssh' does not exist\n",
expectedExit: 1,
mockHomeDir: func() (string, error) {
return "", errors.New("mock home dir error")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
exitCode := 0
exitFunc := func(code int) {
exitCode = code
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
MainWithDependencies(exitFunc, tt.mockHomeDir)
Cmd.ResetFlags()
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if output != tt.expectedOutput {
if tt.name != "Error execution" {
t.Errorf("Output = %q, want %q", output, tt.expectedOutput)
}
}
if exitCode != tt.expectedExit {
t.Errorf("Exit code = %d, want %d", exitCode, tt.expectedExit)
}
if tt.expectedExit == 0 {
os.Remove("output.json")
}
})
}
}
var osExit = os.Exit
func TestMain(t *testing.T) {
oldArgs := os.Args
oldExit := osExit
defer func() {
os.Args = oldArgs
osExit = oldExit
}()
exitCalled := false
exitCode := 0
osExit = func(code int) {
exitCalled = true
exitCode = code
}
os.Args = []string{"cmd", "--to-yaml", "-src", "testdata/main-test.json", "-dest", "test.yaml"}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
main()
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
expectedOutput := "File has been saved successfully\nFile path: test.yaml\n"
if output != expectedOutput {
t.Errorf("Output = %q, want %q", output, expectedOutput)
}
if exitCalled {
t.Errorf("os.Exit was called with code %d", exitCode)
}
os.Remove("test.yaml")
}