-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathformatter_test.go
184 lines (142 loc) · 4.49 KB
/
formatter_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
package format //nolint:testpackage
import (
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/numtide/treefmt/v2/config"
"github.com/numtide/treefmt/v2/stats"
"github.com/numtide/treefmt/v2/test"
"github.com/stretchr/testify/require"
)
func TestInvalidFormatterName(t *testing.T) {
as := require.New(t)
const batchSize = 1024
cfg := &config.Config{}
cfg.OnUnmatched = "info"
statz := stats.New()
// simple "empty" config
_, err := NewCompositeFormatter(cfg, &statz, batchSize)
as.NoError(err)
// valid name using all the acceptable characters
cfg.FormatterConfigs = map[string]*config.Formatter{
"echo_command-1234567890": {
Command: "echo",
Includes: []string{"*"},
},
}
_, err = NewCompositeFormatter(cfg, &statz, batchSize)
as.NoError(err)
// test with some bad examples
for _, character := range []string{
" ", ":", "?", "*", "[", "]", "(", ")", "|", "&", "<", ">", "\\", "/", "%", "$", "#", "@", "`", "'",
} {
cfg.FormatterConfigs = map[string]*config.Formatter{
"touch_" + character: {
Command: "touch",
},
}
_, err = NewCompositeFormatter(cfg, &statz, batchSize)
as.ErrorIs(err, ErrInvalidName)
}
}
func TestFormatSignature(t *testing.T) {
as := require.New(t)
const batchSize = 1024
statz := stats.New()
tempDir := t.TempDir()
// symlink some formatters into temp dir, so we can mess with their mod times
binPath := filepath.Join(tempDir, "bin")
as.NoError(os.Mkdir(binPath, 0o755))
binaries := []string{"black", "rufo", "gofmt"}
for _, name := range binaries {
src, err := exec.LookPath(name)
as.NoError(err)
as.NoError(os.Symlink(src, filepath.Join(binPath, name)))
}
// prepend our test bin directory to PATH
t.Setenv("PATH", binPath+":"+os.Getenv("PATH"))
// start with 2 formatters
cfg := &config.Config{
OnUnmatched: "info",
FormatterConfigs: map[string]*config.Formatter{
"python": {
Command: "black",
Includes: []string{"*.py"},
},
"ruby": {
Command: "rufo",
Options: []string{"-x"},
Includes: []string{"*.rb"},
},
},
}
oldSignature := assertSignatureChangedAndStable(t, as, cfg, nil)
t.Run("change formatter mod time", func(t *testing.T) {
for _, name := range []string{"black", "rufo"} {
t.Logf("changing mod time of %s", name)
// tweak mod time
newTime := time.Now().Add(-time.Minute)
as.NoError(test.Lutimes(t, filepath.Join(binPath, name), newTime, newTime))
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
}
})
t.Run("modify formatter options", func(_ *testing.T) {
f, err := NewCompositeFormatter(cfg, &statz, batchSize)
as.NoError(err)
oldSignature = assertSignatureChangedAndStable(t, as, cfg, nil)
// adjust python includes
python := cfg.FormatterConfigs["python"]
python.Includes = []string{"*.py", "*.pyi"}
newHash, err := f.signature()
as.NoError(err)
as.Equal(oldSignature, newHash, "hash should not have changed")
// adjust python excludes
python.Excludes = []string{"*.pyi"}
newHash, err = f.signature()
as.NoError(err)
as.Equal(oldSignature, newHash, "hash should not have changed")
// adjust python options
python.Options = []string{"-w", "-s"}
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
// adjust python priority
python.Priority = 100
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
// adjust command
python.Command = "deadnix"
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
})
t.Run("add/remove formatters", func(_ *testing.T) {
cfg.FormatterConfigs["go"] = &config.Formatter{
Command: "gofmt",
Options: []string{"-w"},
Includes: []string{"*.go"},
}
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
// remove python formatter
delete(cfg.FormatterConfigs, "python")
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
// remove elm formatter
delete(cfg.FormatterConfigs, "ruby")
oldSignature = assertSignatureChangedAndStable(t, as, cfg, oldSignature)
})
}
func assertSignatureChangedAndStable(
t *testing.T,
as *require.Assertions,
cfg *config.Config,
oldSignature signature,
) (h signature) {
t.Helper()
statz := stats.New()
f, err := NewCompositeFormatter(cfg, &statz, 1024)
as.NoError(err)
newHash, err := f.signature()
as.NoError(err)
as.NotEqual(oldSignature, newHash, "hash should have changed")
sameHash, err := f.signature()
as.NoError(err)
as.Equal(newHash, sameHash, "hash should not have changed")
return newHash
}