/
main_test.go
70 lines (61 loc) 路 1.45 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
package main
import (
"io/fs"
"os"
"testing"
"github.com/Equationzhao/g/internal/config"
"github.com/agiledragon/gomonkey/v2"
"github.com/zeebo/assert"
)
func Test_catchPanic(t *testing.T) {
gomonkey.ApplyFunc(os.Exit, func(int) {})
tests := []struct {
name string
err any
}{
{
name: "empty",
err: nil,
},
{
name: "ErrExist",
err: fs.ErrExist,
},
{
name: "ErrNotExist",
err: fs.ErrNotExist,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
catchPanic(tt.err)
})
}
}
func Test_preprocessArgs(t *testing.T) {
settingArgs := []string{"--args2", "--args3"}
patch := gomonkey.NewPatches()
defer patch.Reset()
patch.ApplyFunc(config.Load, func() (*config.Config, error) {
return &config.Config{
Args: settingArgs,
}, nil
})
os.Args = []string{"g", "-no-config"}
preprocessArgs() // this will remove -no-config
assert.Equal(t, 1, len(os.Args))
os.Args = []string{"g", "--no-config"}
preprocessArgs() // this will remove -no-config
assert.Equal(t, 1, len(os.Args))
os.Args = []string{"g", "--args1"}
preprocessArgs() // this will add args from config
assert.Equal(t, 4, len(os.Args))
assert.DeepEqual(t, os.Args, []string{"g", "--args2", "--args3", "--args1"})
os.Args = []string{"g", "--args1", "-no-config"}
preprocessArgs()
assert.Equal(t, 2, len(os.Args))
settingArgs[0] = "-no-config"
os.Args = []string{"g", "--args1"}
preprocessArgs()
assert.Equal(t, 2, len(os.Args))
}