Skip to content

Commit 10f57fe

Browse files
committed
Tests: improve sys_fs tests
1 parent 5ee1426 commit 10f57fe

File tree

1 file changed

+62
-37
lines changed

1 file changed

+62
-37
lines changed

compiler/tests-compiler/sys_fs.ml

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
open Util
2121

22-
let%expect_test _ =
22+
let%expect_test "readdir fails on non-existing directory and existing files" =
2323
compile_and_run
2424
{|
2525
let f () =
@@ -31,15 +31,19 @@ let f () =
3131
| e -> print_endline (Printexc.to_string e));
3232
(try ignore(Sys.readdir "aaa/bbb") with
3333
| Sys_error _ -> ()
34-
| e -> print_endline (Printexc.to_string e));
34+
| e -> print_endline (Printexc.to_string e));
35+
Array.iter print_endline (Sys.readdir "aaa");
3536
Sys.remove "aaa/bbb";
3637
Sys.rmdir "aaa"
3738
in
3839
f (); Sys.chdir "/static"; f ()
3940
|};
40-
[%expect {| |}]
41+
[%expect {|
42+
bbb
43+
bbb
44+
|}]
4145

42-
let%expect_test _ =
46+
let%expect_test "rmdir work on empty directory only" =
4347
compile_and_run
4448
{|
4549
let f () =
@@ -48,7 +52,7 @@ let f () =
4852
let l = Sys.readdir "aaa" |> Array.to_list in
4953
List.iter print_endline l;
5054
(match Sys.rmdir "aaa" with
51-
| exception _ -> ()
55+
| exception _ -> print_endline "EXPECTED ERROR"
5256
| _ -> print_endline "BUG");
5357
Sys.rmdir "aaa/bbb";
5458
Sys.rmdir "aaa"
@@ -57,10 +61,12 @@ f (); Sys.chdir "/static"; f ()
5761
|};
5862
[%expect {|
5963
bbb
60-
bbb|}]
64+
EXPECTED ERROR
65+
bbb
66+
EXPECTED ERROR
67+
|}]
6168

62-
let%expect_test _ =
63-
(* Check we can rename a directory *)
69+
let%expect_test "Rename a directory" =
6470
compile_and_run
6571
{|
6672
let f () =
@@ -88,8 +94,7 @@ f (); Sys.chdir "/static"; f ()
8894
new file contents: Hello world
8995
|}]
9096

91-
let%expect_test _ =
92-
(* Check we can rename a directory over another directory *)
97+
let%expect_test "Rename a directory over another (empty) directory" =
9398
compile_and_run
9499
{|
95100
let f () =
@@ -118,8 +123,7 @@ f (); Sys.chdir "/static"; f ()
118123
new file contents: Hello world
119124
|}]
120125

