-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfs.test.lua
More file actions
1215 lines (992 loc) · 42.6 KB
/
Copy pathfs.test.lua
File metadata and controls
1215 lines (992 loc) · 42.6 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
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
local helpers = require "tests.helpers"
local lfs = require "lfs"
local mods = require "mods"
local List = mods.list
local fs = mods.fs
local path = mods.path
local isdir = mods.is.dir
local make_tmp_dir = helpers.make_tmp_dir
local tmpname = helpers.tmpname
local join = path.join
local dirname = path.dirname
local fmt = string.format
describe("mods.fs", function()
local is_unix = mods.runtime.is_unix
local cwd = path.cwd() --[[@as string]]
local readme_file = join(cwd, "README.md")
local spec_file = join(cwd, "tests", "_types.lua")
after_each(function()
fs.cd(cwd)
end)
for _, fname in ipairs({ "getsize", "getatime", "getmtime", "getctime" }) do
it(fmt("%s() returns a number for an existing path", fname), function()
assert.is_number(fs[fname](readme_file))
end)
it(fmt("%s() returns nil and an error for a missing path", fname), function()
local value, errmsg, errcode = fs[fname]("__mods_missing_path__")
assert.are_same({ "nil", "string", "number" }, { type(value), type(errmsg), type(errcode) })
end)
end
describe("stat()", function()
it("delegates to lfs.attributes", function()
assert.are_equal(lfs.attributes, fs.stat)
end)
it("returns nil and an error for a missing path", function()
local attrs, errmsg, errcode = fs.stat("__mods_missing_path__")
assert.are_same({ "nil", "string", "number" }, { type(attrs), type(errmsg), type(errcode) })
end)
end)
describe("lstat()", function()
it("delegates to lfs.symlinkattributes", function()
assert.are_equal(lfs.symlinkattributes, fs.lstat)
end)
it("returns nil and an error for a missing path", function()
local attrs, errmsg, errcode = fs.lstat("__mods_missing_path__")
assert.are_same({ "nil", "string", "number" }, { type(attrs), type(errmsg), type(errcode) })
end)
end)
describe("rename()", function()
it("delegates to os.rename", function()
assert.are_equal(os.rename, fs.rename)
end)
it("renames a file", function()
local from = tmpname()
local to = tmpname()
assert.is_true(fs.write_text(from, "abc"))
assert.is_true(fs.rename(from, to))
assert.is_false(fs.exists(from))
assert.are_equal("abc", fs.read_text(to))
assert.is_true(fs.rm(to))
end)
it("fails with an error for a missing path", function()
local ok, errmsg, errcode = fs.rename("__mods_missing_path__", tmpname())
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
end)
end)
describe("cd()", function()
it("delegates to lfs.chdir", function()
assert.are_equal(lfs.chdir, fs.cd)
end)
it("changes the current working directory", function()
local root = make_tmp_dir()
local canonical_root = root
local tmp = lfs.currentdir()
if tmp and lfs.chdir(root) then
canonical_root = lfs.currentdir()
lfs.chdir(tmp)
end
assert.is_true(fs.cd(root))
assert.are_equal(canonical_root, path.cwd())
assert.is_true(fs.cd(cwd))
assert.is_true(fs.rm(root, true))
end)
it("fails with an error for a missing path", function()
local root = make_tmp_dir()
local missing = join(root, "missing")
local ok, errmsg = fs.cd(missing)
assert.is_nil(ok)
assert.is_string(errmsg)
assert.are_equal(cwd, path.cwd())
assert.is_true(fs.rm(root, true))
end)
end)
describe("cwd()", function()
it("delegates to lfs.currentdir", function()
assert.are_equal(lfs.currentdir, fs.cwd)
end)
it("returns the current working directory", function()
assert.are_equal(cwd, fs.cwd())
end)
if is_unix then
it("fails when the current directory no longer exists", function()
local root = make_tmp_dir()
assert.is_true(fs.cd(root))
assert.is_true(fs.rm(root, true))
local dir, errmsg, errcode = fs.cwd()
assert.is_true(fs.cd(cwd))
assert.are_same({ "nil", "string", "number" }, { type(dir), type(errmsg), type(errcode) })
end)
end
end)
describe("write_bytes()", function()
it("writes file contents", function()
local target = tmpname()
assert.is_true(fs.write_bytes(target, "abc"))
assert.are_equal("abc", fs.read_bytes(target))
assert.is_true(fs.rm(target))
end)
it("fails with an error when the parent directory does not exist", function()
local target = join(tmpname(), "missing", "file.bin")
local ok, errmsg, errcode = fs.write_bytes(target, "abc")
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
end)
end)
describe("write_text()", function()
it("writes file contents", function()
local target = tmpname()
assert.is_true(fs.write_text(target, "abc"))
assert.are_equal("abc", fs.read_text(target))
assert.is_true(fs.rm(target))
end)
it("fails with an error when the parent directory does not exist", function()
local target = join(tmpname(), "missing", "file.txt")
local ok, errmsg, errcode = fs.write_text(target, "abc")
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
end)
end)
describe("read_bytes()", function()
it("reads file contents", function()
local target = tmpname()
assert.is_true(fs.write_bytes(target, "abc"))
assert.are_equal("abc", fs.read_bytes(target))
assert.is_true(fs.rm(target))
end)
it("fails with an error for a missing path", function()
local body, errmsg, errcode = fs.read_bytes("__mods_missing_path__")
assert.are_same({ "nil", "string", "number" }, { type(body), type(errmsg), type(errcode) })
end)
end)
describe("read_text()", function()
it("reads file contents", function()
local target = tmpname()
assert.is_true(fs.write_text(target, "abc"))
assert.are_equal("abc", fs.read_text(target))
assert.is_true(fs.rm(target))
end)
it("fails with an error for a missing path", function()
local body, errmsg, errcode = fs.read_text("__mods_missing_path__")
assert.are_same({ "nil", "string", "number" }, { type(body), type(errmsg), type(errcode) })
end)
end)
describe("touch()", function()
it("creates a missing file", function()
local target = tmpname()
assert.is_true(fs.touch(target))
assert.are_equal("", fs.read_bytes(target))
assert.is_true(fs.rm(target))
end)
it("does not truncate an existing file", function()
local target = tmpname()
assert.is_true(fs.write_bytes(target, "abc"))
assert.is_true(fs.touch(target))
assert.are_equal("abc", fs.read_bytes(target))
assert.is_true(fs.rm(target))
end)
it("updates timestamps for an existing file", function()
local target = tmpname()
assert.is_true(fs.write_bytes(target, "abc"))
assert.is_true(lfs.touch(target, 1, 1))
assert.is_true(fs.touch(target))
assert.is_true(fs.getmtime(target) > 1)
assert.is_true(fs.rm(target))
end)
it("fails when the parent directory does not exist", function()
local root = tmpname()
local target = join(root, "missing", "file.txt")
local ok, errmsg, errcode = fs.touch(target)
assert.is_nil(ok)
assert.is_string(errmsg)
assert.is_number(errcode)
end)
if is_unix then
it("updates an existing file through a symlink", function()
local target, link = tmpname(), tmpname()
assert.is_true(fs.write_bytes(target, "abc"))
assert.is_true(fs.symlink(target, link))
assert.is_true(lfs.touch(target, 1, 1))
assert.is_true(fs.touch(link))
assert.is_true(fs.getmtime(target) > 1)
assert.are_equal("abc", fs.read_bytes(target))
assert.is_true(fs.rm(link))
assert.is_true(fs.rm(target))
end)
it("heals a broken symlink by creating its target", function()
local target, link = tmpname(), tmpname()
assert.is_true(fs.symlink(target, link))
assert.is_true(fs.touch(link))
assert.is_true(fs.exists(link))
assert.is_true(fs.exists(target))
assert.is_true(fs.rm(link))
assert.is_true(fs.rm(target))
end)
end
end)
if is_unix then
describe("link()", function()
it("creates a hard link", function()
local root = make_tmp_dir()
local target = join(root, "target.txt")
local link = join(root, "hardlink.txt")
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.link(target, link))
assert.are_equal("abc", fs.read_text(link))
assert.is_true(fs.samefile(target, link))
assert.is_true(fs.rm(root, true))
end)
it("fails with an error for a missing target", function()
local root = make_tmp_dir()
local target = join(root, "missing.txt")
local link = join(root, "link.txt")
local ok, errmsg, errcode = fs.link(target, link)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
end)
describe("symlink()", function()
it("creates a symbolic link", function()
local root = make_tmp_dir()
local target = join(root, "target.txt")
local link = join(root, "symlink.txt")
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.symlink(target, link))
assert.are_equal("abc", fs.read_text(link))
assert.is_true(fs.lexists(link))
assert.is_true(fs.samefile(target, link))
assert.is_true(fs.rm(root, true))
end)
it("creates a broken symbolic link when the target is missing", function()
local root = make_tmp_dir()
local target = join(root, "missing.txt")
local link = join(root, "link.txt")
assert.is_true(fs.symlink(target, link))
assert.is_true(fs.lexists(link))
assert.is_false(fs.exists(link))
assert.is_true(fs.rm(root, true))
end)
end)
end
describe("listdir()", function()
it("returns an empty list for an empty directory", function()
local root = make_tmp_dir()
assert.is_true(fs.listdir(root):isempty())
assert.is_true(fs.rm(root, true))
end)
it("lists direct children by default", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
assert.same({ hidden_dir, hidden_target, target, subdir }, fs.listdir(root):sort())
assert.is_true(fs.rm(root, true))
end)
it("can return basenames instead of full paths", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.same({ ".hidden", ".secret", "data.txt", "sub" }, fs.listdir(root, { names = true }):sort())
assert.is_true(fs.rm(root, true))
end)
it("supports recursive listing", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
assert.same(
{ hidden_dir, hidden_nested, hidden_target, target, subdir, nested },
fs.listdir(root, { recursive = true }):sort()
)
assert.is_true(fs.rm(root, true))
end)
it("supports hidden filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
assert.same({ target, subdir }, fs.listdir(root, { hidden = false }):sort())
assert.same({ target, subdir, nested }, fs.listdir(root, { hidden = false, recursive = true }):sort())
assert.is_true(fs.rm(root, true))
end)
it("supports file type filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
assert.same({ hidden_target, target }, fs.listdir(root, { type = "file" }):sort())
assert.is_true(fs.rm(root, true))
end)
it("supports directory type filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local target = join(root, "data.txt")
local hidden_target = join(root, ".secret")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(hidden_target, "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
assert.same({ hidden_dir, subdir }, fs.listdir(root, { type = "directory" }):sort())
assert.is_true(fs.rm(root, true))
end)
it("fails for a non-directory path", function()
local root = make_tmp_dir()
local target = join(root, "data.txt")
assert.is_true(fs.write_text(target, "abc"))
local items, errmsg = fs.listdir(target)
assert.is_nil(items)
assert.is_string(errmsg)
assert.is_true(fs.rm(root, true))
end)
it("fails for a missing path", function()
local root = make_tmp_dir()
local missing = join(root, "missing")
local items, errmsg = fs.listdir(missing)
assert.is_nil(items)
assert.is_string(errmsg)
assert.is_true(fs.rm(root, true))
end)
if is_unix then
it("follows symlinked directories when requested", function()
local root = make_tmp_dir()
local target_dir = join(root, "target")
local nested = join(target_dir, "nested.txt")
local link_dir = join(root, "linked")
assert.is_true(fs.mkdir(target_dir, true))
assert.is_true(fs.touch(nested))
local ok = fs.symlink(target_dir, link_dir)
if not ok then
assert.is_true(fs.rm(root, true))
return
end
assert.same({ target_dir }, fs.listdir(root, { type = "directory" }):sort())
assert.same({ link_dir }, fs.listdir(root, { type = "link" }):sort())
assert.same(
{ link_dir, join(link_dir, "nested.txt"), target_dir, nested },
fs.listdir(root, {
recursive = true,
follow = true,
}):sort()
)
assert.is_true(fs.rm(root, true))
end)
it("includes broken symlinks and does not traverse them", function()
local root = make_tmp_dir()
local target = join(root, "missing")
local link = join(root, "broken")
assert.is_true(fs.symlink(target, link))
assert.same({ link }, fs.listdir(root))
assert.same({ link }, fs.listdir(root, { recursive = true }))
assert.same({ link }, fs.listdir(root, { type = "link" }))
assert.is_true(fs.rm(root, true))
end)
end
it("labels option validation errors with the function name", function()
assert.has_error(function()
---@diagnostic disable-next-line: assign-type-mismatch
_ = fs.listdir(cwd, { recursive = "yes" })
end, "listdir.opts.recursive: boolean expected, got string")
end)
end)
describe("dir()", function()
it("yields no items for an empty directory", function()
local root = make_tmp_dir()
local ls = List()
for name, tp in fs.dir(root) do
ls:append(name .. ":" .. tp)
end
assert.is_true(ls:isempty())
assert.is_true(fs.rm(root, true))
end)
it("yields direct child names and types", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local nested_dir = join(subdir, "deep")
local target = join(root, "data.txt")
local nested = join(nested_dir, "nested.txt")
local ls = List()
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(nested, "xyz"))
for name, tp in fs.dir(root) do
ls:append(name .. ":" .. tp)
end
assert.same({ "data.txt:file", "sub:directory" }, ls:sort())
assert.is_true(fs.rm(root, true))
end)
it("supports hidden filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local hidden_dir = join(root, ".hidden")
local nested = join(subdir, "nested.txt")
local hidden_nested = join(hidden_dir, "nested.txt")
local ls = List()
assert.is_true(fs.mkdir(subdir))
assert.is_true(fs.mkdir(hidden_dir))
assert.is_true(fs.write_text(join(root, "data.txt"), "abc"))
assert.is_true(fs.write_text(join(root, ".secret"), "zzz"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(hidden_nested, "qqq"))
local opts = { hidden = false, recursive = true }
for name, tp in fs.dir(root, opts) do
ls:append(name .. ":" .. tp)
end
assert.same({ "data.txt:file", "nested.txt:file", "sub:directory" }, ls:sort())
assert.is_true(fs.rm(root, true))
end)
it("supports file type filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local nested_dir = join(subdir, "deep")
local target = join(root, "data.txt")
local nested = join(nested_dir, "nested.txt")
local files = List()
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(nested, "xyz"))
local opts = { recursive = true, type = "file" }
for name, tp in fs.dir(root, opts) do
files:append(name .. ":" .. tp)
end
assert.same({ "data.txt:file", "nested.txt:file" }, files:sort())
assert.is_true(fs.rm(root, true))
end)
it("supports directory type filtering", function()
local root = make_tmp_dir()
local subdir = join(root, "sub")
local nested_dir = join(subdir, "deep")
local target = join(root, "data.txt")
local nested = join(nested_dir, "nested.txt")
local dirs = List()
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.write_text(nested, "xyz"))
local opts = { recursive = true, type = "directory" }
for name, tp in fs.dir(root, opts) do
dirs:append(name .. ":" .. tp)
end
assert.same({ "deep:directory", "sub:directory" }, dirs:sort())
assert.is_true(fs.rm(root, true))
end)
it("fails for a missing path", function()
local root = make_tmp_dir()
local missing = join(root, "missing")
local iter, errmsg = fs.dir(missing)
assert.is_nil(iter)
assert.is_string(errmsg)
assert.is_true(fs.rm(root, true))
end)
it("fails for a non-directory path", function()
local root = make_tmp_dir()
local target = join(root, "data.txt")
assert.is_true(fs.write_text(target, "abc"))
local iter, errmsg = fs.dir(target)
assert.is_nil(iter)
assert.is_string(errmsg)
assert.is_true(fs.rm(root, true))
end)
if is_unix then
it("supports link type filtering", function()
local root = make_tmp_dir()
local target = join(root, "target.txt")
local link = join(root, "linked.txt")
local links = List()
assert.is_true(fs.touch(target))
local ok = fs.symlink(target, link)
if not ok then
assert.is_true(fs.rm(root, true))
return
end
local opts = { type = "link" }
for name, tp in fs.dir(root, opts) do
links:append(name .. ":" .. tp)
end
assert.same({ "linked.txt:link" }, links)
assert.is_true(fs.rm(root, true))
end)
it("follows symlinked directories when requested", function()
local root = make_tmp_dir()
local target_dir = join(root, "target")
local nested = join(target_dir, "nested.txt")
local link_dir = join(root, "linked")
local ls = List()
assert.is_true(fs.mkdir(target_dir, true))
assert.is_true(fs.touch(nested))
local ok = fs.symlink(target_dir, link_dir)
if not ok then
assert.is_true(fs.rm(root, true))
return
end
local opts = { recursive = true, follow = true }
for name, tp in fs.dir(root, opts) do
ls:append(name .. ":" .. tp)
end
local function contains(xs, value)
for i = 1, #xs do
if xs[i] == value then
return true
end
end
return false
end
assert.is_true(#ls >= 3)
assert.is_true(contains(ls, "linked:link"))
assert.is_true(contains(ls, "nested.txt:file"))
assert.is_true(fs.rm(root, true))
end)
it("includes broken symlinks and does not traverse them", function()
local root = make_tmp_dir()
local target = join(root, "missing")
local link = join(root, "broken")
local ls = List()
assert.is_true(fs.symlink(target, link))
local opts = { recursive = true }
for name, tp in fs.dir(root, opts) do
ls:append(name .. ":" .. tp)
end
assert.same({ "broken:link" }, ls)
assert.is_true(fs.rm(root, true))
end)
end
it("labels option validation errors with the function name", function()
assert.has_error(function()
---@diagnostic disable-next-line: assign-type-mismatch
_ = fs.dir(cwd, { recursive = "yes" })
end, "dir.opts.recursive: boolean expected, got string")
end)
end)
describe("mkdir()", function()
it("creates a directory without parent mode", function()
local root = make_tmp_dir()
local target = join(root, "single")
assert.is_true(fs.mkdir(target))
assert.is_true(fs.exists(target))
assert.is_true(isdir(target))
assert.is_true(fs.rm(root, true))
end)
it("creates missing parent directories when parent mode is enabled", function()
local root = make_tmp_dir()
local target = join(root, "deep", "child")
assert.is_true(fs.mkdir(target, true))
assert.is_true(fs.exists(dirname(target)))
assert.is_true(fs.exists(target))
assert.is_true(isdir(target))
assert.is_true(fs.rm(root, true))
end)
it("succeeds when the target directory already exists", function()
local root = make_tmp_dir()
assert.is_true(fs.mkdir(root, true))
assert.is_true(fs.exists(root))
assert.is_true(isdir(root))
assert.is_true(fs.rm(root, true))
end)
it("normalizes dot segments before creating parent directories", function()
local root = make_tmp_dir()
local target = join(root, "alpha", "..", "beta", ".", "child")
local normalized = join(root, "beta", "child")
assert.is_true(fs.mkdir(target, true))
assert.is_false(fs.exists(join(root, "alpha")))
assert.is_true(fs.exists(normalized))
assert.is_true(isdir(normalized))
assert.is_true(fs.rm(root, true))
end)
it("fails when parent directories are missing without parent mode", function()
local target = join(tmpname(), "missing", "child")
local ok, errmsg, errcode = fs.mkdir(target)
assert.is_nil(ok)
assert.is_string(errmsg)
assert.is_number(errcode)
end)
it("fails when a parent path component is a file", function()
local root = make_tmp_dir()
local parent_file = join(root, "data.txt")
local target = join(parent_file, "child")
assert.is_true(fs.write_text(parent_file, "abc"))
local ok, errmsg, errcode = fs.mkdir(target, true)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
it("fails when the target path already exists as a file", function()
local root = make_tmp_dir()
local target = join(root, "data.txt")
assert.is_true(fs.write_text(target, "abc"))
local ok, errmsg, errcode = fs.mkdir(target, true)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
end)
describe("cp()", function()
it("copies binary files", function()
local root = make_tmp_dir()
local src = join(root, "src.bin")
local dst = join(root, "dst.bin")
local body = "a\0b\1c\255z"
assert.is_true(fs.write_bytes(src, body))
assert.is_true(fs.cp(src, dst))
assert.are_equal(body, fs.read_bytes(dst))
assert.is_true(fs.rm(root, true))
end)
it("copies files", function()
local root = make_tmp_dir()
local src = join(root, "src.txt")
local dst = join(root, "dst.txt")
assert.is_true(fs.write_text(src, "abc"))
assert.is_true(fs.cp(src, dst))
assert.are_equal("abc", fs.read_text(dst))
assert.is_true(fs.rm(root, true))
end)
it("overwrites an existing destination file", function()
local root = make_tmp_dir()
local src = join(root, "src.txt")
local dst = join(root, "dst.txt")
assert.is_true(fs.write_text(src, "new"))
assert.is_true(fs.write_text(dst, "old"))
assert.is_true(fs.cp(src, dst))
assert.are_equal("new", fs.read_text(dst))
assert.is_true(fs.rm(root, true))
end)
it("copies empty directories", function()
local root = make_tmp_dir()
local src = join(root, "src")
local empty = join(src, "empty")
local dst = join(root, "copy")
assert.is_true(fs.mkdir(empty, true))
assert.is_true(fs.cp(src, dst))
assert.is_true(isdir(join(dst, "empty")))
assert.is_true(fs.rm(root, true))
end)
it("copies directories recursively", function()
local root = make_tmp_dir()
local src = join(root, "src")
local top_level = join(src, "top.txt")
local nested_dir = join(src, "deep")
local nested = join(nested_dir, "nested.txt")
local dst = join(root, "copy")
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.write_text(top_level, "abc"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.cp(src, dst))
assert.are_equal("abc", fs.read_text(join(dst, "top.txt")))
assert.is_true(fs.exists(join(dst, "deep")))
assert.are_equal("xyz", fs.read_text(join(dst, "deep", "nested.txt")))
assert.is_true(fs.rm(root, true))
end)
it("merges into an existing destination directory", function()
local root = make_tmp_dir()
local src = join(root, "src")
local nested_dir = join(src, "deep")
local src_file = join(src, "top.txt")
local nested = join(nested_dir, "nested.txt")
local dst = join(root, "copy")
local keep = join(dst, "keep.txt")
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.mkdir(dst, true))
assert.is_true(fs.write_text(src_file, "abc"))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(keep, "keep"))
assert.is_true(fs.cp(src, dst))
assert.are_equal("keep", fs.read_text(keep))
assert.are_equal("abc", fs.read_text(join(dst, "top.txt")))
assert.are_equal("xyz", fs.read_text(join(dst, "deep", "nested.txt")))
assert.is_true(fs.rm(root, true))
end)
it("fails when the source is missing", function()
local root = make_tmp_dir()
local missing = join(root, "missing.txt")
local dst = join(root, "dst.txt")
local ok, errmsg, errcode = fs.cp(missing, dst)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
it("fails when the destination parent is missing", function()
local root = make_tmp_dir()
local src = join(root, "src.txt")
local dst = join(root, "missing", "dst.txt")
assert.is_true(fs.write_text(src, "abc"))
local ok, errmsg, errcode = fs.cp(src, dst)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
it("fails when a nested destination path conflicts with a file", function()
local root = make_tmp_dir()
local src = join(root, "src")
local nested_dir = join(src, "deep")
local nested = join(nested_dir, "nested.txt")
local dst = join(root, "copy")
local conflicting = join(dst, "deep")
assert.is_true(fs.mkdir(nested_dir, true))
assert.is_true(fs.mkdir(dst, true))
assert.is_true(fs.write_text(nested, "xyz"))
assert.is_true(fs.write_text(conflicting, "not a dir"))
local ok, errmsg, errcode = fs.cp(src, dst)
assert.are_same({ "nil", "string", "number" }, { type(ok), type(errmsg), type(errcode) })
assert.is_true(fs.rm(root, true))
end)
it("fails when copying a directory onto itself", function()
local root = make_tmp_dir()
local src = join(root, "src")
local nested = join(src, "nested.txt")
assert.is_true(fs.mkdir(src, true))
assert.is_true(fs.write_text(nested, "xyz"))
local ok, errmsg, errcode = fs.cp(src, src)
local msg = "cannot copy a directory into itself or its descendant"
assert.are_same({ "nil", msg, "nil" }, { type(ok), errmsg, type(errcode) })
assert.are_equal("xyz", fs.read_text(nested))
assert.is_true(fs.rm(root, true))
end)
it("fails when copying a directory into its descendant", function()
local root = make_tmp_dir()
local src = join(root, "src")
local nested = join(src, "nested.txt")
local dst = join(src, "child", "copy")
assert.is_true(fs.mkdir(src, true))
assert.is_true(fs.write_text(nested, "xyz"))
local ok, errmsg, errcode = fs.cp(src, dst)
local msg = "cannot copy a directory into itself or its descendant"
assert.are_same({ "nil", msg, "nil" }, { type(ok), errmsg, type(errcode) })
assert.is_false(fs.exists(dst))
assert.are_equal("xyz", fs.read_text(nested))
assert.is_true(fs.rm(root, true))
end)
end)
describe("rm()", function()
it("removes a file without recursive mode", function()
local target = tmpname()
assert.is_true(fs.write_text(target, "abc"))
assert.is_true(fs.rm(target))
assert.is_false(fs.exists(target))
end)
it("removes an empty directory without recursive mode", function()
local root = make_tmp_dir()
assert.is_true(fs.rm(root))
assert.is_false(fs.exists(root))
end)
it("fails with an error for a non-empty directory without recursive mode", function()
local root = make_tmp_dir()
local target = join(root, "data.txt")
assert.is_true(fs.write_text(target, "abc"))
local ok, err = fs.rm(root)
assert.is_nil(ok)
assert.is_string(err)
assert.is_true(fs.rm(root, true))
end)
it("fails with an error for a non-path", function()
local ok, err = fs.rm("__mods_missing_path__")
assert.is_nil(ok)
assert.is_string(err)
local ok, err = fs.rm("a/b/c", true)