-
Notifications
You must be signed in to change notification settings - Fork 14
/
betteralign_test.go
73 lines (56 loc) · 1.63 KB
/
betteralign_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
package betteralign_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/dkorunic/betteralign"
"golang.org/x/tools/go/analysis/analysistest"
"gotest.tools/v3/golden"
)
func TestSuggestions(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, betteralign.Analyzer, "a")
}
func TestApply(t *testing.T) {
srcDir := filepath.Join("testdata", "src")
workDir := filepath.Join(srcDir, "a")
tmpDir, err := os.MkdirTemp(srcDir, "apply-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpWorkDir := filepath.Join(tmpDir, "a")
if err := os.Mkdir(tmpWorkDir, 0o750); err != nil {
t.Fatal(err)
}
paths, err := filepath.Glob(filepath.Join(workDir, "*.go"))
if err != nil {
t.Fatal(err)
}
for _, path := range paths {
testBasename := filepath.Base(path)
testTmpname := filepath.Join(tmpWorkDir, testBasename)
src, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(testTmpname, src, 0o644); err != nil {
t.Fatal(err)
}
}
testdata := analysistest.TestData()
betteralign.Analyzer.Flags.Set("apply", "true")
defer betteralign.Analyzer.Flags.Set("apply", "false")
analysistest.Run(t, testdata, betteralign.Analyzer, filepath.Join(filepath.Base(tmpDir), "a"))
for _, path := range paths {
testBasename := filepath.Base(path)
testTmpname := filepath.Join(tmpWorkDir, testBasename)
testResult, err := os.ReadFile(testTmpname)
if err != nil {
t.Fatal(err)
}
goldenFilename := filepath.Join("src", "a", strings.Join([]string{testBasename, ".golden"}, ""))
golden.Assert(t, string(testResult), goldenFilename)
}
}