-
Notifications
You must be signed in to change notification settings - Fork 60
/
shortener_test.go
95 lines (80 loc) · 2.08 KB
/
shortener_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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const fixturesDir = "_fixtures"
// TestShortener verifies the core shortening functionality on the files in the _fixtures
// directory. To update the expected outputs, run tests with the REGENERATE_TEST_OUTPUTS
// environment variable set to "true".
func TestShortener(t *testing.T) {
info, err := os.ReadDir(fixturesDir)
assert.Nil(t, err)
fixturePaths := []string{}
for _, fileInfo := range info {
if fileInfo.IsDir() {
continue
} else if !strings.HasSuffix(fileInfo.Name(), ".go") {
continue
} else if strings.HasSuffix(fileInfo.Name(), "__exp.go") {
continue
}
fixturePaths = append(
fixturePaths,
filepath.Join(fixturesDir, fileInfo.Name()),
)
}
dotDir, err := os.MkdirTemp("", "dot")
if err != nil {
t.Fatalf("Error creating output directory for dot files: %+v", err)
}
defer os.RemoveAll(dotDir)
shortener := NewShortener(
ShortenerConfig{
MaxLen: 100,
TabLen: 4,
KeepAnnotations: false,
ShortenComments: true,
ReformatTags: true,
IgnoreGenerated: true,
BaseFormatterCmd: "gofmt",
DotFile: filepath.Join(dotDir, "out.dot"),
ChainSplitDots: true,
},
)
for _, fixturePath := range fixturePaths {
contents, err := os.ReadFile(fixturePath)
if err != nil {
t.Fatalf(
"Unexpected error reading fixture %s: %+v",
fixturePath,
err,
)
}
shortenedContents, err := shortener.Shorten(contents)
assert.Nil(t, err)
expectedPath := fixturePath[0:len(fixturePath)-3] + "__exp" + ".go"
if os.Getenv("REGENERATE_TEST_OUTPUTS") == "true" {
err := os.WriteFile(expectedPath, shortenedContents, 0644)
if err != nil {
t.Fatalf(
"Unexpected error writing output file %s: %+v",
expectedPath,
err,
)
}
}
expectedContents, err := os.ReadFile(expectedPath)
if err != nil {
t.Fatalf(
"Unexpected error reading expected file %s: %+v",
expectedPath,
err,
)
}
assert.Equal(t, string(expectedContents), string(shortenedContents))
}
}