-
Notifications
You must be signed in to change notification settings - Fork 60
/
main_test.go
202 lines (161 loc) · 3.95 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
package main
import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var testFiles = map[string]string{
"test1.go": `package main
import "fmt"
func main() {
fmt.Printf("%s %s %s %s %s %s", "argument1", "argument2", "argument3", "argument4", "argument5", "argument6")
}`,
"test2.go": `package main
func main() {
myMap := map[string]string{"key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4", "key5", "value5"}
}`,
}
func TestRunDir(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal("Unexpected error creating temp dir", err)
}
defer os.RemoveAll(tmpDir)
paths = &[]string{tmpDir}
writeOutput = boolPtr(false)
listFiles = boolPtr(false)
writeTestFiles(t, testFiles, false, tmpDir)
err = run()
assert.Nil(t, err)
// Without writeOutput set to true, inputs should be unchanged
for name, contents := range testFiles {
path := filepath.Join(tmpDir, name)
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal("Unexpected error reading test file", err)
}
assert.Equal(
t,
strings.TrimSpace(contents),
strings.TrimSpace(string(bytes)),
)
}
writeOutput = boolPtr(true)
err = run()
assert.Nil(t, err)
// Now, files should be modified in place
for name, contents := range testFiles {
path := filepath.Join(tmpDir, name)
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal("Unexpected error reading test file", err)
}
assert.NotEqual(
t,
strings.TrimSpace(contents),
strings.TrimSpace(string(bytes)),
)
}
}
func TestRunFilePaths(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal("Unexpected error creating temp dir", err)
}
defer os.RemoveAll(tmpDir)
paths = &[]string{}
writeOutput = boolPtr(true)
listFiles = boolPtr(false)
writeTestFiles(t, testFiles, true, tmpDir)
err = run()
assert.Nil(t, err)
// Now, files should be modified in place
for name, contents := range testFiles {
path := filepath.Join(tmpDir, name)
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal("Unexpected error reading test file", err)
}
assert.NotEqual(
t,
strings.TrimSpace(contents),
strings.TrimSpace(string(bytes)),
)
}
}
func TestRunListFiles(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal("Unexpected error creating temp dir", err)
}
defer os.RemoveAll(tmpDir)
paths = &[]string{}
listFiles = boolPtr(true)
updatedTestFiles := map[string]string{
"test1.go": testFiles["test1.go"],
"test2.go": testFiles["test2.go"],
// File that doesn't need to be shortened
"test3.go": "package main\n",
}
writeTestFiles(t, updatedTestFiles, true, tmpDir)
output, err := captureStdout(t, run)
assert.Nil(t, err)
// Only first two files appear in output list
expectedPaths := []string{
filepath.Join(tmpDir, "test1.go"),
filepath.Join(tmpDir, "test2.go"),
}
actualPaths := strings.Split(strings.TrimSpace(output), "\n")
sort.Slice(actualPaths, func(i, j int) bool {
return actualPaths[i] < actualPaths[j]
})
assert.Equal(
t,
expectedPaths,
actualPaths,
)
}
func boolPtr(b bool) *bool {
return &b
}
func writeTestFiles(
t *testing.T,
fileContents map[string]string,
addToPaths bool,
tmpDir string,
) {
for name, contents := range fileContents {
path := filepath.Join(tmpDir, name)
if addToPaths {
tmpPaths := append(*paths, path)
paths = &tmpPaths
}
err := os.WriteFile(path, []byte(contents), 0644)
if err != nil {
t.Fatal("Unexpected error writing test file", err)
}
}
}
func captureStdout(t *testing.T, f func() error) (string, error) {
origStdout := os.Stdout
defer func() {
os.Stdout = origStdout
}()
r, w, err := os.Pipe()
if err != nil {
t.Fatal("Unexpected error opening pipe", err)
}
os.Stdout = w
resultErr := f()
w.Close()
outBytes, err := io.ReadAll(r)
if err != nil {
t.Fatal("Unexpected error reading result", err)
}
w.Close()
return string(outBytes), resultErr
}