-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponsefile_test.go
178 lines (165 loc) · 3.98 KB
/
responsefile_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
// Copyright (c) 2023, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package responsefile_test
import (
"os"
"slices"
"testing"
"mvdan.cc/responsefile"
)
func TestShorten(t *testing.T) {
t.Parallel()
tests := []struct {
shortenOptions responsefile.ShortenOptions
args []string
wantResponseFile bool
}{
{
shortenOptions: responsefile.ShortenOptions{},
args: []string{},
wantResponseFile: false,
},
{
shortenOptions: responsefile.ShortenOptions{},
args: []string{"foo", "bar", "baz"},
wantResponseFile: false,
},
{
shortenOptions: responsefile.ShortenOptions{
ArgLengthLimit: 20,
},
args: []string{"foo", "bar", "baz"},
wantResponseFile: false,
},
{
shortenOptions: responsefile.ShortenOptions{
ArgLengthLimit: 2,
},
args: []string{"foo", "bar", "baz"},
wantResponseFile: true,
},
{
shortenOptions: responsefile.ShortenOptions{
ArgLengthLimit: -1,
},
args: []string{},
wantResponseFile: false,
},
{
shortenOptions: responsefile.ShortenOptions{
ArgLengthLimit: -1,
},
args: []string{""},
wantResponseFile: false,
},
{
shortenOptions: responsefile.ShortenOptions{
ArgLengthLimit: -1,
},
args: []string{"foo", "bar", "baz"},
wantResponseFile: true,
},
}
for _, test := range tests {
test := test
t.Run("", func(t *testing.T) {
t.Parallel()
shortened, cleanup, err := responsefile.Shorten(test.args, test.shortenOptions)
if err != nil {
if cleanup != nil {
t.Fatal("cleanup func must be nil on error")
}
t.Fatal(err)
}
if cleanup == nil {
t.Fatal("cleanup func must not be nil without an error")
}
t.Cleanup(cleanup)
if !test.wantResponseFile {
if !slices.Equal(shortened, test.args) {
t.Fatalf("did not expect a response file, got %#v", shortened)
}
} else {
if slices.Equal(shortened, test.args) {
t.Fatalf("expected a response file, got %#v", shortened)
}
}
// Ensure that we can roundtrip back to the original args too.
expanded, err := responsefile.Expand(shortened, responsefile.ExpandOptions{})
if err != nil {
t.Fatal(err)
}
if !slices.Equal(expanded, test.args) {
t.Fatalf("roundtrip got %#v, expected %#v", expanded, test.args)
}
})
}
}
func TestExpand(t *testing.T) {
t.Parallel()
tdir := t.TempDir()
atTemp := func(content string) (path string) {
t.Helper()
f, err := os.CreateTemp(tdir, "")
if err != nil {
t.Fatal(err)
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
return "@" + f.Name()
}
tests := []struct {
args []string
wantArgs []string
}{
{
args: []string{},
wantArgs: []string{},
},
{
args: []string{"foo", "bar", "baz"},
wantArgs: []string{"foo", "bar", "baz"},
},
{
args: []string{"foo", atTemp("bar1\nbar2\n"), "baz"},
wantArgs: []string{"foo", "bar1", "bar2", "baz"},
},
{
args: []string{atTemp("crlf\r\n"), atTemp(""), atTemp("nolf")},
wantArgs: []string{"crlf", "nolf"},
},
{
args: []string{atTemp("escaped\\\\\\\nchars")},
// TODO: newlines can be escaped, so this should be:
// wantArgs: []string{"escaped\\\nchars"},
wantArgs: []string{"escaped\\", "chars"},
},
{
args: []string{atTemp("l1_1\n" +
atTemp("l2_1\nl2_2\n") + "\n" +
atTemp(atTemp("l3")) + "\n" +
atTemp("l2_3\nl2_4\n") + "\n" +
"l1_2\n"),
},
wantArgs: []string{"l1_1", "l2_1", "l2_2", "l3", "l2_3", "l2_4", "l1_2"},
},
}
for _, test := range tests {
test := test
t.Run("", func(t *testing.T) {
t.Parallel()
expanded, err := responsefile.Expand(test.args, responsefile.ExpandOptions{})
if err != nil {
t.Fatal(err)
}
if !slices.Equal(expanded, test.wantArgs) {
t.Fatalf("expand got %#v, expected %#v", expanded, test.wantArgs)
}
})
}
}