-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
121 lines (102 loc) · 3.12 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
package main
import (
"os"
"testing"
)
func TestArguments(t *testing.T) {
// We manipulate the Args to set them up for the testcases
// After this test we restore the initial args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
cases := []struct {
Name string
Args []string
ExpectedExit int
}{
{"no arguments", []string{""}, 0},
{"single path argument", []string{"."}, 0},
{"color argument", []string{"--color"}, 0},
{"debug argument", []string{"--debug"}, 0},
{"excluded argument", []string{"--excluded"}, 0},
{"fullpath argument", []string{"--fullpath"}, 0},
{"included argument", []string{"--included"}, 0},
{"invertcolors argument", []string{"--invertcolors"}, 0},
{"nocolor argument", []string{"--nocolor"}, 0},
{"nofillpath argument", []string{"--nofullpath"}, 0},
{"verbose argument", []string{"--verbose"}, 0},
}
for _, tc := range cases {
// we need a value to set Args[0] to cause flag begins parsing at Args[1]
os.Args = append([]string{tc.Name}, tc.Args...)
actualExit := realMain()
if tc.ExpectedExit != actualExit {
t.Errorf("Wrong exit code for args: %v, expected: %v, got: %v",
tc.Args, tc.ExpectedExit, actualExit)
}
}
}
func TestFails(t *testing.T) {
// We manipulate the Args to set them up for the testcases
// After this test we restore the initial args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
cases := []struct {
Name string
Args []string
ExpectedExit int
}{
{"unreadable ignorefile", []string{"--verbose", "tests/unable_to_read_dockerignore"}, 1},
}
err := os.MkdirAll("tests/unable_to_read_dockerignore", 0755)
check(err)
createEmptyTestFile := func(name string) {
d := []byte("")
check(os.WriteFile(name, d, 0333))
}
createEmptyTestFile("tests/unable_to_read_dockerignore/.dockerignore")
defer os.RemoveAll("tests")
for _, tc := range cases {
// we need a value to set Args[0] to cause flag begins parsing at Args[1]
os.Args = append([]string{tc.Name}, tc.Args...)
actualExit := realMain()
if tc.ExpectedExit != actualExit {
t.Errorf("Wrong exit code for args: %v, expected: %v, got: %v",
tc.Args, tc.ExpectedExit, actualExit)
}
}
}
func TestConfig(t *testing.T) {
// We manipulate the Args to set them up for the testcases
// After this test we restore the initial args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
cases := []struct {
Name string
Args []string
ExpectedExit int
}{
{"basic config", []string{"--verbose", "tests/ok"}, 0},
}
err := os.MkdirAll("tests/ok", 0755)
check(err)
createEmptyTestFile := func(name string) {
d := []byte("")
check(os.WriteFile(name, d, 0644))
}
createEmptyTestFile("tests/ok/.dockerignore")
defer os.RemoveAll("tests")
for _, tc := range cases {
// we need a value to set Args[0] to cause flag begins parsing at Args[1]
os.Args = append([]string{tc.Name}, tc.Args...)
actualExit := realMain()
if tc.ExpectedExit != actualExit {
t.Errorf("Wrong exit code for args: %v, expected: %v, got: %v",
tc.Args, tc.ExpectedExit, actualExit)
}
}
}
func check(e error) {
if e != nil {
panic(e)
}
}