-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhpatch_test.go
More file actions
746 lines (708 loc) · 25.3 KB
/
Copy pathhpatch_test.go
File metadata and controls
746 lines (708 loc) · 25.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package hpatch
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/yusing/hpatch/internal/patchtest"
)
func TestHPatch2NormalMultiFileWorkflow(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "a.txt", "alpha old\nkeep\nend\n", 0o750)
if err := os.Chmod(filepath.Join(root, "a.txt"), 0o750); err != nil {
t.Fatal(err)
}
writeTestFile(t, root, "b.txt", "x x x\n", 0o640)
writeTestFile(t, root, "obsolete.txt", "remove me\n", 0o640)
script := strings.Join([]string{
"in a.txt",
"type " + row(1, "alpha old") + ` "old" "new"`,
"type- " + row(2, "keep") + ` "// note\n"`,
"type " + row(3, "end") + ` ""`,
"in b.txt",
"type " + row(1, "x x x") + ` "x" 2 "y"`,
"new draft.txt",
`type "foo bar"`,
"mv final.txt",
"in obsolete.txt",
"rm",
}, "\n")
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if !strings.Contains(result.Report, "files add=1 update=2 move=0 delete=1\n") {
t.Fatalf("report = %q", result.Report)
}
want := map[string]string{
"a.txt": "alpha new\n// note\nkeep\n",
"b.txt": "y y x\n",
"final.txt": "foo bar",
}
if got := readTree(t, root); !reflect.DeepEqual(got, want) {
t.Fatalf("tree = %#v, want %#v", got, want)
}
if mode := testFileMode(t, filepath.Join(root, "a.txt")); mode != 0o750 {
t.Fatalf("a.txt mode = %o, want 750", mode)
}
}
func TestHPatch2TranslateMatchesNormalMode(t *testing.T) {
initial := map[string]string{
"code.go": "package sample\n\nvar value = old\n",
"obsolete.go": "package obsolete\n",
}
root := t.TempDir()
for path, content := range initial {
writeTestFile(t, root, path, content, 0o644)
}
script := strings.Join([]string{
"in code.go",
"type " + row(3, "var value = old") + ` "old" "current"`,
"mv current.go",
"new note.txt",
`type "hello world\n"`,
"in obsolete.go",
"rm",
}, "\n")
result, err := translateForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("TranslateForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if !strings.Contains(result.Report, "files add=1 update=1 move=1 delete=1\n") {
t.Fatalf("report = %q", result.Report)
}
if got := readTree(t, root); !reflect.DeepEqual(got, initial) {
t.Fatalf("translate mutated tree: %#v", got)
}
translated, err := patchtest.Apply(initial, string(result.Patch))
if err != nil {
t.Fatalf("applying translated patch: %v\n%s", err, string(result.Patch))
}
want := map[string]string{
"current.go": "package sample\n\nvar value = current\n",
"note.txt": "hello world\n",
}
if !reflect.DeepEqual(translated, want) {
t.Fatalf("translated tree = %#v, want %#v\n%s", translated, want, string(result.Patch))
}
}
func TestHPatch2LineAndRangeTerminatorSemantics(t *testing.T) {
tests := []struct {
name, content, script, want string
}{
{"line preserves CRLF", "one\r\ntwo\r\n", "in file.txt\ntype " + row(1, "one") + ` "ONE"`, "ONE\r\ntwo\r\n"},
{"range preserves final CR", "one\rtwo\rthree", "in file.txt\ntype " + row(1, "one") + ".." + row(2, "two") + ` "both"`, "both\rthree"},
{"unterminated final replacement", "one\ntwo", "in file.txt\ntype " + row(2, "two") + ` "TWO"`, "one\nTWO"},
{"empty line removes LF", "one\ntwo\n", "in file.txt\ntype " + row(1, "one") + ` ""`, "two\n"},
{"empty line removes CRLF", "one\r\ntwo\r\n", "in file.txt\ntype " + row(1, "one") + ` ""`, "two\r\n"},
{"empty line removes standalone CR", "one\rtwo\r", "in file.txt\ntype " + row(1, "one") + ` ""`, "two\r"},
{"empty unterminated final line", "one\ntwo", "in file.txt\ntype " + row(2, "two") + ` ""`, "one\n"},
{"empty range removes final terminator", "one\ntwo\nthree\n", "in file.txt\ntype " + row(1, "one") + ".." + row(2, "two") + ` ""`, "three\n"},
{"empty heredoc removes line", "one\ntwo\n", "in file.txt\ntype " + row(1, "one") + " <<PATCH\nPATCH\n", "two\n"},
{"empty text replacement keeps terminator", "one\ntwo\n", "in file.txt\ntype " + row(1, "one") + ` "one" ""`, "\ntwo\n"},
{"explicit terminator creates blank line", "one\ntwo\n", "in file.txt\ntype " + row(1, "one") + ` "\n"`, "\ntwo\n"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", test.content, 0o644)
result, err := applyForHostAtTest(t, root, test.script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != test.want {
t.Fatalf("file = %q, want %q", got, test.want)
}
})
}
}
func TestHPatch2EmptyInitializerRemainsEmptyFile(t *testing.T) {
root := t.TempDir()
result, err := applyForHostAtTest(t, root, "new empty.txt\ntype \"\"\n", "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "empty.txt"); got != "" {
t.Fatalf("file = %q, want empty", got)
}
}
func TestHPatch2SameBoundaryInsertionsKeepScriptOrder(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "target\n", 0o644)
target := row(1, "target")
script := strings.Join([]string{
"in file.txt",
`type- ` + target + ` "first\n"`,
`type- ` + target + ` "second\n"`,
`type+ ` + target + ` "after-one\n"`,
`type+ ` + target + ` "after-two\n"`,
}, "\n")
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
want := "first\nsecond\ntarget\nafter-one\nafter-two\n"
if got := readTestFile(t, root, "file.txt"); got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2InsertionsAtReplacementBoundariesAreAllowed(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "target\n", 0o644)
target := row(1, "target")
script := strings.Join([]string{
"in file.txt",
`type ` + target + ` "replacement"`,
`type- ` + target + ` "before\n"`,
`type+ ` + target + ` "after\n"`,
}, "\n")
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got, want := readTestFile(t, root, "file.txt"), "before\nreplacement\nafter\n"; got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2RejectsInvalidTargetsAtomically(t *testing.T) {
tests := []struct{ name, script, reason string }{
{"stale", "in file.txt\ntype 2:0000 \"B\"", "row-stale"},
{"missing", "in file.txt\ntype 9:0000 \"B\"", "row-missing"},
{"incomplete", "in file.txt\ntype " + row(1, "alpha") + ` "alpha" 2 "A"`, "occurrence-missing"},
{"invalid count", "in file.txt\ntype " + row(1, "alpha") + ` "alpha" 0 "A"`, "invalid-count"},
{"reversed", "in file.txt\ntype " + row(2, "beta") + ".." + row(1, "alpha") + ` ""`, "target-order"},
{"overlap", "in file.txt\ntype " + row(1, "alpha") + ` "A"` + "\ntype " + row(1, "alpha") + ` ""`, "edit-conflict"},
{"insertion inside replacement", "in file.txt\ntype " + row(1, "alpha") + ` "A"` + "\ntype- " + row(1, "alpha") + ` "ph" "x"`, "edit-conflict"},
{"introduced", "in file.txt\ntype+ " + row(1, "alpha") + ` "new\n"` + "\ntype " + row(1, "alpha") + ` "new" "NEW"`, "occurrence-missing"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\nbeta\n", 0o644)
before := readTestFile(t, root, "file.txt")
result, err := applyForHostAtTest(t, root, test.script, "")
if err == nil || !strings.Contains(result.Diagnostic, test.reason) {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q; want %q", err, result.Diagnostic, test.reason)
}
if got := readTestFile(t, root, "file.txt"); got != before {
t.Fatalf("rejection changed file to %q", got)
}
})
}
}
func TestHPatch2RelocatesUniqueRowsAfterPriorEdits(t *testing.T) {
tests := []struct {
name string
content string
script string
want string
}{
{
name: "line shifted down",
content: "inserted\nalpha\nbeta\n",
script: "in file.txt\ntype " + row(1, "alpha") + ` "ALPHA"`,
want: "inserted\nALPHA\nbeta\n",
},
{
name: "line shifted above prior eof",
content: "alpha\n",
script: "in file.txt\ntype " + row(2, "alpha") + ` "ALPHA"`,
want: "ALPHA\n",
},
{
name: "range endpoints shifted down",
content: "inserted\nalpha\nbeta\ngamma\n",
script: "in file.txt\ntype " + row(1, "alpha") + ".." + row(2, "beta") + ` "AB"`,
want: "inserted\nAB\ngamma\n",
},
{
name: "text anchor shifted down",
content: "inserted\nalpha old\nbeta\n",
script: "in file.txt\ntype " + row(1, "alpha old") + ` "old" "new"`,
want: "inserted\nalpha new\nbeta\n",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", test.content, 0o644)
result, err := applyForHostAtTest(t, root, test.script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != test.want {
t.Fatalf("file = %q, want %q", got, test.want)
}
})
}
}
func TestHPatch2ResolvesPostEditCoordinateForUnchangedBaselineRow(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\n}\nbeta\n}\n", 0o644)
script := "in file.txt\n" +
"type- " + row(1, "alpha") + ` "one\ntwo\n"` + "\n" +
"type+ " + row(6, "}") + ` "tail\n"`
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
want := "one\ntwo\nalpha\n}\nbeta\n}\ntail\n"
if got := readTestFile(t, root, "file.txt"); got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2DoesNotResolvePostEditCoordinateForIntroducedRow(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\nbeta\n", 0o644)
script := "in file.txt\n" +
"type+ " + row(1, "alpha") + ` "new\n"` + "\n" +
"type " + row(2, "new") + ` "NEW"`
result, err := applyForHostAtTest(t, root, script, "")
if err == nil || !strings.Contains(result.Diagnostic, "row-stale") {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != "alpha\nbeta\n" {
t.Fatalf("rejection changed file to %q", got)
}
}
func TestHPatch2RejectsAmbiguousRelocatedRow(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "other\nalpha\nalpha\n", 0o644)
before := readTestFile(t, root, "file.txt")
script := "in file.txt\ntype " + row(1, "alpha") + ` "ALPHA"`
result, err := applyForHostAtTest(t, root, script, "")
if err == nil || !strings.Contains(result.Diagnostic, "row-stale") || !strings.Contains(result.Diagnostic, "ambiguous across 2 rows") {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != before {
t.Fatalf("rejection changed file to %q", got)
}
}
func TestHPatch2IgnoresRedundantStaleLiteralAnchor(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\nunique target\nomega\n", 0o644)
script := `in file.txt
type 1:ffff "unique target" "replacement"`
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if content := readTestFile(t, root, "file.txt"); content != "alpha\nreplacement\nomega\n" {
t.Fatalf("content = %q", content)
}
}
func TestHPatch2RejectsStaleLiteralAnchorWhenLiteralIsAmbiguous(t *testing.T) {
root := t.TempDir()
before := "target\nanchor\ntarget\n"
writeTestFile(t, root, "file.txt", before, 0o644)
script := `in file.txt
type 2:ffff "target" "replacement"`
result, err := applyForHostAtTest(t, root, script, "")
if err == nil {
t.Fatalf("ApplyForHost() unexpectedly succeeded, diagnostic %q", result.Diagnostic)
}
if !strings.Contains(result.Diagnostic, "reason row-stale") {
t.Fatalf("diagnostic = %q", result.Diagnostic)
}
if content := readTestFile(t, root, "file.txt"); content != before {
t.Fatalf("content = %q, want unchanged %q", content, before)
}
}
func TestHPatch2NewFileInitializerIsImmediate(t *testing.T) {
tests := []struct{ name, script, wantMessage string }{
{"existing", "in existing.txt\ntype \"new\"", "bare type VALUE only initializes the immediately preceding new; editing an existing file requires a line, range, or text target"},
{"intervening", "new new.txt\nin existing.txt\nin new.txt\ntype \"new\"", ""},
{"second", "new new.txt\ntype \"one\"\ntype \"two\"", ""},
{"target new", "new new.txt\ntype \"one\\n\"\ntype " + row(1, "one") + ` "ONE"`, ""},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "existing.txt", "old\n", 0o644)
result, err := applyForHostAtTest(t, root, test.script, "")
if err == nil || !strings.Contains(result.Diagnostic, "initialization") {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if test.wantMessage != "" && !strings.Contains(result.Diagnostic, test.wantMessage) {
t.Fatalf("diagnostic = %q, want message %q", result.Diagnostic, test.wantMessage)
}
if got := readTree(t, root); !reflect.DeepEqual(got, map[string]string{"existing.txt": "old\n"}) {
t.Fatalf("rejection changed tree: %#v", got)
}
})
}
}
func TestHPatch2RejectsRemovedGrammar(t *testing.T) {
for _, command := range []string{`tsel 0000 "x"`, `rsel 0000 0000`, "copy", "cut", "paste", "commit", "del " + row(1, "x"), "type <<BODY\nx\nBODY"} {
t.Run(strings.Fields(command)[0], func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "x\n", 0o644)
result, err := applyForHostAtTest(t, root, "in file.txt\n"+command, "")
if err == nil || !strings.Contains(result.Diagnostic, "reason script-syntax") {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
})
}
}
func TestHPatch2FixedHeredocAndInlineInsertion(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "target\n", 0o644)
script := "in file.txt\n" +
"type- " + row(1, "target") + ` "// comment\n"` + "\n" +
"type+ " + row(1, "target") + " <<PATCH\nmultiline\nvalue\nPATCH\n"
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
want := "// comment\ntarget\nmultiline\nvalue\n"
if got := readTestFile(t, root, "file.txt"); got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2HeredocValueSupportsTextTarget(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "prefix needle suffix\n", 0o644)
script := "in file.txt\ntype " + row(1, "prefix needle suffix") + " \"needle\" <<PATCH\n" +
"multiline\nvalue\nPATCH\n"
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
want := "prefix multiline\nvalue\n suffix\n"
if got := readTestFile(t, root, "file.txt"); got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2UnanchoredLiteralTargetsUseImmutableBaseline(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha x\nbeta x\n", 0o644)
script := strings.Join([]string{
"in file.txt",
`type "alpha" "ALPHA"`,
`type+ "x" 2 "!"`,
}, "\n")
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got, want := readTestFile(t, root, "file.txt"), "ALPHA x!\nbeta x!\n"; got != want {
t.Fatalf("file = %q, want %q", got, want)
}
}
func TestHPatch2UnanchoredLiteralHeredocAndMissingOccurrence(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "before needle after\n", 0o644)
script := "in file.txt\ntype \"needle\" <<PATCH\nmultiline\nvalue\nPATCH\n"
result, err := applyForHostAtTest(t, root, script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got, want := readTestFile(t, root, "file.txt"), "before multiline\nvalue\n after\n"; got != want {
t.Fatalf("file = %q, want %q", got, want)
}
writeTestFile(t, root, "missing.txt", "one x\n", 0o644)
before := readTestFile(t, root, "missing.txt")
result, err = applyForHostAtTest(t, root, "in missing.txt\ntype \"x\" 2 \"y\"", "")
if err == nil || !strings.Contains(result.Diagnostic, "occurrence-missing") {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "missing.txt"); got != before {
t.Fatalf("rejection changed file to %q", got)
}
}
func TestHPatch2QuotedDoubleLessRemainsInlineText(t *testing.T) {
tests := []struct {
name string
path string
initial string
script func(string) string
want string
}{
{
name: "initializer",
path: "file.txt",
script: func(string) string {
return `new file.txt` + "\n" + `type "a << b"`
},
want: "a << b",
},
{
name: "initializer after escaped quote",
path: "file.txt",
script: func(string) string {
return `new file.txt` + "\n" + `type "a \"<< b"`
},
want: `a "<< b`,
},
{
name: "replacement value",
path: "file.txt",
initial: "old\n",
script: func(string) string {
return "in file.txt\ntype " + row(1, "old") + ` "a << b"`
},
want: "a << b\n",
},
{
name: "target literal",
path: "file.txt",
initial: "a << b\n",
script: func(string) string {
return "in file.txt\ntype " + row(1, "a << b") + ` "a << b" "changed"`
},
want: "changed\n",
},
{
name: "path",
path: "a<<b.txt",
initial: "old\n",
script: func(string) string {
return "in a<<b.txt\ntype " + row(1, "old") + ` "changed"`
},
want: "changed\n",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
if test.initial != "" {
writeTestFile(t, root, test.path, test.initial, 0o644)
}
result, err := applyForHostAtTest(t, root, test.script(test.path), "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, test.path); got != test.want {
t.Fatalf("file = %q, want %q", got, test.want)
}
})
}
}
func TestHPatch2InvalidHeredocIsOneHeaderOwnedFailure(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "unchanged\n", 0o644)
script := "in file.txt\ntype " + row(1, "unchanged") + " <<BODY\n" +
"rm\nBODY\n"
result, err := applyForHostAtTest(t, root, script, "")
if err == nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if strings.Count(result.Diagnostic, ": command") != 1 ||
!strings.Contains(result.Diagnostic, "command 2") ||
!strings.Contains(result.Diagnostic, "requires an unquoted <<PATCH") {
t.Fatalf("diagnostic = %q", result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != "unchanged\n" {
t.Fatalf("rejection changed file to %q", got)
}
}
func TestHPatch2TargetLiteralRejectsC0ControlsExceptTab(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "a\tb\n", 0o644)
valid := "in file.txt\ntype " + row(1, "a\tb") + ` "a\tb" "ok"`
result, err := applyForHostAtTest(t, root, valid, "")
if err != nil {
t.Fatalf("tab target error = %v, diagnostic %q", err, result.Diagnostic)
}
writeTestFile(t, root, "file.txt", "a\x01b\n", 0o644)
invalid := "in file.txt\ntype " + row(1, "a\x01b") + ` "a\u0001b" "bad"`
result, err = applyForHostAtTest(t, root, invalid, "")
if err == nil || !strings.Contains(result.Diagnostic, "forbidden control character") {
t.Fatalf("control target error = %v, diagnostic %q", err, result.Diagnostic)
}
}
func TestHPatch2MultilineLiteralTargets(t *testing.T) {
tests := []struct {
name string
content string
script string
want string
}{
{
name: "unanchored replacement",
content: "before\nfirst\nsecond\nafter\n",
script: `in file.txt` + "\n" + `type "first\nsecond" "replacement"`,
want: "before\nreplacement\nafter\n",
},
{
name: "row anchored replacement",
content: "skip first\nfirst\nsecond\nafter\n",
script: "in file.txt\ntype " + row(2, "first") + ` "first\nsecond" "replacement"`,
want: "skip first\nreplacement\nafter\n",
},
{
name: "unicode escaped line feed",
content: "first\nsecond\n",
script: `in file.txt` + "\n" + `type "first\u000Asecond" "replacement"`,
want: "replacement\n",
},
{
name: "occurrence count",
content: "first\nsecond\nfirst\nsecond\n",
script: `in file.txt` + "\n" + `type "first\nsecond" 2 "replacement"`,
want: "replacement\nreplacement\n",
},
{
name: "target includes trailing LF",
content: "first\nsecond\n",
script: `in file.txt` + "\n" + `type "first\n" "FIRST\n"`,
want: "FIRST\nsecond\n",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", test.content, 0o644)
result, err := applyForHostAtTest(t, root, test.script, "")
if err != nil {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != test.want {
t.Fatalf("file = %q, want %q", got, test.want)
}
})
}
}
func TestHPatch2MultilineLiteralTargetRejectionsAreAtomic(t *testing.T) {
tests := []struct {
name, content, script, diagnostic string
}{
{
name: "missing exact multiline text",
content: "first\nchanged\n",
script: `in file.txt` + "\n" + `type "first\nsecond" "replacement"`,
diagnostic: "occurrence-missing",
},
{
name: "missing trailing LF",
content: "first",
script: `in file.txt` + "\n" + `type "first\n" "replacement"`,
diagnostic: "occurrence-missing",
},
{
name: "ambiguous stale anchor",
content: "first\nsecond\nfirst\nsecond\n",
script: `in file.txt` + "\n" + `type 1:ffff "first\nsecond" "replacement"`,
diagnostic: "row-stale",
},
{
name: "raw physical newline",
content: "first\nsecond\n",
script: "in file.txt\ntype \"first\nsecond\" \"replacement\"",
diagnostic: "script-syntax",
},
{
name: "escaped carriage return",
content: "first\rsecond\n",
script: `in file.txt` + "\n" + `type "first\rsecond" "replacement"`,
diagnostic: "forbidden carriage return",
},
{
name: "unicode carriage return",
content: "first\rsecond\n",
script: `in file.txt` + "\n" + `type "first\u000Dsecond" "replacement"`,
diagnostic: "forbidden carriage return",
},
{
name: "forbidden control",
content: "first\x01second\n",
script: `in file.txt` + "\n" + `type "first\u0001second" "replacement"`,
diagnostic: "forbidden control character",
},
{
name: "empty target",
content: "first\n",
script: `in file.txt` + "\n" + `type "" "replacement"`,
diagnostic: "target literal must not be empty",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", test.content, 0o644)
result, err := applyForHostAtTest(t, root, test.script, "")
if err == nil || !strings.Contains(result.Diagnostic, test.diagnostic) {
t.Fatalf("ApplyForHost() error = %v, diagnostic %q; want %q", err, result.Diagnostic, test.diagnostic)
}
if got := readTestFile(t, root, "file.txt"); got != test.content {
t.Fatalf("rejection changed file to %q", got)
}
})
}
}
func row(line int, content string) string {
return fmt.Sprintf("%d:%s", line, hashLine(content))
}
func applyForHostAtTest(t *testing.T, rootPath, script, dataDirectory string) (HostTranslation, error) {
t.Helper()
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
defer root.Close()
return ApplyForHost(t.Context(), Workspace{Root: root}, script, dataDirectory)
}
func translateForHostAtTest(t *testing.T, rootPath, script, dataDirectory string) (HostTranslation, error) {
t.Helper()
root, err := os.OpenRoot(rootPath)
if err != nil {
t.Fatal(err)
}
defer root.Close()
return TranslateForHost(t.Context(), Workspace{Root: root}, script, dataDirectory)
}
func writeTestFile(t *testing.T, root, path, content string, mode fs.FileMode) {
t.Helper()
fullPath := filepath.Join(root, path)
if err := os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(fullPath, []byte(content), mode); err != nil {
t.Fatal(err)
}
}
func readTestFile(t *testing.T, root, path string) string {
t.Helper()
content, err := os.ReadFile(filepath.Join(root, path))
if err != nil {
t.Fatal(err)
}
return string(content)
}
func readTree(t *testing.T, root string) map[string]string {
t.Helper()
files := make(map[string]string)
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
relative, err := filepath.Rel(root, path)
if err != nil {
return err
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
files[relative] = string(content)
return nil
})
if err != nil {
t.Fatal(err)
}
return files
}
func testFileMode(t *testing.T, path string) fs.FileMode {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
return info.Mode().Perm()
}