121-
let%expect_test _ =
122-
(* Check we can't rename a directory over another non-empty directory *)
126+
let%expect_test "Can't rename a directory over another non-empty directory" =
123127
compile_and_run
124128
{|
125129
let f () =
@@ -134,7 +138,7 @@ let f () =
134138
Printf.fprintf oc "Hello world\n";
135139
close_out oc;
136140
(match Sys.rename "aaa/bbb" "aaa/bbb2" with
137-
| exception Sys_error _ -> ()
141+
| exception Sys_error _ -> print_endline "EXPECTED ERROR"
138142
| _ -> failwith "BUG: rename should have failed");
139143
Sys.remove "aaa/bbb/ccc/ddd";
140144
Sys.rmdir "aaa/bbb/ccc";
@@ -145,10 +149,12 @@ let f () =
145149
in
146150
f (); Sys.chdir "/static"; f ()
147151
|};
148-
[%expect {| |}]
152+
[%expect {|
153+
EXPECTED ERROR
154+
EXPECTED ERROR
155+
|}]
149156

150-
let%expect_test _ =
151-
(* Check we can rename a file to a pre-existing file *)
157+
let%expect_test "Rename a file to a pre-existing file" =
152158
compile_and_run
153159
{|
154160
let f () =
@@ -166,18 +172,20 @@ let f () =
166172
Printf.printf "contents of 'bbb': %s\n%!" line;
167173
Sys.remove "bbb";
168174
(match Sys.remove "aaa" with
169-
| exception _ -> ()
175+
| exception _ -> print_endline "EXPECTED ERROR"
170176
| _ -> print_endline "BUG")
171177
in
172178
f (); Sys.chdir "/static"; f ()
173179
|};
174-
[%expect {|
180+
[%expect
181+
{|
175182
contents of 'bbb': aaa
183+
EXPECTED ERROR
176184
contents of 'bbb': aaa
185+
EXPECTED ERROR
177186
|}]
178187

179-
let%expect_test _ =
180-
(* Check we can't rename a directory over a file *)
188+
let%expect_test "Can't rename a directory over a file" =
181189
compile_and_run
182190
{|
183191
let f () =
@@ -189,11 +197,11 @@ let f () =
189197
close_out oc;
190198
let oc = open_out "aaa/bbb2" in
191199
Printf.fprintf oc "Hello world\n";
192-
close_out oc;
200+
close_out oc;
193201
(match Sys.rename "aaa/bbb" "aaa/bbb2"
194-
with
202+
with
195203
| () -> failwith "BUG: rename should have failed"
196-
| exception Sys_error _ -> ());
204+
| exception Sys_error _ -> print_endline "EXPECTED ERROR");
197205
Sys.remove "aaa/bbb/ccc/ddd";
198206
Sys.rmdir "aaa/bbb/ccc";
199207
Sys.rmdir "aaa/bbb";
@@ -202,10 +210,12 @@ let f () =
202210
in
203211
f (); Sys.chdir "/static"; f ()
204212
|};
205-
[%expect {| |}]
213+
[%expect {|
214+
EXPECTED ERROR
215+
EXPECTED ERROR
216+
|}]
206217

207-
let%expect_test _ =
208-
(* Check we can't rename a file over a directory *)
218+
let%expect_test "Can't rename a file over a directory" =
209219
compile_and_run
210220
{|
211221
let f () =
@@ -217,9 +227,9 @@ let f () =
217227
close_out oc;
218228
let oc = open_out "aaa/bbb2" in
219229
Printf.fprintf oc "Hello world\n";
220-
close_out oc;
230+
close_out oc;
221231
(match Sys.rename "aaa/bbb2" "aaa/bbb" with
222-
| exception Sys_error _ -> ()
232+
| exception Sys_error _ -> print_endline "EXPECTED ERROR"
223233
| _ -> failwith "BUG: rename should have failed");
224234
Sys.remove "aaa/bbb/ccc/ddd";
225235
Sys.rmdir "aaa/bbb/ccc";
@@ -229,24 +239,39 @@ let f () =
229239
in
230240
f (); Sys.chdir "/static"; f ()
231241
|};
232-
[%expect {| |}]
242+
[%expect {|
243+
EXPECTED ERROR
244+
EXPECTED ERROR
245+
|}]
233246

234-
let%expect_test _ =
247+
let%expect_test "mkdir in non-existing directory" =
235248
compile_and_run
236249
{|
237-
(match Sys.mkdir "/not/exists" 0o777 with
250+
let f () =
251+
(match Sys.mkdir "not/exists" 0o777 with
238252
| exception Sys_error path -> print_endline "EXPECTED ERROR"
239253
| exception err -> print_endline (Printexc.to_string err)
240-
| _ -> print_endline "BUG");
254+
| _ -> print_endline "BUG")
255+
in
256+
f (); Sys.chdir "/static"; f ()
241257
|};
242-
[%expect {|EXPECTED ERROR|}]
258+
[%expect {|
259+
EXPECTED ERROR
260+
EXPECTED ERROR
261+
|}]
243262

244-
let%expect_test _ =
263+
let%expect_test "rmdir non-existing directory" =
245264
compile_and_run
246265
{|
247-
(match Sys.rmdir "/not/exists" with
266+
let f () =
267+
(match Sys.rmdir "not/exists" with
248268
| exception Sys_error path -> print_endline "EXPECTED ERROR"
249269
| exception err -> print_endline (Printexc.to_string err)
250-
| _ -> print_endline "BUG");
270+
| _ -> print_endline "BUG")
271+
in
272+
f (); Sys.chdir "/static"; f ()
251273
|};
252-
[%expect {|EXPECTED ERROR|}]
274+
[%expect {|
275+
EXPECTED ERROR
276+
EXPECTED ERROR
277+
|}]

0 commit comments

Comments
 (0)