-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation_test.go
More file actions
277 lines (259 loc) · 9.15 KB
/
Copy pathvalidation_test.go
File metadata and controls
277 lines (259 loc) · 9.15 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package hpatch
import (
"os"
"reflect"
"strings"
"testing"
)
func TestHPatch2IndentationOnlyReplacementOffersExactCorrection(t *testing.T) {
rootPath := t.TempDir()
writeTestFile(t, rootPath, "script.sh", "header\n\texit \"$status\"\n", 0o644)
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
defer root.Close()
command := "type " + row(2, "\texit \"$status\"") + ` "exit \"$status\"\n"`
script := "in script.sh\n" + command
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, script, t.TempDir())
if err == nil {
t.Fatal("indentation-only replacement unexpectedly succeeded")
}
wantCommand := "type " + row(2, "\texit \"$status\"") + ` "\texit \"$status\"\n"`
want := []CommandCorrection{{Command: 2, Replacement: wantCommand}}
if !reflect.DeepEqual(result.Corrections, want) {
t.Fatalf("corrections = %#v, want %#v", result.Corrections, want)
}
wantRejections := []HostRejection{{
Command: 2, SourceLine: 2, Operation: "type", Target: "line",
Reason: "edit-conflict", Path: "script.sh",
}}
if !reflect.DeepEqual(result.Rejections, wantRejections) {
t.Fatalf("rejections = %#v, want %#v", result.Rejections, wantRejections)
}
if !strings.Contains(result.Diagnostic, "indentation-only change") {
t.Fatalf("diagnostic = %q", result.Diagnostic)
}
if strings.Contains(result.Diagnostic, "1:"+hashLine(`exit "$status"`)) {
t.Fatalf("diagnostic contains fabricated target row: %q", result.Diagnostic)
}
metrics := result.Invocation.value
if metrics.Reasons[reasonEditConflict] != 1 ||
metrics.CommandReasons[commandOperationIndex("type")][reasonEditConflict] != 1 {
t.Fatalf("indentation correction metrics = %+v", metrics)
}
}
func TestHostRejectionsPreserveGroupedCommandIdentityWithoutSourceText(t *testing.T) {
rootPath := t.TempDir()
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
defer root.Close()
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, "bad\nworse\n", t.TempDir())
if err == nil {
t.Fatal("invalid commands unexpectedly succeeded")
}
want := []HostRejection{
{Command: 1, SourceLine: 1, Operation: "bad", Reason: "script-syntax"},
{Command: 2, SourceLine: 2, Operation: "worse", Reason: "script-syntax"},
}
if !reflect.DeepEqual(result.Rejections, want) {
t.Fatalf("rejections = %#v, want %#v", result.Rejections, want)
}
}
func TestHPatch2InvalidGoAfterMoveUsesContentOrigin(t *testing.T) {
tests := []struct {
name string
setup func(*testing.T, string)
script string
}{
{
name: "existing file",
setup: func(t *testing.T, root string) {
writeTestFile(t, root, "old.go", "package p\n\nvar value = 1\n", 0o644)
},
script: "in old.go\ntype " + row(3, "var value = 1") + ` "var ="` + "\nmv moved.go",
},
{
name: "new file initializer",
setup: func(*testing.T, string) {},
script: `new old.go
type "package p\n\nvar =\n"
mv moved.go`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
test.setup(t, root)
stdout, stderr, exitCode := runForTest(root, nil, test.script)
if exitCode != 1 || stdout != "" ||
!strings.Contains(stderr, `operation "type"`) ||
!strings.Contains(stderr, `path "moved.go"`) ||
strings.Contains(stderr, `operation "mv"`) {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
})
}
}
func TestHPatch2MoveOnlyGoValidationUsesMoveOrigin(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "source.txt", "not Go\n", 0o644)
stdout, stderr, exitCode := runForTest(root, nil, "in source.txt\nmv moved.go")
if exitCode != 1 || stdout != "" ||
!strings.Contains(stderr, `operation "mv"`) ||
!strings.Contains(stderr, `path "moved.go"`) {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
}
func TestHPatch2ChangedGoFilesAreFormatted(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.go", "package p\n\nvar value=1\n", 0o644)
script := "in file.go\ntype " + row(3, "var value=1") + ` "var value=2"`
_, stderr, exitCode := runForTest(root, nil, script)
if exitCode != 0 {
t.Fatalf("Run() = exit %d, stderr %q", exitCode, stderr)
}
if got, want := readTestFile(t, root, "file.go"), "package p\n\nvar value = 2\n"; got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2InvalidGoRejectsAtomically(t *testing.T) {
rootPath := t.TempDir()
before := "package p\n\nvar value = 1\n"
writeTestFile(t, rootPath, "file.go", before, 0o644)
script := "in file.go\ntype " + row(3, "var value = 1") + ` "var ="`
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, script, t.TempDir())
root.Close()
if err == nil {
t.Fatal("invalid Go unexpectedly translated")
}
want := []HostRejection{{
Command: 2, SourceLine: 2, Operation: "type", Target: "line",
Reason: "language-syntax", Path: "file.go", GeneratedLine: 3, GeneratedColumn: 5,
}}
if !reflect.DeepEqual(result.Rejections, want) {
t.Fatalf("rejections = %#v, want %#v", result.Rejections, want)
}
stdout, stderr, exitCode := runForTest(rootPath, nil, script)
if exitCode != 1 || stdout != "" || !strings.Contains(stderr, "language-syntax") {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
if !strings.Contains(stderr, "generated Go near 3:5\n") || !strings.Contains(stderr, "> 3 | var =\n") {
t.Fatalf("diagnostic lacks generated-source context: %q", stderr)
}
if got := readTestFile(t, rootPath, "file.go"); got != before {
t.Fatalf("file = %q, want unchanged", got)
}
}
func TestHPatch2InvalidGoAttributesCausativeMutation(t *testing.T) {
rootPath := t.TempDir()
before := "package p\n\nvar first = 1\nvar second = 2\n"
writeTestFile(t, rootPath, "file.go", before, 0o644)
script := strings.Join([]string{
"in file.go",
"type " + row(3, "var first = 1") + ` "var ="`,
"type " + row(4, "var second = 2") + ` "var second = 3"`,
}, "\n")
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, script, t.TempDir())
root.Close()
if err == nil {
t.Fatal("invalid Go unexpectedly translated")
}
want := []HostRejection{{
Command: 2, SourceLine: 2, Operation: "type", Target: "line",
Reason: "language-syntax", Path: "file.go", GeneratedLine: 3, GeneratedColumn: 5,
}}
if !reflect.DeepEqual(result.Rejections, want) {
t.Fatalf("rejections = %#v, want %#v", result.Rejections, want)
}
if got := readTestFile(t, rootPath, "file.go"); got != before {
t.Fatalf("file = %q, want unchanged", got)
}
}
func TestHPatch2InvalidGoReportsMultilineValueRow(t *testing.T) {
rootPath := t.TempDir()
before := "package p\n\nvar value = 1\n"
writeTestFile(t, rootPath, "file.go", before, 0o644)
script := "in file.go\ntype " + row(3, "var value = 1") + " <<PATCH\n" +
"var first = 1\n" +
"var =\n" +
"var third = 3\n" +
"PATCH\n"
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, script, t.TempDir())
root.Close()
if err == nil {
t.Fatal("invalid Go unexpectedly translated")
}
want := []HostRejection{{
Command: 2, SourceLine: 2, Operation: "type", Target: "line",
Reason: "language-syntax", Path: "file.go", GeneratedLine: 4, GeneratedColumn: 5, ValueLine: 2,
}}
if !reflect.DeepEqual(result.Rejections, want) {
t.Fatalf("rejections = %#v, want %#v", result.Rejections, want)
}
_, stderr, exitCode := runForTest(rootPath, nil, script)
if exitCode != 1 {
t.Fatalf("Run() = exit %d, stderr %q", exitCode, stderr)
}
for _, fragment := range []string{
"command 2 multiline value near row 2\n",
" 2.1 | var first = 1\n",
"> 2.2 | var =\n",
" 2.3 | var third = 3\n",
} {
if !strings.Contains(stderr, fragment) {
t.Fatalf("diagnostic lacks %q:\n%s", fragment, stderr)
}
}
if got := readTestFile(t, rootPath, "file.go"); got != before {
t.Fatalf("file = %q, want unchanged", got)
}
}
func TestHPatch2MultilineValueRowsUsePhysicalFraming(t *testing.T) {
rootPath := t.TempDir()
script := "new file.go\ntype <<PATCH\npackage p\rvar =\nPATCH\n"
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
result, err := TranslateForHost(t.Context(), Workspace{Root: root}, script, t.TempDir())
root.Close()
if err == nil {
t.Fatal("invalid Go unexpectedly translated")
}
if len(result.Rejections) != 1 || result.Rejections[0].Command != 2 || result.Rejections[0].ValueLine != 1 {
t.Fatalf("rejections = %#v, want command 2 physical value row 1", result.Rejections)
}
_, stderr, exitCode := runForTest(rootPath, nil, script)
if exitCode != 1 {
t.Fatalf("Run() = exit %d, stderr %q", exitCode, stderr)
}
for _, fragment := range []string{
"command 2 multiline value near row 1\n",
"> 2.1 | package p\\rvar =\n",
} {
if !strings.Contains(stderr, fragment) {
t.Fatalf("diagnostic lacks %q:\n%s", fragment, stderr)
}
}
if strings.Contains(stderr, "2.2 |") {
t.Fatalf("diagnostic split an embedded carriage return into another value row:\n%s", stderr)
}
if _, err := os.Stat(rootPath + "/file.go"); !os.IsNotExist(err) {
t.Fatalf("rejected translation created file.go: %v", err)
}
}