1+ module Int63 = Optint. Int63
2+ module Path = Eio. Path
3+
4+ let () = Eio.Exn.Backend. show := false
5+
6+ open Eio.Std
7+
8+ let ( / ) = Path. ( / )
9+
10+ let try_read_file path =
11+ match Path. load path with
12+ | s -> traceln " read %a -> %S" Path. pp path s
13+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
14+
15+ let try_write_file ~create ?append path content =
16+ match Path. save ~create ?append path content with
17+ | () -> traceln " write %a -> ok" Path. pp path
18+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
19+
20+ let try_mkdir path =
21+ match Path. mkdir path ~perm: 0o700 with
22+ | () -> traceln " mkdir %a -> ok" Path. pp path
23+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
24+
25+ let try_rename p1 p2 =
26+ match Path. rename p1 p2 with
27+ | () -> traceln " rename %a to %a -> ok" Path. pp p1 Path. pp p2
28+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
29+
30+ let try_read_dir path =
31+ match Path. read_dir path with
32+ | names -> traceln " read_dir %a -> %a" Path. pp path Fmt.Dump. (list string ) names
33+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
34+
35+ let try_unlink path =
36+ match Path. unlink path with
37+ | () -> traceln " unlink %a -> ok" Path. pp path
38+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
39+
40+ let try_rmdir path =
41+ match Path. rmdir path with
42+ | () -> traceln " rmdir %a -> ok" Path. pp path
43+ | exception ex -> traceln " @[<h>%a@]" Eio.Exn. pp ex
44+
45+ let with_temp_file path fn =
46+ Fun. protect (fun () -> fn path) ~finally: (fun () -> Eio.Path. unlink path)
47+
48+ let chdir path =
49+ traceln " chdir %S" path;
50+ Unix. chdir path
51+
52+ let assert_kind path kind =
53+ Path. with_open_in path @@ fun file ->
54+ assert ((Eio.File. stat file).kind = kind)
55+
56+ let test_create_and_read env () =
57+ let cwd = Eio.Stdenv. cwd env in
58+ let data = " my-data" in
59+ with_temp_file (cwd / " test-file" ) @@ fun path ->
60+ Path. save ~create: (`Exclusive 0o666 ) path data;
61+ Alcotest. (check string ) " same data" data (Path. load path)
62+
63+ let test_cwd_no_access_abs env () =
64+ let cwd = Eio.Stdenv. cwd env in
65+ let temp = Filename. temp_file " eio" " win" in
66+ try
67+ Path. save ~create: (`Exclusive 0o666 ) (cwd / temp) " my-data" ;
68+ failwith " Should have failed"
69+ with Eio. Io (Eio.Fs. E (Permission_denied _ ), _ ) -> ()
70+
71+ let test_exclusive env () =
72+ let cwd = Eio.Stdenv. cwd env in
73+ with_temp_file (cwd / " test-file" ) @@ fun path ->
74+ Path. save ~create: (`Exclusive 0o666 ) path " first-write" ;
75+ try
76+ Path. save ~create: (`Exclusive 0o666 ) path " first-write" ;
77+ failwith " Should have failed"
78+ with Eio. Io (Eio.Fs. E (Already_exists _ ), _ ) -> ()
79+
80+ let test_if_missing env () =
81+ let cwd = Eio.Stdenv. cwd env in
82+ let test_file = (cwd / " test-file" ) in
83+ with_temp_file test_file @@ fun test_file ->
84+ Path. save ~create: (`If_missing 0o666 ) test_file " 1st-write-original" ;
85+ Path. save ~create: (`If_missing 0o666 ) test_file " 2nd-write" ;
86+ Alcotest. (check string ) " same contents" " 2nd-write-original" (Path. load test_file)
87+
88+ let test_trunc env () =
89+ let cwd = Eio.Stdenv. cwd env in
90+ let test_file = (cwd / " test-file" ) in
91+ with_temp_file test_file @@ fun test_file ->
92+ Path. save ~create: (`Or_truncate 0o666 ) test_file " 1st-write-original" ;
93+ Path. save ~create: (`Or_truncate 0o666 ) test_file " 2nd-write" ;
94+ Alcotest. (check string ) " same contents" " 2nd-write" (Path. load test_file)
95+
96+ let test_empty env () =
97+ let cwd = Eio.Stdenv. cwd env in
98+ let test_file = (cwd / " test-file" ) in
99+ try
100+ Path. save ~create: `Never test_file " 1st-write-original" ;
101+ traceln " Got %S" @@ Path. load test_file;
102+ failwith " Should have failed"
103+ with Eio. Io (Eio.Fs. E (Not_found _ ), _ ) -> ()
104+
105+ let test_append env () =
106+ let cwd = Eio.Stdenv. cwd env in
107+ let test_file = (cwd / " test-file" ) in
108+ with_temp_file test_file @@ fun test_file ->
109+ Path. save ~create: (`Or_truncate 0o666 ) test_file " 1st-write-original" ;
110+ Path. save ~create: `Never ~append: true test_file " 2nd-write" ;
111+ Alcotest. (check string ) " append" " 1st-write-original2nd-write" (Path. load test_file)
112+
113+ let tests env = [
114+ " create-write-read" , `Quick , test_create_and_read env;
115+ " cwd-abs-path" , `Quick , test_cwd_no_access_abs env;
116+ " create-exclusive" , `Quick , test_exclusive env;
117+ " create-if_missing" , `Quick , test_if_missing env;
118+ " create-trunc" , `Quick , test_trunc env;
119+ " create-empty" , `Quick , test_empty env;
120+ " append" , `Quick , test_append env;
121+ ]
0 commit comments