-
Notifications
You must be signed in to change notification settings - Fork 0
/
jet_test.go
1087 lines (948 loc) · 26.2 KB
/
jet_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
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"flag"
"io"
"io/fs"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"testing"
)
// TestPairMatch tests the match function of the pair struct.
func TestPairMatch(t *testing.T) {
p := pair{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
}
tests := []struct {
input []byte
expected bool
}{
{[]byte("foobar"), true},
{[]byte("baz"), false},
{[]byte("foo baz"), true},
{[]byte(""), false},
}
for _, test := range tests {
result := p.match(test.input)
if result != test.expected {
t.Errorf("pair.match(%q) = %v; want %v", test.input, result, test.expected)
}
}
}
// TestPairReplaceAll tests the replaceAll function of the pair struct.
func TestPairReplaceAll(t *testing.T) {
p := pair{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
}
tests := []struct {
input []byte
expected []byte
}{
{[]byte("foo foo baz"), []byte("bar bar baz")},
{[]byte("no match here"), []byte("no match here")},
{[]byte("foofoo"), []byte("barbar")},
}
for _, test := range tests {
result := p.replaceAll(test.input)
if !bytes.Equal(result, test.expected) {
t.Errorf("pair.replaceAll(%q) = %q; want %q", test.input, result, test.expected)
}
}
}
// TestPairsetMatch tests the match function of the pairset type.
func TestPairsetMatch(t *testing.T) {
ps := pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
{
pattern: regexp.MustCompile("baz"),
replacement: []byte("qux"),
},
}
tests := []struct {
input []byte
expected bool
}{
{[]byte("hello foo"), true},
{[]byte("hello baz"), true},
{[]byte("hello world"), false},
}
for _, test := range tests {
result := ps.match(test.input)
if result != test.expected {
t.Errorf("pairset.match(%q) = %v; want %v", test.input, result, test.expected)
}
}
}
// TestPairsetReplaceAll tests the replaceAll function of the pairset type.
func TestPairsetReplaceAll(t *testing.T) {
ps := pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
{
pattern: regexp.MustCompile("baz"),
replacement: []byte("qux"),
},
}
tests := []struct {
input []byte
expected []byte
}{
{[]byte("foo baz foo baz"), []byte("bar qux bar qux")},
{[]byte("no match here"), []byte("no match here")},
{[]byte("foo and baz"), []byte("bar and qux")},
}
for _, test := range tests {
result := ps.replaceAll(test.input)
if !bytes.Equal(result, test.expected) {
t.Errorf("pairset.replaceAll(%q) = %q; want %q", test.input, result, test.expected)
}
}
}
func TestPairsetSetValidPattern(t *testing.T) {
var ps pairset
// Reset the flag CommandLine so it doesn't interfere with the test
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Provide a valid pattern and simulate command line arguments for the replacement.
// Since Set reads the replacement from flag.Arg(0), we must simulate it.
os.Args = []string{"cmd", "replacement"}
flag.Parse()
if err := ps.Set("foo"); err != nil {
t.Errorf("expected no error for valid pattern, got %v", err)
}
if len(ps) != 1 {
t.Fatalf("expected 1 pair, got %d", len(ps))
}
if ps[0].pattern.String() != "foo" {
t.Errorf("expected pattern to be 'foo', got %q", ps[0].pattern.String())
}
if string(ps[0].replacement) != "replacement" {
t.Errorf("expected replacement to be 'replacement', got %q", ps[0].replacement)
}
}
// TestPairsetSetInvalidPattern tests the Set method of pairset for invalid patterns.
func TestPairsetSetInvalidPattern(t *testing.T) {
var ps pairset
// The pattern "(?" is invalid
err := ps.Set("(?")
if err == nil {
t.Errorf("expected error for invalid pattern, got nil")
}
}
// TestWalkerEdit tests the edit function of the walker struct with a temporary file.
func TestWalkerEdit(t *testing.T) {
// Create a temporary file with initial content.
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := []byte("foo baz foo baz")
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
// Initialize the walker.
w := &walker{
ToStdout: false,
Glob: "*",
IsVerbose: false,
MaxDepth: -1,
IncludeHidden: true,
ReplaceNames: false,
NamesOnly: false,
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
{
pattern: regexp.MustCompile("baz"),
replacement: []byte("qux"),
},
},
WaitGroup: new(sync.WaitGroup),
}
// Perform the edit operation.
w.edit(tmpfile.Name())
// Read back the content of the file.
newContent, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
expected := []byte("bar qux bar qux")
if !bytes.Equal(newContent, expected) {
t.Errorf("After edit, content = %q; want %q", newContent, expected)
}
}
// TestWalkerEditFilename tests the editFilename function of the walker struct.
func TestWalkerEditFilename(t *testing.T) {
// Create a temporary directory.
tmpDir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create a file with a name that needs to be replaced.
oldPath := filepath.Join(tmpDir, "foo.txt")
newPath := filepath.Join(tmpDir, "bar.txt")
if _, err := os.Create(oldPath); err != nil {
t.Fatal(err)
}
// Initialize the walker.
w := &walker{
ToStdout: false,
Glob: "*",
IsVerbose: false,
MaxDepth: -1,
IncludeHidden: true,
ReplaceNames: true,
NamesOnly: true,
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
},
}
// Perform the editFilename operation.
resultPath := w.editFilename(oldPath)
// Check if the file has been renamed.
if resultPath != newPath {
t.Errorf("editFilename result = %q; want %q", resultPath, newPath)
}
if _, err := os.Stat(newPath); os.IsNotExist(err) {
t.Errorf("Renamed file %q does not exist", newPath)
}
}
func TestWalkerEditStdin(t *testing.T) {
// Prepare the walker with replacement pairs
w := &walker{
ToStdout: true,
Glob: "*",
IsVerbose: false,
MaxDepth: -1,
IncludeHidden: true,
ReplaceNames: false,
NamesOnly: false,
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
{
pattern: regexp.MustCompile("baz"),
replacement: []byte("qux"),
},
},
WaitGroup: new(sync.WaitGroup),
}
// Save original stdin and stdout
origStdin := os.Stdin
origStdout := os.Stdout
defer func() {
os.Stdin = origStdin
os.Stdout = origStdout
}()
// Create a pipe to simulate stdin
rStdin, wStdin, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe for stdin: %v", err)
}
os.Stdin = rStdin
// Create a pipe to capture stdout
rStdout, wStdout, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe for stdout: %v", err)
}
os.Stdout = wStdout
// Write input to stdin (include a null byte to mark the end)
input := []byte("foo baz foo\x00")
if _, err := wStdin.Write(input); err != nil {
t.Fatalf("failed to write to stdin: %v", err)
}
wStdin.Close() // End of input
// Run editStdin which reads from os.Stdin and writes to os.Stdout
w.editStdin()
// Close stdout writer to allow reading
wStdout.Close()
// Read output
var buf bytes.Buffer
if _, err := io.Copy(&buf, rStdout); err != nil {
t.Fatalf("failed to read stdout: %v", err)
}
output := buf.String()
expected := "bar qux bar\x00"
if output != expected {
t.Errorf("expected %q, got %q", expected, output)
}
}
// TestWalkerEditInvalidPath tests the edit function of the walker struct with a non-existent file.
func TestWalkerEditInvalidPath(t *testing.T) {
// Capture stdout to check error message.
origStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
wk := &walker{
ToStdout: false,
Glob: "*",
IsVerbose: false,
MaxDepth: -1,
IncludeHidden: true,
ReplaceNames: false,
NamesOnly: false,
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
WaitGroup: new(sync.WaitGroup),
}
wk.edit("non_existent_file.txt")
w.Close()
os.Stdout = origStdout
var buf bytes.Buffer
io.Copy(&buf, r)
r.Close()
output := buf.String()
if !strings.Contains(output, "no such file or directory") {
t.Errorf("expected error about missing file, got: %s", output)
}
}
// TestDepth tests the depth function.
func TestDepth(t *testing.T) {
tests := []struct {
path string
expected int
}{
{"", 1},
{"a/b/c", 3},
{"/a/b/c", 4},
}
for _, test := range tests {
result := depth(test.path)
if result != test.expected {
t.Errorf("depth(%q) = %d; want %d", test.path, result, test.expected)
}
}
}
// TestIsHidden tests the isHidden function.
func TestIsHidden(t *testing.T) {
tests := []struct {
name string
expected bool
}{
{".hidden", true},
{"..", false},
{".", false},
{"visible", false},
}
for _, test := range tests {
result := isHidden(test.name)
if result != test.expected {
t.Errorf("isHidden(%q) = %v; want %v", test.name, result, test.expected)
}
}
}
// TestProcessFile tests the processFile function of the walker struct.
func TestProcessFile(t *testing.T) {
// Create a temporary directory with files.
tmpDir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create files and directories.
files := []string{
"foo.txt",
".hidden.txt",
"subdir/bar.txt",
"subdir/.hidden_sub.txt",
}
for _, file := range files {
fullPath := filepath.Join(tmpDir, file)
if strings.Contains(file, "/") {
os.MkdirAll(filepath.Dir(fullPath), 0755)
}
if _, err := os.Create(fullPath); err != nil {
t.Fatal(err)
}
}
// Initialize the walker.
w := &walker{
ToStdout: false,
Glob: "*.txt",
IsVerbose: false,
MaxDepth: -1,
IncludeHidden: false,
ReplaceNames: false,
NamesOnly: false,
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
},
WaitGroup: new(sync.WaitGroup),
}
// Walk the directory.
w.Walk(tmpDir)
// Wait for all goroutines to finish.
w.Wait()
// Check that the non-hidden file has been edited.
content, err := os.ReadFile(filepath.Join(tmpDir, "foo.txt"))
if err != nil {
t.Fatal(err)
}
expected := []byte{}
if !bytes.Equal(content, expected) {
t.Errorf("Expected content of 'foo.txt' to be empty; got %q", content)
}
// Check that the hidden file has not been edited.
content, err = os.ReadFile(filepath.Join(tmpDir, ".hidden.txt"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, expected) {
t.Errorf("Expected content of '.hidden.txt' to be empty; got %q", content)
}
}
// TestPairsetString tests the String method of the pairset type.
func TestPairsetString(t *testing.T) {
tests := []struct {
name string
pairs pairset
expected string
}{
{
name: "Single Pair",
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
},
expected: "['foo', 'bar']",
},
{
name: "Multiple Pairs",
pairs: pairset{
{
pattern: regexp.MustCompile("foo"),
replacement: []byte("bar"),
},
{
pattern: regexp.MustCompile("baz"),
replacement: []byte("qux"),
},
},
expected: "['foo', 'bar'] ['baz', 'qux']",
},
{
name: "Empty Pairset",
pairs: pairset{},
expected: "",
},
{
name: "Special Characters",
pairs: pairset{
{
pattern: regexp.MustCompile(`a\w+b`),
replacement: []byte("replacement"),
},
},
expected: "['a\\w+b', 'replacement']",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.pairs.String()
if result != test.expected {
t.Errorf("pairset.String() = %q; want %q", result, test.expected)
}
})
}
}
func TestParseFlags(t *testing.T) {
// Save the original command-line arguments and restore after the test.
origArgs := os.Args
defer func() { os.Args = origArgs }()
// Reset the flag CommandLine so it doesn't conflict with other tests.
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Simulate command-line arguments.
// Explanation of arguments:
// -p: print to stdout
// -v: verbose
// -g *.txt: glob pattern
// -a: include hidden
// -l 3: max depth
// -r: replace names
// -n: names only
// After the flags, we must have pattern, replacement, and files.
// Here "foo" is the pattern, "bar" is the replacement, and then two files: file1.txt and file2.txt.
os.Args = []string{
"cmd",
"-p",
"-v",
"-g", "*.txt",
"-a",
"-l", "3",
"-r",
"-n",
"foo", "bar", "file1.txt", "file2.txt",
}
// Execute parseFlags
w, files := parseFlags()
// Check walker fields
if !w.ToStdout {
t.Errorf("expected ToStdout to be true, got false")
}
if !w.IsVerbose {
t.Errorf("expected IsVerbose to be true, got false")
}
if w.Glob != "*.txt" {
t.Errorf("expected Glob to be '*.txt', got %s", w.Glob)
}
if !w.IncludeHidden {
t.Errorf("expected IncludeHidden to be true, got false")
}
if w.MaxDepth != 3 {
t.Errorf("expected MaxDepth to be 3, got %d", w.MaxDepth)
}
if !w.ReplaceNames {
t.Errorf("expected ReplaceNames to be true, got false")
}
if !w.NamesOnly {
t.Errorf("expected NamesOnly to be true, got false")
}
if w.WaitGroup == nil {
t.Errorf("expected WaitGroup to be initialized, got nil")
}
// Check that w.pairs is initialized correctly.
if len(w.pairs) != 1 {
t.Fatalf("expected 1 pair, got %d", len(w.pairs))
}
p := w.pairs[0]
if p.pattern.String() != "foo" {
t.Errorf("expected pattern 'foo', got %s", p.pattern.String())
}
if string(p.replacement) != "bar" {
t.Errorf("expected replacement 'bar', got %s", p.replacement)
}
// Check the files slice
expectedFiles := []string{filepath.Clean("file1.txt"), filepath.Clean("file2.txt")}
if !reflect.DeepEqual(files, expectedFiles) {
t.Errorf("expected files %v, got %v", expectedFiles, files)
}
}
func TestUsageWithJetAsArg0(t *testing.T) {
// Save original os.Args and stdout
origArgs := os.Args
origStdout := os.Stdout
defer func() {
os.Args = origArgs
os.Stdout = origStdout
}()
// Set os.Args[0] to "jet"
os.Args = []string{"jet"}
// Create a pipe to capture usage output
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stdout = w
// Call usage to print to our pipe
usage()
// Restore original stdout
w.Close()
os.Stdout = origStdout
// Read the captured output
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatalf("failed to read output: %v", err)
}
r.Close()
output := buf.String()
// Define the expected usage output when os.Args[0] is "jet"
expected := `jet - Just Edit Text
Jet allows you to replace all the substrings matched by specified regular
expressions in one or more files.
If it is given a directory as input, it will recursively replace all matches
in the files of the directory tree.
Usage:
jet [options] pattern replacement input-files...
jet [options] -e pattern1 replacement1 -e pattern2 replacement2 input-files...
Options:
-p Print to stdout instead of modifying files.
-v Enable verbose mode; explain what is being done.
-g string Only process files matching the given glob pattern.
-a Includes hidden files (those starting with a dot).
-l int Maximum depth for directory traversal.
-r, --replace-names Replace matches in file and directory names.
-n, --names-only Only replace matching names, ignoring file contents.
-e pattern replacement Specify a regular expression pattern and replacement.
Can be used multiple times for multiple replacements.
-h, --help Prints this help message and exits.
Notice:
When using the -e flag multiple times, the pattern-replacement pairs are
executed in the same order they are specified, one by one.
Examples:
jet "foo" "bar" my/path1 my/path2
Replace all occurrences of "foo" with "bar" in the files under my/path1
and my/path2.
jet -e "foo" "bar" -e "baz" "qux" my/path1 my/path2
Replace all occurrences of "foo" with "bar" and "baz" with "qux" in the
files under my/path1 and my/path2.
jet -p -v "foo" "bar" my/path1
Replace "foo" with "bar" in my/path1 and print the results to stdout
with verbose output.
jet -e "foo" "bar" -e "baz" "qux" -g "*.txt" -a my/path1
Replace "foo" with "bar" and "baz" with "qux" in all text files,
including hidden files, under my/path1.
Jet Copyright (C) 2023 Nicolò Santamaria
This program comes with ABSOLUTELY NO WARRANTY; for details refer to
https://www.gnu.org/licenses/gpl-3.0.html.
This is free software, and you are welcome to change and redistribute it
under the conditions defined by the license.
`
if output != expected {
t.Errorf("usage output does not match expected.\nExpected:\n%s\nGot:\n%s", expected, output)
}
}
// Utility to capture stdout.
func captureStdout(f func()) string {
origStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = origStdout
var buf bytes.Buffer
io.Copy(&buf, r)
r.Close()
return buf.String()
}
// MockDirEntry for testing processFile.
type MockDirEntry struct {
name string
isDir bool
}
func (m MockDirEntry) Name() string { return m.name }
func (m MockDirEntry) IsDir() bool { return m.isDir }
func (m MockDirEntry) Type() fs.FileMode { return 0 }
func (m MockDirEntry) Info() (fs.FileInfo, error) { return nil, nil }
func TestWalkerEdit_ErrorReadingFile(t *testing.T) {
w := &walker{
pairs: pairset{{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")}},
}
// Try editing a non-existent file.
output := captureStdout(func() {
w.edit("non_existent_file.txt")
})
if !strings.Contains(output, "no such file or directory") {
t.Errorf("expected error message about missing file, got: %s", output)
}
}
func TestWalkerEdit_ToStdout(t *testing.T) {
// Create a temp file
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := []byte("foo baz")
tmpfile.Write(content)
tmpfile.Close()
w := &walker{
ToStdout: true,
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
output := captureStdout(func() {
w.edit(tmpfile.Name())
})
if output != "bar baz" {
t.Errorf("expected replaced output 'bar baz', got %q", output)
}
}
func TestWalkerEdit_Verbose(t *testing.T) {
// Create a temp file
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
tmpfile.Write([]byte("foo"))
tmpfile.Close()
w := &walker{
IsVerbose: true,
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
output := captureStdout(func() {
w.edit(tmpfile.Name())
})
if !strings.Contains(output, "writing") {
t.Errorf("expected 'writing' in verbose output, got %q", output)
}
}
func TestWalkerEdit_StatError(t *testing.T) {
// Stat error can be simulated by removing file before stat.
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
name := tmpfile.Name()
tmpfile.Write([]byte("foo"))
tmpfile.Close()
os.Remove(name) // remove the file to cause a stat error later
w := &walker{
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
output := captureStdout(func() {
w.edit(name)
})
if !strings.Contains(output, "no such file or directory") {
t.Errorf("expected stat error, got %q", output)
}
}
func TestWalkerEdit_WriteError(t *testing.T) {
// Cause write error by using a directory instead of a file
tmpdir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
w := &walker{
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
output := captureStdout(func() {
// Attempting to write to a directory path should fail
w.edit(tmpdir)
})
if !strings.Contains(output, "is a directory") {
t.Errorf("expected write error for directory, got %q", output)
}
}
// Tests for editFilename
func TestWalkerEditFilename_NoChange(t *testing.T) {
w := &walker{
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
// If filename does not contain "foo" no change is made.
oldPath := "somefile.txt"
newPath := w.editFilename(oldPath)
if newPath != oldPath {
t.Errorf("expected no change, got %q", newPath)
}
}
func TestWalkerEditFilename_ChangeAndRenameSuccess(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
oldPath := filepath.Join(tmpdir, "foo.txt")
if _, err := os.Create(oldPath); err != nil {
t.Fatal(err)
}
w := &walker{
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
IsVerbose: true,
}
output := captureStdout(func() {
newPath := w.editFilename(oldPath)
expected := filepath.Join(tmpdir, "bar.txt")
if newPath != expected {
t.Errorf("expected newPath %q, got %q", expected, newPath)
}
})
if !strings.Contains(output, "renaming") {
t.Errorf("expected 'renaming' in verbose output")
}
}
func TestWalkerEditFilename_RenameError(t *testing.T) {
// Attempt rename to a non-writable directory to force error
tmpdir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
oldPath := filepath.Join(tmpdir, "foo.txt")
if _, err := os.Create(oldPath); err != nil {
t.Fatal(err)
}
// Make the directory read-only
os.Chmod(tmpdir, 0500)
defer os.Chmod(tmpdir, 0700) // restore permissions
w := &walker{
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
output := captureStdout(func() {
newPath := w.editFilename(oldPath)
if newPath != oldPath {
t.Errorf("expected to return oldPath on error, got %q", newPath)
}
})
if !strings.Contains(output, "permission denied") {
t.Errorf("expected rename error message, got %q", output)
}
}
// Tests for processFile
func TestWalkerProcessFile_WithErrorParameter(t *testing.T) {
w := &walker{
Glob: "*",
MaxDepth: -1,
IncludeHidden: true,
WaitGroup: new(sync.WaitGroup),
}
output := captureStdout(func() {
w.processFile("anyfile.txt", MockDirEntry{name: "anyfile.txt", isDir: false},
fs.ErrNotExist) // Simulated error
})
if !strings.Contains(output, "file does not exist") && !strings.Contains(output, "no such file") {
t.Errorf("expected error message, got %q", output)
}
}
func TestWalkerProcessFile_ExceedMaxDepth(t *testing.T) {
w := &walker{
MaxDepth: 1,
WaitGroup: new(sync.WaitGroup),
}
// depth("a/b/c") = 3, which is greater than MaxDepth=1
err := w.processFile("a/b/c", MockDirEntry{name: "c", isDir: true}, nil)
if err != fs.SkipDir {
t.Errorf("expected fs.SkipDir for exceeding max depth, got %v", err)
}
}
func TestWalkerProcessFile_HiddenFilesNotIncluded(t *testing.T) {
w := &walker{
IncludeHidden: false,
MaxDepth: -1,
WaitGroup: new(sync.WaitGroup),
}
// Hidden file
err := w.processFile(".hiddenfile", MockDirEntry{name: ".hiddenfile", isDir: false}, nil)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
// Since it's hidden and not included, no action is taken.
// To confirm no goroutine started, we can rely on coverage or manual checks.
}
func TestWalkerProcessFile_HiddenDirsNotIncluded(t *testing.T) {
w := &walker{
IncludeHidden: false,
WaitGroup: new(sync.WaitGroup),
}
err := w.processFile(".hiddendir", MockDirEntry{name: ".hiddendir", isDir: true}, nil)
if err != fs.SkipDir {
t.Errorf("expected fs.SkipDir for hidden directory, got %v", err)
}
}
func TestWalkerProcessFile_MatchesGlobAndNamesOnly(t *testing.T) {
w := &walker{
Glob: "*.txt",
NamesOnly: true,
WaitGroup: new(sync.WaitGroup),
pairs: pairset{
{pattern: regexp.MustCompile("foo"), replacement: []byte("bar")},
},
}
tmpdir, err := os.MkdirTemp("", "testdir")