-
Notifications
You must be signed in to change notification settings - Fork 0
/
ote_test.go
171 lines (141 loc) · 3.24 KB
/
ote_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
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"go.akshayshah.org/attest"
)
func Test_run(t *testing.T) {
t.Parallel()
tests := []struct {
name string
fp string
readonly bool
wantErr string
}{
{
name: "testdata/modfiles/mod1",
fp: "testdata/modfiles/mod1",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod2",
fp: "testdata/modfiles/mod2",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod3",
fp: "testdata/modfiles/mod3",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod4",
fp: "testdata/modfiles/mod4",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod5",
fp: "testdata/modfiles/mod5",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod6",
fp: "testdata/modfiles/mod6",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/mod7",
fp: "testdata/modfiles/mod7",
readonly: true,
wantErr: "",
},
{
name: "testdata/modfiles/nonExistentPackage",
fp: "testdata/modfiles/nonExistentPackage",
readonly: true,
wantErr: "no such file or directory",
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
w := &bytes.Buffer{}
err := run(tt.fp, w, tt.readonly)
if len(tt.wantErr) > 0 {
attest.Subsequence(t, err.Error(), tt.wantErr)
return
}
attest.Ok(t, err)
got := w.String()
path := getDataPath(t, "ote_test.go", tt.name)
dealWithTestData(t, path, got)
})
}
}
func Test_cli(t *testing.T) {
t.Parallel()
tests := []struct {
name string
file string
readonly bool
version bool
}{
{
name: "current directory",
file: ".",
readonly: false,
version: false,
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := qt.New(t)
f, r, v := cli()
c.Assert(f, qt.Equals, tt.file)
c.Assert(r, qt.Equals, tt.readonly)
c.Assert(v, qt.Equals, tt.version)
})
}
}
const oteWriteDataForTests = "OTE_WRITE_DATA_FOR_TESTS"
// dealWithTestData asserts that gotContent is equal to data found at path.
//
// If the environment variable [oteWriteDataForTests] is set, this func
// will write gotContent to path instead.
func dealWithTestData(t *testing.T, path, gotContent string) {
t.Helper()
path = strings.ReplaceAll(path, ".go", "")
p, e := filepath.Abs(path)
attest.Ok(t, e)
writeData := os.Getenv(oteWriteDataForTests) != ""
if writeData {
attest.Ok(t,
os.WriteFile(path, []byte(gotContent), 0o644),
)
t.Logf("\n\t written testdata to %s\n", path)
return
}
b, e := os.ReadFile(p)
attest.Ok(t, e)
expectedContent := string(b)
attest.Equal(t, gotContent, expectedContent, attest.Sprintf("path: %s", path))
}
func getDataPath(t *testing.T, testPath, testName string) string { //nolint:unparam
t.Helper()
s := strings.ReplaceAll(testName, " ", "_")
tName := strings.ReplaceAll(s, "/", "_")
path := filepath.Join("testdata", "text_files", testPath, tName) + ".txt"
return path
}