Skip to content

Commit 665547c

Browse files
committed
Make error msg matching cases more uniform
1 parent 7467ea5 commit 665547c

File tree

1 file changed

+45
-58
lines changed

1 file changed

+45
-58
lines changed

src/sys/stm_tests.ml

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ struct
280280
| Mkfile (path, new_file_name) ->
281281
Res (result unit exn, protect mkfile (p path / new_file_name))
282282

283-
let match_err err path msg = err = (p path) ^ ": " ^ msg
283+
let match_msg err path msg = err = (p path) ^ ": " ^ msg
284+
let match_msgs err path msgs = List.exists (match_msg err path) msgs
284285

285286
let postcond c (fs: filesys) res =
286287
match c, res with
@@ -293,19 +294,20 @@ struct
293294
| Some File -> b = false
294295
| None -> false)
295296
| Error (Sys_error s) ->
296-
(match_err s path "No such file or directory" && not (Model.mem fs path)) ||
297-
(match_err s path "Not a directory" && List.exists (fun pref -> not (path_is_a_dir fs pref)) (path_prefixes path))
297+
(match_msg s path "No such file or directory" && not (Model.mem fs path)) ||
298+
(match_msg s path "Not a directory" &&
299+
List.exists (fun pref -> not (path_is_a_dir fs pref)) (path_prefixes path))
298300
| _ -> false)
299301
| Remove (path, file_name), Res ((Result (Unit,Exn),_), res) ->
300-
let complete_path = (path @ [file_name]) in
302+
let full_path = (path @ [file_name]) in
301303
(match res with
302-
| Ok () -> Model.mem fs complete_path && path_is_a_dir fs path && not (path_is_a_dir fs complete_path)
304+
| Ok () -> Model.mem fs full_path && path_is_a_dir fs path && not (path_is_a_dir fs full_path)
303305
| Error (Sys_error s) ->
304-
(match_err s complete_path "No such file or directory" && not (Model.mem fs complete_path)) ||
305-
(match_err s complete_path "Is a directory" && path_is_a_dir fs complete_path) || (*Linux*)
306-
(match_err s complete_path "Operation not permitted" && path_is_a_dir fs complete_path) || (*macOS*)
307-
(match_err s complete_path "Permission denied" && path_is_a_dir fs complete_path) || (*Win*)
308-
(match_err s complete_path "Not a directory" && not (path_is_a_dir fs path))
306+
(match_msg s full_path "No such file or directory" && not (Model.mem fs full_path)) ||
307+
(match_msgs s full_path ["Is a directory"; (*Linux*)
308+
"Operation not permitted"; (*macOS*)
309+
"Permission denied"(*Win*)] && path_is_a_dir fs full_path) ||
310+
(match_msg s full_path "Not a directory" && not (path_is_a_dir fs path))
309311
| Error _ -> false
310312
)
311313
| Rename (old_path, new_path), Res ((Result (Unit,Exn),_), res) ->
@@ -324,84 +326,69 @@ struct
324326
is_true_prefix new_path old_path || (path_is_a_dir fs new_path && not (path_is_an_empty_dir fs new_path)))
325327
| Error _ -> false)
326328
| Mkdir (path, new_dir_name), Res ((Result (Unit,Exn),_), res) ->
327-
let complete_path = (path @ [new_dir_name]) in
329+
let full_path = (path @ [new_dir_name]) in
328330
(match res with
329331
| Error err ->
330332
(match err with
331333
| Sys_error s ->
332-
(match_err s complete_path "Permission denied") ||
333-
(match_err s complete_path "File exists" && Model.mem fs complete_path) ||
334-
((match_err s complete_path "No such file or directory"
335-
|| match_err s complete_path "Invalid argument") && not (Model.mem fs path)) ||
336-
if Sys.win32 && not (path_is_a_dir fs complete_path)
337-
then match_err s complete_path "No such file or directory"
338-
else match_err s complete_path "Not a directory"
334+
(match_msg s full_path "Permission denied") ||
335+
(match_msg s full_path "File exists" && Model.mem fs full_path) ||
336+
(match_msgs s full_path ["No such file or directory";
337+
"Invalid argument"] && not (Model.mem fs path)) ||
338+
(match_msgs s full_path ["Not a directory";
339+
"No such file or directory"(*win32*)] && not (path_is_a_dir fs full_path))
339340
| _ -> false)
340-
| Ok () -> Model.mem fs path && path_is_a_dir fs path && not (Model.mem fs complete_path))
341+
| Ok () -> Model.mem fs path && path_is_a_dir fs path && not (Model.mem fs full_path))
341342
| Rmdir (path, delete_dir_name), Res ((Result (Unit,Exn),_), res) ->
342-
let complete_path = (path @ [delete_dir_name]) in
343+
let full_path = (path @ [delete_dir_name]) in
343344
(match res with
344345
| Error err ->
345346
(match err with
346347
| Sys_error s ->
347-
(match_err s complete_path "Permission denied") ||
348-
(match_err s complete_path "Directory not empty" && not (path_is_an_empty_dir fs complete_path)) ||
349-
(match_err s complete_path "No such file or directory" && not (Model.mem fs complete_path)) ||
350-
if Sys.win32 && not (path_is_a_dir fs complete_path) (* if not a directory *)
351-
then match_err s complete_path "Invalid argument"
352-
else match_err s complete_path "Not a directory"
348+
(match_msg s full_path "Permission denied") ||
349+
(match_msg s full_path "Directory not empty" && not (path_is_an_empty_dir fs full_path)) ||
350+
(match_msg s full_path "No such file or directory" && not (Model.mem fs full_path)) ||
351+
(match_msgs s full_path ["Not a directory";
352+
"Invalid argument"(*win32*)] && not (path_is_a_dir fs full_path))
353353
| _ -> false)
354354
| Ok () ->
355-
Model.mem fs complete_path && path_is_a_dir fs complete_path && path_is_an_empty_dir fs complete_path)
355+
Model.mem fs full_path && path_is_a_dir fs full_path && path_is_an_empty_dir fs full_path)
356356
| Readdir path, Res ((Result (Array String,Exn),_), res) ->
357357
(match res with
358358
| Error err ->
359359
(match err with
360360
| Sys_error s ->
361-
(match_err s path "Permission denied") ||
362-
(match_err s path "No such file or directory" && not (Model.mem fs path)) ||
363-
if Sys.win32 && not (path_is_a_dir fs path) (* if not a directory *)
364-
then match_err s path "Invalid argument"
365-
else match_err s path "Not a directory"
361+
(match_msg s path "Permission denied") ||
362+
(match_msg s path "No such file or directory" && not (Model.mem fs path)) ||
363+
(match_msgs s path ["Not a directory";
364+
"Invalid argument"(*win32*)] && not (path_is_a_dir fs path))
366365
| _ -> false)
367366
| Ok array_of_subdir ->
368367
(* Temporary work around for mingW, see https://github.com/ocaml/ocaml/issues/11829 *)
369368
if Sys.win32 && not (Model.mem fs path)
370369
then array_of_subdir = [||]
371370
else
372371
(Model.mem fs path && path_is_a_dir fs path &&
373-
(match Model.readdir fs path with
374-
| None -> false
375-
| Some l ->
376-
List.sort String.compare l
377-
= List.sort String.compare (Array.to_list array_of_subdir))))
372+
(match Model.readdir fs path with
373+
| None -> false
374+
| Some l ->
375+
List.sort String.compare l
376+
= List.sort String.compare (Array.to_list array_of_subdir))))
378377
| Mkfile (path, new_file_name), Res ((Result (Unit,Exn),_),res) -> (
379-
let complete_path = path @ [ new_file_name ] in
380-
let match_msg err msg = match_err err complete_path msg in
381-
let match_msgs err = List.exists (match_msg err) in
382-
let msgs_already_exists = ["File exists"; "Permission denied"]
383-
(* Permission denied: seen (sometimes?) on Windows *)
384-
and msgs_non_existent_dir = ["No such file or directory";
385-
"Invalid argument";
386-
"Permission denied"]
387-
(* Invalid argument: seen on macOS
388-
Permission denied: seen on Windows *)
389-
and msg_path_not_dir =
390-
match Sys.os_type with
391-
| "Cygwin"
392-
| "Unix" -> "Not a directory"
393-
| "Win32" -> "No such file or directory"
394-
| v -> failwith ("Sys tests not working with " ^ v)
395-
in
378+
let full_path = path @ [ new_file_name ] in
396379
match res with
397380
| Error err -> (
398381
match err with
399382
| Sys_error s ->
400-
(Model.mem fs complete_path && match_msgs s msgs_already_exists)
401-
|| (not (Model.mem fs path) && match_msgs s msgs_non_existent_dir)
402-
|| (not (path_is_a_dir fs path) && match_msg s msg_path_not_dir)
383+
(match_msgs s full_path ["File exists";
384+
"Permission denied"] && Model.mem fs full_path) ||
385+
(match_msgs s full_path ["No such file or directory";
386+
"Invalid argument";
387+
"Permission denied"] && not (Model.mem fs path)) ||
388+
(match_msgs s full_path ["Not a directory";
389+
"No such file or directory"] && not (path_is_a_dir fs path))
403390
| _ -> false)
404-
| Ok () -> path_is_a_dir fs path && not (Model.mem fs complete_path))
391+
| Ok () -> path_is_a_dir fs path && not (Model.mem fs full_path))
405392
| _,_ -> false
406393
end
407394

0 commit comments

Comments
 (0)