-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_test.go
1030 lines (1023 loc) · 23.8 KB
/
file_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 file_test
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
itn "github.com/1set/starlet/internal"
lf "github.com/1set/starlet/lib/file"
"go.starlark.net/starlark"
)
func TestLoadModule_File(t *testing.T) {
isOnWindows := runtime.GOOS == "windows"
tests := []struct {
name string
script string
wantErr string
fileContent string
skipWindows bool
}{
{
name: `trim bom`,
script: itn.HereDoc(`
load('file', 'trim_bom')
b0 = trim_bom('A')
assert.eq(b0, 'A')
b1 = trim_bom('hello')
assert.eq(b1, 'hello')
b2 = trim_bom(b'\xef\xbb\xbfhello')
assert.eq(b2, b'hello')
`),
},
{
name: `trim bom with no args`,
script: itn.HereDoc(`
load('file', 'trim_bom')
trim_bom()
`),
wantErr: "file.trim_bom: takes exactly one argument (0 given)",
},
{
name: `trim bom with invalid args`,
script: itn.HereDoc(`
load('file', 'trim_bom')
trim_bom(123)
`),
wantErr: "file.trim_bom: expected string or bytes, got int",
},
{
name: `read empty file`,
script: itn.HereDoc(`
load('file', 'read_bytes', 'read_string', 'read_lines', 'count_lines', 'head_lines', 'tail_lines')
fp = 'testdata/empty.txt'
b = read_bytes(fp)
assert.eq(b, b'')
s = read_string(fp)
assert.eq(s, '')
l = read_lines(fp)
assert.eq(l, [])
c = count_lines(fp)
assert.eq(c, 0)
h = head_lines(fp, 10)
assert.eq(h, [])
t = tail_lines(fp, 10)
assert.eq(t, [])
`),
},
{
name: `read bytes`,
script: itn.HereDoc(`
load('file', 'read_bytes')
b = read_bytes('testdata/aloha.txt')
assert.eq(b, b'ALOHA\n')
`),
},
{
name: `read bom bytes`,
script: itn.HereDoc(`
load('file', 'read_bytes', 'trim_bom')
b = read_bytes('testdata/bom.txt')
assert.eq(b, b'\xef\xbb\xbfhas bom')
t = trim_bom(b)
assert.eq(t, b'has bom')
`),
},
{
name: `read no args`,
script: itn.HereDoc(`
load('file', 'read_string')
read_string()
`),
wantErr: "file.read_string: missing argument for name",
},
{
name: `read invalid args`,
script: itn.HereDoc(`
load('file', 'read_string')
read_string(123)
`),
wantErr: "file.read_string: for parameter name: got int, want string or bytes",
},
{
name: `read string`,
script: itn.HereDoc(`
load('file', 'read_string')
s = read_string('testdata/aloha.txt')
assert.eq(s, 'ALOHA\n')
`),
},
{
name: `read lines`,
script: itn.HereDoc(`
load('file', 'read_lines')
l1 = read_lines('testdata/line_mac.txt')
assert.eq(len(l1), 3)
assert.eq(l1, ['Line 1', 'Line 2', 'Line 3'])
l2 = read_lines('testdata/line_win.txt')
assert.eq(len(l2), 3)
assert.eq(l2, ['Line 1', 'Line 2', 'Line 3'])
`),
},
{
name: `read one line`,
script: itn.HereDoc(`
load('file', 'read_lines')
text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ甲乙丙丁戊己庚辛壬癸子丑寅卯辰巳午未申酉戌亥'
l1 = read_lines('testdata/1line.txt')
assert.eq(len(l1), 1)
assert.eq(l1, [text])
l2 = read_lines('testdata/1line_nl.txt')
assert.eq(len(l2), 1)
assert.eq(l2, [text])
`),
},
{
name: `read json no args`,
script: itn.HereDoc(`
load('file', 'read_json')
j1 = read_json()
`),
wantErr: `file.read_json: missing argument for name`,
},
{
name: `read json not found`,
script: itn.HereDoc(`
load('file', 'read_json')
j1 = read_json("no-such-file")
`),
wantErr: `open no-such-file`,
},
{
name: `read broken json`,
script: itn.HereDoc(`
load('file', 'read_json')
j1 = read_json('testdata/1line.txt')
`),
wantErr: `json.decode: at offset`,
},
{
name: `read json`,
script: itn.HereDoc(`
load('file', 'read_json')
j1 = read_json('testdata/json1.json')
assert.true(type(j1) == "dict")
assert.eq(j1["num"], 42)
assert.eq(j1["undef"], None)
assert.eq(j1["bool"], True)
assert.eq(j1["arr"], [1,2,3])
assert.eq(j1["obj"], {"foo": "bar", "baz": "qux"})
`),
},
{
name: `read jsonl no args`,
script: itn.HereDoc(`
load('file', 'read_jsonl')
j1 = read_jsonl()
`),
wantErr: `file.read_jsonl: missing argument for name`,
},
{
name: `read jsonl not found`,
script: itn.HereDoc(`
load('file', 'read_jsonl')
j1 = read_jsonl("no-such-file")
`),
wantErr: `open no-such-file`,
},
{
name: `read broken jsonl`,
script: itn.HereDoc(`
load('file', 'read_jsonl')
j1 = read_jsonl('testdata/1line.txt')
`),
wantErr: `line 1: json.decode: at offset`,
},
{
name: `read jsonl`,
script: itn.HereDoc(`
load('file', 'read_jsonl')
js = read_jsonl('testdata/json2.json')
assert.eq(len(js), 3)
assert.eq(js[-1], {"name": "Mike", "age": 32, "city": "Chicago", "opt": True})
`),
},
{
name: `write no args`,
script: itn.HereDoc(`
load('file', 'write_bytes')
write_bytes()
`),
wantErr: `file.write_bytes: missing argument for name`,
},
{
name: `write no data`,
script: itn.HereDoc(`
load('file', 'write_bytes')
write_bytes("try-one-test.txt")
`),
wantErr: `file.write_bytes: missing argument for data`,
},
{
name: `write_bytes invalid data`,
script: itn.HereDoc(`
load('file', 'write_bytes')
fp = %q
write_bytes(fp, 123)
`),
wantErr: "file.write_bytes: expected string or bytes, got int",
},
{
name: `write_string invalid data`,
script: itn.HereDoc(`
load('file', 'write_string')
fp = %q
write_string(fp, 123)
`),
wantErr: "file.write_string: expected string or bytes, got int",
},
{
name: `write_lines invalid data`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, 123)
`),
wantErr: "file.write_lines: expected list/tuple/set, got int",
},
{
name: `write bytes`,
script: itn.HereDoc(`
load('file', 'write_bytes')
fp = %q
write_bytes(fp, b'hello')
write_bytes(fp, b'world')
write_bytes(fp, 'Word')
`),
fileContent: "Word",
},
{
name: `write string`,
script: itn.HereDoc(`
load('file', 'write_string')
fp = %q
write_string(fp, "Hello World")
write_string(fp, "Aloha!\nA hui hou!\n")
`),
fileContent: "Aloha!\nA hui hou!\n",
},
{
name: `write lines: string`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, "Hello World")
write_lines(fp, "Goodbye~")
`),
fileContent: "Goodbye~\n",
},
{
name: `write lines: bytes`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, b"Hello World")
write_lines(fp, b"Goodbye!")
`),
fileContent: "Goodbye!\n",
},
{
name: `write lines: list`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, ["Hello", "World"])
write_lines(fp, ["Great", "Job"])
`),
fileContent: "Great\nJob\n",
},
{
name: `write lines: tuple`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, ("Hello", "World"))
write_lines(fp, ("Nice", "Job"))
`),
fileContent: "Nice\nJob\n",
},
{
name: `write lines: set`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, set(["Hello", "World"]))
write_lines(fp, set(["Good", "Job"]))
`),
fileContent: "Good\nJob\n",
},
{
name: `write lines: various type`,
script: itn.HereDoc(`
load('file', 'write_lines')
fp = %q
write_lines(fp, ["Hello", b'world', 123, [True, False]])
`),
fileContent: "Hello\nworld\n123\n[True, False]\n",
},
{
name: `write json no args`,
script: itn.HereDoc(`
load('file', 'write_json')
write_json()
`),
wantErr: `file.write_json: missing argument for name`,
},
{
name: `write json no data`,
script: itn.HereDoc(`
load('file', 'write_json')
fp = %q
write_json(fp)
`),
wantErr: `file.write_json: missing argument for data`,
},
{
name: `write json invalid data`,
script: itn.HereDoc(`
load('file', 'write_json')
fp = %q
write_json(fp, lambda x: x*2)
`),
wantErr: `json.encode: cannot encode function as JSON`,
},
{
name: `write json string`,
script: itn.HereDoc(`
load('file', 'write_json')
fp = %q
write_json(fp, "abc")
`),
fileContent: `abc`,
},
{
name: `write json bytes`,
script: itn.HereDoc(`
load('file', 'write_json')
fp = %q
write_json(fp, b"123")
`),
fileContent: `123`,
},
{
name: `write json dict`,
script: itn.HereDoc(`
load('file', 'write_json')
fp = %q
write_json(fp, {"b": True})
write_json(fp, {"a": 520})
`),
fileContent: `{"a":520}`,
},
{
name: `append json no args`,
script: itn.HereDoc(`
load('file', 'append_json')
append_json()
`),
wantErr: `file.append_json: missing argument for name`,
},
{
name: `append json no data`,
script: itn.HereDoc(`
load('file', 'append_json')
fp = %q
append_json(fp)
`),
wantErr: `file.append_json: missing argument for data`,
},
{
name: `append json invalid data`,
script: itn.HereDoc(`
load('file', 'append_json')
fp = %q
append_json(fp, lambda x: x*2)
`),
wantErr: `json.encode: cannot encode function as JSON`,
},
{
name: `append json string`,
script: itn.HereDoc(`
load('file', 'append_json')
fp = %q
append_json(fp, "abc")
append_json(fp, "ABC")
`),
fileContent: `abcABC`,
},
{
name: `append json bytes`,
script: itn.HereDoc(`
load('file', 'append_json')
fp = %q
append_json(fp, b"123")
append_json(fp, b"456")
`),
fileContent: `123456`,
},
{
name: `append json dict`,
script: itn.HereDoc(`
load('file', 'append_json')
fp = %q
append_json(fp, {"a": 520})
append_json(fp, {"b": 1==1})
`),
fileContent: `{"a":520}{"b":true}`,
},
{
name: `append bytes`,
script: itn.HereDoc(`
load('file', 'append_bytes')
fp = %q
append_bytes(fp, b'hello')
append_bytes(fp, b'world')
`),
fileContent: "helloworld",
},
{
name: `append string`,
script: itn.HereDoc(`
load('file', 'append_string', 'write_string')
fp = %q
write_string(fp, "Hello World\n")
append_string(fp, "Aloha!\nA hui hou!\n")
`),
fileContent: "Hello World\nAloha!\nA hui hou!\n",
},
{
name: `append lines`,
script: itn.HereDoc(`
load('file', 'write_lines', 'append_lines')
fp = %q
write_lines(fp, ["Hello", "World"])
append_lines(fp, ["Great", "Job"])
append_lines(fp, ["Bye"])
`),
fileContent: "Hello\nWorld\nGreat\nJob\nBye\n",
},
{
name: `write jsonl no args`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
write_jsonl()
`),
wantErr: `file.write_jsonl: missing argument for name`,
},
{
name: `write jsonl no data`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp)
`),
wantErr: `file.write_jsonl: missing argument for data`,
},
{
name: `write jsonl invalid data`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp, lambda x: x*2)
`),
wantErr: `json.encode: cannot encode function as JSON`,
},
{
name: `write jsonl string`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp, "abc")
`),
fileContent: "abc\n",
},
{
name: `write jsonl bytes`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp, b"123")
`),
fileContent: "123\n",
},
{
name: `write jsonl dict`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp, {"b": True})
write_jsonl(fp, {"a": 520})
`),
fileContent: "{\"a\":520}\n",
},
{
name: `write jsonl list`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
l = [{"a": 520}, {"b": True}]
write_jsonl(fp, l)
`),
fileContent: "{\"a\":520}\n{\"b\":true}\n",
},
{
name: `write jsonl tuple`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
l = ({"a": 520}, {"b": True}, {"c": "hello"})
write_jsonl(fp, l)
`),
fileContent: "{\"a\":520}\n{\"b\":true}\n{\"c\":\"hello\"}\n",
},
{
name: `write jsonl set`,
script: itn.HereDoc(`
load('file', 'write_jsonl')
fp = %q
write_jsonl(fp, set({"a": 520}))
`),
fileContent: "\"a\"\n",
},
{
name: `append jsonl no args`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
append_jsonl()
`),
wantErr: `file.append_jsonl: missing argument for name`,
},
{
name: `append jsonl no data`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp)
`),
wantErr: `file.append_jsonl: missing argument for data`,
},
{
name: `append jsonl invalid data`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp, [lambda x: x*2])
`),
wantErr: `json.encode: cannot encode function as JSON`,
},
{
name: `append jsonl string`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp, "abc")
`),
fileContent: "abc\n",
},
{
name: `append jsonl bytes`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp, b"123")
`),
fileContent: "123\n",
},
{
name: `append jsonl dict`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp, {"b": True})
append_jsonl(fp, {"a": 520})
`),
fileContent: "{\"b\":true}\n{\"a\":520}\n",
},
{
name: `append jsonl list`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
l = [{"a": 520}, {"b": True}]
append_jsonl(fp, l)
`),
fileContent: "{\"a\":520}\n{\"b\":true}\n",
},
{
name: `append jsonl tuple`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
l = ({"a": 520}, {"b": True}, {"c": "hello"})
append_jsonl(fp, l)
`),
fileContent: "{\"a\":520}\n{\"b\":true}\n{\"c\":\"hello\"}\n",
},
{
name: `append jsonl set`,
script: itn.HereDoc(`
load('file', 'append_jsonl')
fp = %q
append_jsonl(fp, set({"a": 520}))
append_jsonl(fp, set({"c": 800}))
`),
fileContent: "\"a\"\n\"c\"\n",
},
{
name: `count lines: no args`,
script: itn.HereDoc(`
load('file', 'count_lines')
count_lines()
`),
wantErr: `file.count_lines: missing argument for name`,
},
{
name: `count lines`,
script: itn.HereDoc(`
load('file', 'count_lines')
assert.eq(3, count_lines('testdata/line_mac.txt'))
assert.eq(3, count_lines('testdata/line_win.txt'))
`),
},
{
name: `read head lines`,
script: itn.HereDoc(`
load('file', 'head_lines')
l1 = head_lines('testdata/line_mac.txt', 10)
assert.eq(len(l1), 3)
assert.eq(l1, ['Line 1', 'Line 2', 'Line 3'])
l2 = head_lines('testdata/line_win.txt', 2)
assert.eq(len(l2), 2)
assert.eq(l2, ['Line 1', 'Line 2'])
`),
},
{
name: `read tail lines`,
script: itn.HereDoc(`
load('file', 'tail_lines')
l1 = tail_lines('testdata/line_mac.txt', 10)
assert.eq(len(l1), 3)
assert.eq(l1, ['Line 1', 'Line 2', 'Line 3'])
l2 = tail_lines('testdata/line_win.txt', 2)
assert.eq(len(l2), 2)
assert.eq(l2, ['Line 2', 'Line 3'])
`),
},
{
name: `read head lines: invalid args`,
script: itn.HereDoc(`
load('file', 'head_lines')
l1 = head_lines(123, 10)
`),
wantErr: `file.head_lines: for parameter name: got int, want string or bytes`,
},
{
name: `read tail lines: invalid n`,
script: itn.HereDoc(`
load('file', 'tail_lines')
l1 = tail_lines('testdata/line_mac.txt', -7)
`),
wantErr: `file.tail_lines: expected positive integer, got -7`,
},
{
name: `write_bytes: conflict`,
script: itn.HereDoc(`
load('file', 'write_bytes')
write_bytes('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `write_string: conflict`,
script: itn.HereDoc(`
load('file', 'write_string')
write_string('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `write_lines: conflict`,
script: itn.HereDoc(`
load('file', 'write_lines')
write_lines('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `append_bytes: conflict`,
script: itn.HereDoc(`
load('file', 'append_bytes')
append_bytes('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `append_string: conflict`,
script: itn.HereDoc(`
load('file', 'append_string')
append_string('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `append_lines: conflict`,
script: itn.HereDoc(`
load('file', 'append_lines')
append_lines('testdata/', b'hello')
`),
wantErr: `open testdata/:`,
},
{
name: `read_bytes: not exist`,
script: itn.HereDoc(`
load('file', 'read_bytes')
s = read_bytes('not-such-file1.txt')
`),
wantErr: `open not-such-file1.txt:`,
},
{
name: `read_string: not exist`,
script: itn.HereDoc(`
load('file', 'read_string')
s = read_string('not-such-file2.txt')
`),
wantErr: `open not-such-file2.txt:`,
},
{
name: `read_lines: not exist`,
script: itn.HereDoc(`
load('file', 'read_lines')
s = read_lines('not-such-file3.txt')
`),
wantErr: `open not-such-file3.txt:`,
},
{
name: `count not exist`,
script: itn.HereDoc(`
load('file', 'count_lines')
s = count_lines('not-such-file4.txt')
`),
wantErr: `open not-such-file4.txt:`,
},
{
name: `head not exist`,
script: itn.HereDoc(`
load('file', 'head_lines')
s = head_lines('not-such-file5.txt', 10)
`),
wantErr: `open not-such-file5.txt:`,
},
{
name: `tail not exist`,
script: itn.HereDoc(`
load('file', 'tail_lines')
s = tail_lines('not-such-file6.txt', 10)
`),
wantErr: `open not-such-file6.txt:`,
},
{
name: `stat: no arg`,
script: itn.HereDoc(`
load('file', 'stat')
s = stat()
`),
wantErr: `file.stat: missing argument for name`,
},
{
name: `stat: not exist`,
script: itn.HereDoc(`
load('file', 'stat')
s = stat('not-such-file7.txt')
`),
wantErr: `file.stat: lstat not-such-file7.txt`,
skipWindows: true,
},
{
name: `stat: empty path`,
script: itn.HereDoc(`
load('file', 'stat')
s = stat('')
`),
wantErr: `file.stat: lstat :`,
skipWindows: true,
},
{
name: `stat: file`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata/aloha.txt'
s = stat(fp)
assert.eq(s.name, 'aloha.txt')
assert.eq(s.size, 6)
assert.eq(s.type, 'file')
assert.eq(s.ext, '.txt')
assert.true("testdata" in s.path)
assert.true("aloha.txt" in s.path)
assert.true(s.modified.unix > 0)
assert.eq(s.get_md5(), '6a12867bd5e0810f2dae51da4a51f001')
assert.eq(s.get_sha1(), '71a45eadccd2f29bbf60f46b13e019ae62c8b0bd')
assert.eq(s.get_sha256(), 'eb69c86a84164b23808dcda13fdfbe664760701cf605df28272d4efd2ed18ab4')
assert.eq(s.get_sha512(), '9946cf3ba83eff33d9798fe933785b8a0f20aa179ca1d18418fd401d955a1270edf9542eb7b10833bbfe1de84b31dde6924e4e01f0e335174c45fede9f9e80ef')
`),
},
{
name: `stat: follow file`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata/aloha.txt'
s = stat(fp, True)
assert.eq(s.name, 'aloha.txt')
assert.eq(s.size, 6)
assert.eq(s.type, 'file')
assert.eq(s.ext, '.txt')
assert.true("testdata" in s.path)
assert.true("aloha.txt" in s.path)
assert.true(s.modified.unix > 0)
assert.eq(s.get_md5(), '6a12867bd5e0810f2dae51da4a51f001')
assert.eq(s.get_sha1(), '71a45eadccd2f29bbf60f46b13e019ae62c8b0bd')
assert.eq(s.get_sha256(), 'eb69c86a84164b23808dcda13fdfbe664760701cf605df28272d4efd2ed18ab4')
assert.eq(s.get_sha512(), '9946cf3ba83eff33d9798fe933785b8a0f20aa179ca1d18418fd401d955a1270edf9542eb7b10833bbfe1de84b31dde6924e4e01f0e335174c45fede9f9e80ef')
`),
},
{
name: `stat: empty file`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata/empty.txt'
s = stat(fp)
assert.eq(s.name, 'empty.txt')
assert.eq(s.size, 0)
assert.eq(s.type, 'file')
assert.eq(s.ext, '.txt')
`),
},
{
name: `stat: file no ext`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata/noext'
s = stat(fp)
assert.eq(s.type, 'file')
assert.eq(s.name, 'noext')
assert.eq(s.ext, '')
`),
},
//{
// name: `stat: file dot ext`,
// script: itn.HereDoc(`
// load('file', 'stat')
// fp = 'testdata/dotext.'
// s = stat(fp)
// assert.eq(s.type, 'file')
// assert.eq(s.name, 'dotext.')
// assert.eq(s.ext, '.')
// `),
//},
{
name: `stat: dir`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata'
s = stat(fp)
assert.eq(s.name, 'testdata')
assert.eq(s.type, 'dir')
assert.eq(s.ext, '')
assert.true(s.path.endswith(fp))
assert.true(s.modified.unix > 0)
`),
},
{
name: `stat: follow dir`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata'
s = stat(fp, follow=True)
assert.eq(s.name, 'testdata')
assert.eq(s.type, 'dir')
assert.eq(s.ext, '')
assert.true(s.path.endswith(fp))
assert.true(s.modified.unix > 0)
`),
},
{
name: `stat: symlink`,
script: itn.HereDoc(`
load('file', 'stat')
fp = '/dev/stdin'
s = stat(fp)
assert.eq(s.name, 'stdin')
assert.eq(s.type, 'symlink')
assert.eq(s.ext, '')
`),
skipWindows: true,
},
{
name: `stat: char device`,
script: itn.HereDoc(`
load('file', 'stat')
fp = '/dev/null'
s = stat(fp)
assert.eq(s.name, 'null')
assert.eq(s.path, fp)
assert.eq(s.ext, '')
assert.eq(s.type, 'char')
`),
skipWindows: true,
},
//{
// name: `stat: block device`,
// script: itn.HereDoc(`
// load('file', 'stat')
// fp = '/dev/disk0'
// s = stat(fp)
// assert.eq(s.name, 'disk0')
// assert.eq(s.path, fp)
// assert.eq(s.ext, '')
// assert.eq(s.type, 'block')
// `),
// skipWindows: true,
//},
{
name: `stat: dir get hash`,
script: itn.HereDoc(`
load('file', 'stat')
fp = 'testdata'
s = stat(fp)
s.get_md5()
`),
wantErr: `testdata: is a directory`,
skipWindows: true,
},
{
name: `stat: no perm`,
script: itn.HereDoc(`
load('file', 'stat')
fp = '/etc/sudoers'
s = stat(fp)
s.get_md5()
`),
wantErr: `get_md5: open /etc/sudoers`,
skipWindows: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// skip windows
if isOnWindows && tt.skipWindows {
t.Skipf("Skip test on Windows")
return
}
// prepare temp file if needed
var (
tp string
td string
predecl starlark.StringDict
)
if strings.Contains(tt.script, "%q") {
tf, err := os.CreateTemp("", "starlet-file-test-write")
if err != nil {
t.Errorf("os.CreateTemp() expects no error, actual error = '%v'", err)
return
}
tp = tf.Name()
//t.Logf("Temp file to write: %s", tp)
tt.script = fmt.Sprintf(tt.script, tp)
td, err = os.MkdirTemp("", "starlet-file-test-dir")