File: file.ml

package info (click to toggle)
ocaml-luv 0.5.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,504 kB
  • sloc: ml: 11,130; makefile: 6,223; sh: 4,592; ansic: 1,517; python: 38
file content (1039 lines) | stat: -rw-r--r-- 29,065 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
open Test_helpers

let statfs_path =
  if not Sys.win32 then
    "file.ml"
  else
    Printf.sprintf "%c:\\" (Sys.getcwd ()).[0]

let with_file_for_reading ?(to_fail = false) f =
  let flags =
    if not to_fail then
      [`RDONLY]
    else
      [`WRONLY]
  in

  let file =
    Luv.File.Sync.open_ "read_test_input" flags
    |> check_success_result "open_"
  in

  f file;

  Luv.File.Sync.close file
  |> check_success_result "close"

let with_file_for_writing f =
  let filename = "write_test_output" in

  let file =
    Luv.File.Sync.open_ filename [`WRONLY; `CREAT; `TRUNC]
    |> check_success_result "open_";
  in

  f file;

  Luv.File.Sync.close file
  |> check_success_result "close";

  let channel = open_in filename in
  let content = input_line channel in
  close_in channel;

  Alcotest.(check string) "content" "ope" content

let with_dummy_file f =
  let filename = "test_dummy" in

  open_out filename |> close_out;

  f filename;

  if Sys.file_exists filename then
    Sys.remove filename

let with_directory f =
  let directory = "dir" in
  let file_1 = "dir/foo" in
  let file_2 = "dir/bar" in

  Unix.mkdir "dir" 0o755;
  open_out file_1 |> close_out;
  open_out file_2 |> close_out;

  f directory;

  Sys.remove file_1;
  Sys.remove file_2;
  Unix.rmdir directory

let call_scandir_next_repeatedly scan =
  let rec repeat entry_accumulator =
    match Luv.File.scandir_next scan with
    | Some entry ->
      repeat (entry::entry_accumulator)
    | None ->
      Luv.File.scandir_end scan;
      entry_accumulator
  in
  repeat []

let tests = [
  "file", [
    "open, read, close: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.open_ "read_test_input" [`RDONLY] begin fun result ->
        let file = check_success_result "file" result in

        let buffer = Luv.Buffer.create 9 in
        Luv.Buffer.fill buffer '\000';

        Luv.File.read file [buffer] begin fun result ->
          let length =
            check_success_result "read result" result
            |> Unsigned.Size_t.to_int
          in
          Alcotest.(check int) "byte count" 9 length;

          Luv.Buffer.sub buffer ~offset:0 ~length
          |> Luv.Buffer.to_string
          |> Alcotest.(check string) "data" "open Foo\n";

          Luv.File.read file [buffer] begin fun result ->
            check_success_result "read result (eof)" result
            |> Unsigned.Size_t.to_int
            |> Alcotest.(check int) "byte count (eof)" 0;

            Luv.File.close file begin fun result ->
              check_success_result "close result" result;
              finished := true
            end
          end
        end
      end;

      run ();

      Alcotest.(check bool) "finished" true !finished
    end;

    "open, read, close: sync", `Quick, begin fun () ->
      let file =
        Luv.File.Sync.open_ "read_test_input" [`RDONLY]
        |> check_success_result "open_"
      in

      let buffer = Luv.Buffer.create 9 in
      Luv.Buffer.fill buffer '\000';

      let length =
        Luv.File.Sync.read file [buffer]
        |> check_success_result "read"
        |> Unsigned.Size_t.to_int
      in

      Alcotest.(check int) "byte count" 9 length;

      Luv.Buffer.sub buffer ~offset:0 ~length
      |> Luv.Buffer.to_string
      |> Alcotest.(check string) "data" "open Foo\n";

      Luv.File.Sync.read file [buffer]
      |> check_success_result "read (eof)"
      |> Unsigned.Size_t.to_int
      |> Alcotest.(check int) "byte count (eof)" 0;

      Luv.File.Sync.close file
      |> check_success_result "close"
    end;

    "open: nonexistent, async", `Quick, begin fun () ->
      let result = ref (Result.Error `UNKNOWN) in

      Luv.File.open_ "non_existent_file" [`RDONLY] begin fun result' ->
        result := result'
      end;

      run ();
      check_error_result "result" `ENOENT !result
    end;

    "open: nonexistent, sync", `Quick, begin fun () ->
      Luv.File.Sync.open_ "non_existent_file" [`RDONLY]
      |> check_error_result "open_" `ENOENT
    end;

    "open, close: memory leak, async", `Quick, begin fun () ->
      no_memory_leak begin fun _ ->
        let finished = ref false in

        Luv.File.open_ "file.ml" [`RDONLY] begin fun result ->
          let file = check_success_result "file" result in
          Luv.File.close file begin fun _ ->
            finished := true
          end
        end;

        run ();
        Alcotest.(check bool) "finished" true !finished
      end
    end;

    "open, close: memory leak, sync", `Quick, begin fun () ->
      no_memory_leak begin fun _ ->
        let file =
          Luv.File.Sync.open_ "file.ml" [`RDONLY]
          |> check_success_result "open_"
        in

        Luv.File.Sync.close file
        |> check_success_result "close"
      end
    end;

    "open: failure leak, async", `Quick, begin fun () ->
      no_memory_leak begin fun _ ->
        Luv.File.open_ "non_existent_file" [`RDONLY] begin fun result ->
          check_error_result "result" `ENOENT result
        end;

        run ()
      end
    end;

    "open: failure leak, sync", `Quick, begin fun () ->
      no_memory_leak begin fun _ ->
        Luv.File.Sync.open_ "non_existent_file" [`RDONLY]
        |> check_error_result "open_" `ENOENT;
      end
    end;

    "open: gc", `Quick, begin fun () ->
      Gc.full_major ();

      let called = ref false in

      Luv.File.open_ "non_existent_file" [`RDONLY] begin fun _result ->
        called := true
      end;

      Gc.full_major ();

      run ();
      Alcotest.(check bool) "called" true !called
    end;

    "open: exception", `Quick, begin fun () ->
      check_exception Exit begin fun () ->
        Luv.File.open_ "non_existent_file" [`RDONLY] begin fun _result ->
          raise Exit
        end;
        run ()
      end
    end;

    "read failure: async", `Quick, begin fun () ->
      with_file_for_reading ~to_fail:true begin fun file ->
        let buffer = Luv.Buffer.create 1 in

        Luv.File.read file [buffer] begin fun result ->
          check_error_results "byte_count" [`EBADF; `EPERM] result
        end;

        run ()
      end
    end;

    "read failure: sync", `Quick, begin fun () ->
      with_file_for_reading ~to_fail:true begin fun file ->
        let buffer = Luv.Buffer.create 1 in

        Luv.File.Sync.read file [buffer]
        |> check_error_results "read" [`EBADF; `EPERM]
      end
    end;

    "read leak: async", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        let buffer = Luv.Buffer.create 1 in

        no_memory_leak begin fun _ ->
          let finished = ref false in

          Luv.File.read file [buffer] begin fun _ ->
            finished := true
          end;

          run ();
          Alcotest.(check bool) "finished" true !finished
        end
      end
    end;

    "read leak: sync", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        let buffer = Luv.Buffer.create 1 in

        no_memory_leak begin fun _ ->
          Luv.File.Sync.read file [buffer]
          |> check_success_result "read"
          |> ignore;
        end
      end
    end;

    "read sync failure leak", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        no_memory_leak begin fun _ ->
          Luv.File.read file [] ignore;
          run ()
        end
      end
    end;

    "read gc", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        Gc.full_major ();

        let called = ref false in
        let buffer = Luv.Buffer.from_string "\000" in

        let finalized = ref false in
        Gc.finalise (fun _ -> finalized := true) buffer;

        Luv.File.read file [buffer] begin fun _ ->
          called := true
        end;

        Gc.full_major ();
        Alcotest.(check bool) "finalized (1)" false !finalized;

        run ();
        Alcotest.(check bool) "called" true !called;

        Gc.full_major ();
        Alcotest.(check bool) "finalized (2)" true !finalized
      end
    end;

    "write: async", `Quick, begin fun () ->
      with_file_for_writing begin fun file ->
        let buffer = Luv.Buffer.from_string "ope" in

        Luv.File.write file [buffer] begin fun result ->
          let byte_count = check_success_result "write result" result in
          Alcotest.(check int)
            "byte count" 3 (Unsigned.Size_t.to_int byte_count)
        end;

        run ()
      end
    end;

    "write: sync", `Quick, begin fun () ->
      with_file_for_writing begin fun file ->
        let buffer = Luv.Buffer.from_string "ope" in

        Luv.File.Sync.write file [buffer]
        |> check_success_result "write"
        |> ignore
      end
    end;

    "unlink: async", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        Alcotest.(check bool) "exists" true (Sys.file_exists path);

        Luv.File.unlink path begin fun result ->
          check_success_result "result" result
        end;

        run ();
        Alcotest.(check bool) "does not exist" false (Sys.file_exists path)
      end
    end;

    "unlink: sync", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        Alcotest.(check bool) "exists" true (Sys.file_exists path);

        Luv.File.Sync.unlink path
        |> check_success_result "unlink";

        Alcotest.(check bool) "does not exist" false (Sys.file_exists path)
      end
    end;

    "unlink failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.unlink "non_existent_file" begin fun result ->
        check_error_result "result" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "unlink failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.unlink "non_existent_file"
      |> check_error_result "unlink" `ENOENT
    end;

    "mkdir, rmdir: async", `Quick, begin fun () ->
      let finished = ref false in
      let directory = "dummy_directory" in

      Luv.File.mkdir directory begin fun result ->
        check_success_result "mkdir result" result;
        Alcotest.(check bool) "exists" true (Sys.file_exists directory);

        Luv.File.rmdir directory begin fun result ->
          check_success_result "rmdir result" result;
          Alcotest.(check bool)
            "does not exist" false (Sys.file_exists directory);

          finished := true
        end
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "mkdir, rmdir: sync", `Quick, begin fun () ->
      let directory = "dummy_directory" in

      Luv.File.Sync.mkdir directory
      |> check_success_result "mkdir";

      Alcotest.(check bool) "exists" true (Sys.file_exists directory);

      Luv.File.Sync.rmdir directory
      |> check_success_result "rmdir";

      Alcotest.(check bool) "does not exist" false (Sys.file_exists directory)
    end;

    "mkdir failure: async", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        let finished = ref false in

        Luv.File.mkdir path begin fun result ->
          check_error_result "mkdir result" `EEXIST result;
          finished := true
        end;

        run ();
        Alcotest.(check bool) "finished" true !finished
      end
    end;

    "mkdir failure: sync", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        Luv.File.Sync.mkdir path
        |> check_error_result "mkdir" `EEXIST
      end
    end;

    "rmdir failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.rmdir "non_existent_file" begin fun result ->
        check_error_result "rmdir result" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "rmdir failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.rmdir "non_existent_file"
      |> check_error_result "rmdir" `ENOENT
    end;

    "mkdtemp: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.mkdtemp "fooXXXXXX" begin fun result ->
        let path = check_success_result "mkdtemp result" result in

        Luv.File.rmdir path begin fun result ->
          check_success_result "rmdir result" result;
          finished := true
        end
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "mkdtemp: sync", `Quick, begin fun () ->
      let path =
        Luv.File.Sync.mkdtemp "fooXXXXXX"
        |> check_success_result "mkdtemp"
      in

      Luv.File.Sync.rmdir path
      |> check_success_result "rmdir"
    end;

    "mkdtemp failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.mkdtemp "non-existent/fooXXXXXX" begin fun result ->
        check_error_result "mkdtemp result" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "mkdtemp failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.mkdtemp "non-existent/fooXXXXXX"
      |> check_error_result "mkdtemp result" `ENOENT
    end;

    "mkstemp: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.mkstemp "fooXXXXXX" begin fun result ->
        let path, file = check_success_result "mkstemp result" result in

        Luv.File.close file begin fun result ->
          check_success_result "close" result;

          Luv.File.unlink path begin fun result ->
            check_success_result "unlink" result;
            finished := true
          end
        end
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "mkstemp: sync", `Quick, begin fun () ->
      let path, file =
        Luv.File.Sync.mkstemp "fooXXXXXX"
        |> check_success_result "mkstemp"
      in

      Luv.File.Sync.close file
      |> check_success_result "close";

      Luv.File.Sync.unlink path
      |> check_success_result "unlink"
    end;

    "mkstemp failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.mkstemp "non-existent/fooXXXXXX" begin fun result ->
        check_error_result "mkstemp result" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "mkstemp failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.mkstemp "non-existent/fooXXXXXX"
      |> check_error_result "mkstemp result" `ENOENT
    end;

    "opendir, closedir: async", `Quick, begin fun () ->
      with_directory begin fun directory ->
        Luv.File.opendir directory begin fun result ->
          let dir = check_success_result "opendir" result in
          Luv.File.closedir dir (check_success_result "closedir")
        end;

        run ()
      end
    end;

    "opendir, closedir: sync", `Quick, begin fun () ->
      with_directory begin fun directory ->
        Luv.File.Sync.opendir directory
        |> check_success_result "opendir"
        |> Luv.File.Sync.closedir
        |> check_success_result "closedir"
      end
    end;

    "readdir: async", `Quick, begin fun () ->
      with_directory begin fun directory ->
        Luv.File.opendir directory begin fun result ->
          let dir = check_success_result "opendir" result in

          Luv.File.readdir dir begin fun result ->
            check_success_result "readdir" result
            |> Array.to_list
            |> check_directory_entries "entries" ["foo"; "bar"];

            Luv.File.closedir dir (check_success_result "closedir")
          end
        end;

        run ()
      end
    end;

    "readdir: sync", `Quick, begin fun () ->
      with_directory begin fun directory ->
        let dir =
          Luv.File.Sync.opendir directory |> check_success_result "opendir" in

        Luv.File.Sync.readdir dir
        |> check_success_result "readdir"
        |> Array.to_list
        |> check_directory_entries "entries" ["foo"; "bar"];

        Luv.File.Sync.closedir dir |> check_success_result "closedir"
      end
    end;

    "readdir: limit", `Quick, begin fun () ->
      with_directory begin fun directory ->
        let dir =
          Luv.File.Sync.opendir directory |> check_success_result "opendir" in

        Luv.File.Sync.readdir ~number_of_entries:0 dir
        |> check_success_result "readdir"
        |> Array.to_list
        |> check_directory_entries "entries" [];

        Luv.File.Sync.closedir dir |> check_success_result "closedir"
      end
    end;

    "readdir: gc", `Quick, begin fun () ->
      with_directory begin fun directory ->
        Luv.File.opendir directory begin fun result ->
          let dir = check_success_result "opendir" result in

          Luv.File.readdir dir begin fun result ->
            check_success_result "readdir" result
            |> Array.to_list
            |> check_directory_entries "entries" ["foo"; "bar"];

            Luv.File.closedir dir (check_success_result "closedir")
          end;

          Gc.full_major ();
        end;

        run ()
      end
    end;

    "scandir: async", `Quick, begin fun () ->
      with_directory begin fun directory ->
        let entries = ref [] in

        Luv.File.scandir directory begin fun result ->
          entries :=
            check_success_result "scandir" result
            |> call_scandir_next_repeatedly
        end;

        run ();
        check_directory_entries "scandir_next" ["foo"; "bar"] !entries
      end
    end;

    "scandir: sync", `Quick, begin fun () ->
      with_directory begin fun directory ->
        Luv.File.Sync.scandir directory
        |> check_success_result "scandir"
        |> call_scandir_next_repeatedly
        |> check_directory_entries "scandir_next" ["foo"; "bar"]
      end
    end;

    "scandir failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.scandir "non_existent_directory" begin fun result ->
        check_error_result "scandir" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "scandir failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.scandir "non_existent_directory"
      |> check_error_result "scandir" `ENOENT
    end;

    "stat: async", `Quick, begin fun () ->
      let size = ref 0 in

      Luv.File.stat "file.ml" begin fun result ->
        check_success_result "stat" result
        |> fun stat -> size := Unsigned.UInt64.to_int Luv.File.Stat.(stat.size)
      end;

      run ();
      Alcotest.(check int) "size" Unix.((stat "file.ml").st_size) !size
    end;

    "stat: sync", `Quick, begin fun () ->
      Luv.File.Sync.stat "file.ml"
      |> check_success_result "stat"
      |> fun stat -> Luv.File.Stat.(stat.size)
      |> Unsigned.UInt64.to_int
      |> Alcotest.(check int) "size" Unix.((stat "file.ml").st_size)
    end;

    "stat failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.stat "non_existent_file" begin fun result ->
        check_error_result "stat" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "stat failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.stat "non_existent_file"
      |> check_error_result "stat" `ENOENT
    end;

    "lstat: async", `Quick, begin fun () ->
      let size = ref 0 in

      Luv.File.lstat "file.ml" begin fun result ->
        check_success_result "lstat" result
        |> fun stat -> size := Unsigned.UInt64.to_int Luv.File.Stat.(stat.size)
      end;

      run ();
      Alcotest.(check int) "size" Unix.((lstat "file.ml").st_size) !size
    end;

    "lstat: sync", `Quick, begin fun () ->
      Luv.File.Sync.lstat "file.ml"
      |> check_success_result "lstat"
      |> fun stat -> Luv.File.Stat.(stat.size)
      |> Unsigned.UInt64.to_int
      |> Alcotest.(check int) "size" Unix.((lstat "file.ml").st_size)
    end;

    "lstat failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.lstat "non_existent_file" begin fun result ->
        check_error_result "lstat" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "lstat failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.lstat "non_existent_file"
      |> check_error_result "lstat" `ENOENT
    end;

    "fstat: async", `Quick, begin fun () ->
      let size = ref 0 in

      with_file_for_reading begin fun file ->
        Luv.File.fstat file begin fun result ->
          check_success_result "fstat" result
          |> fun stat ->
            size := Unsigned.UInt64.to_int Luv.File.Stat.(stat.size)
        end;

        run ()
      end;

      Alcotest.(check int) "size" Unix.((stat "read_test_input").st_size) !size
    end;

    "fstat: sync", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        Luv.File.Sync.fstat file
        |> check_success_result "fstat"
        |> fun stat -> Luv.File.Stat.(stat.size)
        |> Unsigned.UInt64.to_int
        |> Alcotest.(check int) "size" Unix.((stat "read_test_input").st_size)
      end
    end;

    "bit test: one bit", `Quick, begin fun () ->
      Luv__Helpers.Bit_field.test (fun i -> i) [0b0001] 0b0111
      |> Alcotest.(check bool) "test" true
    end;

    "bit test: multiple bits", `Quick, begin fun () ->
      Luv__Helpers.Bit_field.test (fun i -> i) [0b0001; 0b0010] 0b0111
      |> Alcotest.(check bool) "test" true
    end;

    "bit test: partial", `Quick, begin fun () ->
      Luv__Helpers.Bit_field.test (fun i -> i) [0b0100; 0b1000] 0b0111
      |> Alcotest.(check bool) "test" false
    end;

    "statfs: async", `Quick, begin fun () ->
      Luv.File.statfs statfs_path begin fun result ->
        check_success_result "statfs" result |> ignore
      end;

      run ()
    end;

    "statfs: sync", `Quick, begin fun () ->
      Luv.File.Sync.statfs statfs_path
      |> check_success_result "statfs"
      |> ignore
    end;

    "statfs failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.statfs "non_existent_file" begin fun result ->
        check_error_result "statfs" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "statfs failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.statfs "non_existent_file"
      |> check_error_result "statfs" `ENOENT
    end;

    "rename: async", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        let to_ = path ^ ".renamed" in

        Alcotest.(check bool) "original at start" true (Sys.file_exists path);
        Alcotest.(check bool) "new at start" false (Sys.file_exists to_);

        Luv.File.rename path ~to_ (check_success_result "rename");
        run ();

        Alcotest.(check bool) "original at end" false (Sys.file_exists path);
        Alcotest.(check bool) "new at end" true (Sys.file_exists to_);

        Sys.remove to_
      end
    end;

    "rename: sync", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        let to_ = path ^ ".renamed" in

        Alcotest.(check bool) "original at start" true (Sys.file_exists path);
        Alcotest.(check bool) "new at start" false (Sys.file_exists to_);

        Luv.File.Sync.rename path ~to_
        |> check_success_result "rename";

        Alcotest.(check bool) "original at end" false (Sys.file_exists path);
        Alcotest.(check bool) "new at end" true (Sys.file_exists to_);

        Sys.remove to_
      end
    end;

    "rename failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.rename "non_existent_file" ~to_:"foo" begin fun result ->
        check_error_result "rename" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "rename failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.rename "non_existent_file" ~to_:"foo"
      |> check_error_result "rename" `ENOENT
    end;

    "ftruncate: async", `Quick, begin fun () ->
      let buffer = Luv.Buffer.from_string "open" in

      with_file_for_writing begin fun file ->
        Luv.File.Sync.write file [buffer]
        |> check_success_result "write"
        |> Unsigned.Size_t.to_int
        |> Alcotest.(check int) "bytes written" 4;

        Luv.File.ftruncate file 3L (check_success_result "ftruncate");
        run ()
      end
    end;

    "ftruncate: sync", `Quick, begin fun () ->
      let buffer = Luv.Buffer.from_string "open" in

      with_file_for_writing begin fun file ->
        Luv.File.Sync.write file [buffer]
        |> check_success_result "write"
        |> Unsigned.Size_t.to_int
        |> Alcotest.(check int) "bytes written" 4;

        Luv.File.Sync.ftruncate file 3L
        |> check_success_result "ftruncate"
      end
    end;

    "ftruncate failure: async", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        let finished = ref false in

        Luv.File.ftruncate file 0L begin fun result ->
          check_error_results "ftruncate" [`EINVAL; `EPERM] result;
          finished := true
        end;

        run ();
        Alcotest.(check bool) "finished" true !finished
      end
    end;

    "ftruncate failure: sync", `Quick, begin fun () ->
      with_file_for_reading begin fun file ->
        Luv.File.Sync.ftruncate file 0L
        |> check_error_results "ftruncate" [`EINVAL; `EPERM]
      end
    end;

    "copyfile: async", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        let to_ = path ^ ".copy" in

        Alcotest.(check bool) "original at start" true (Sys.file_exists path);
        Alcotest.(check bool) "new at start" false (Sys.file_exists to_);

        Luv.File.copyfile path ~to_ (check_success_result "copyfile");
        run ();

        Alcotest.(check bool) "original at end" true (Sys.file_exists path);
        Alcotest.(check bool) "new at end" true (Sys.file_exists to_);

        Sys.remove to_
      end
    end;

    "copyfile: sync", `Quick, begin fun () ->
      with_dummy_file begin fun path ->
        let to_ = path ^ ".copy" in

        Alcotest.(check bool) "original at start" true (Sys.file_exists path);
        Alcotest.(check bool) "new at start" false (Sys.file_exists to_);

        Luv.File.Sync.copyfile path ~to_
        |> check_success_result "copyfile";

        Alcotest.(check bool) "original at end" true (Sys.file_exists path);
        Alcotest.(check bool) "new at end" true (Sys.file_exists to_);

        Sys.remove to_
      end
    end;

    "copyfile failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.copyfile "non_existent_file" ~to_:"foo" begin fun result ->
        check_error_result "copyfile" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "copyfile failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.copyfile "non_existent_file" ~to_:"foo"
      |> check_error_result "copyfile" `ENOENT
    end;

    "sendfile: async", `Quick, begin fun () ->
      with_file_for_reading begin fun from ->
        with_file_for_writing begin fun to_ ->
          Luv.File.sendfile
              ~to_ from ~offset:0L (Unsigned.Size_t.of_int 3)
              begin fun result ->

            check_success_result "sendfile" result
            |> Unsigned.Size_t.to_int
            |> Alcotest.(check int) "byte count" 3
          end;

          run ()
        end
      end
    end;

    "sendfile: sync", `Quick, begin fun () ->
      with_file_for_reading begin fun from ->
        with_file_for_writing begin fun to_ ->
          Luv.File.Sync.sendfile
            ~to_ from ~offset:0L (Unsigned.Size_t.of_int 3)
          |> check_success_result "sendfile"
          |> Unsigned.Size_t.to_int
          |> Alcotest.(check int) "byte count" 3
        end
      end
    end;

    "access: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.access "file.ml" [`R_OK] begin fun result ->
        check_success_result "access" result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "access: sync", `Quick, begin fun () ->
      Luv.File.Sync.access "file.ml" [`R_OK]
      |> check_success_result "access"
    end;

    "access failure: async", `Quick, begin fun () ->
      let finished = ref false in

      Luv.File.access "non_existent_file" [`R_OK] begin fun result ->
        check_error_result "access" `ENOENT result;
        finished := true
      end;

      run ();
      Alcotest.(check bool) "finished" true !finished
    end;

    "access failure: sync", `Quick, begin fun () ->
      Luv.File.Sync.access "non_existent_file" [`R_OK]
      |> check_error_result "access" `ENOENT
    end;
  ]
]