Skip to content

Commit

Permalink
Add Sys.remove
Browse files Browse the repository at this point in the history
  • Loading branch information
jmid committed May 8, 2023
1 parent 7f17f99 commit 92aa1de
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/sys/stm_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct
type cmd =
| File_exists of path
| Is_directory of path
| Remove of path * string
| Mkdir of path * string
| Rmdir of path * string
| Readdir of path
Expand Down Expand Up @@ -74,6 +75,7 @@ struct
Gen.(oneof [
map (fun path -> File_exists path) (path_gen s);
map (fun path -> Is_directory path) (path_gen s);
map (fun (path,new_dir_name) -> Remove (path, new_dir_name)) (pair_gen s);
map (fun (path,new_dir_name) -> Mkdir (path, new_dir_name)) (pair_gen s);
map (fun (path,delete_dir_name) -> Rmdir (path, delete_dir_name)) (pair_gen s);
map (fun path -> Readdir path) (path_gen s);
Expand All @@ -100,6 +102,35 @@ struct

let mem_model fs path = find_opt_model fs path <> None

let rec remove_model fs path file_name =
match fs with
| File -> fs
| Directory d ->
(match path with
| [] ->
(match Map_names.find_opt file_name d.fs_map with
| None
| Some (Directory _) -> fs
| Some File -> Directory { fs_map = Map_names.remove file_name d.fs_map }
)
| dir::dirs ->
Directory
{ fs_map = Map_names.update dir (function
| None -> None
| Some File -> Some File
| Some (Directory _ as d') -> Some (remove_model d' dirs file_name)) d.fs_map
}
(*
(match Map_names.find_opt dir d.fs_map with
| None
| Some File -> fs
| Some (Directory _ as d') ->
let fs' = remove_model d' dirs file_name in
Directory { fs_map = Map_names.update dir d.fs_map }
)
*)
)

let rec mkdir_model fs path new_dir_name =
match fs with
| File -> fs
Expand Down Expand Up @@ -171,6 +202,7 @@ struct
if mem_model fs (path @ [new_dir_name])
then fs
else mkdir_model fs path new_dir_name
| Remove (path, file_name) -> remove_model fs path file_name
| Is_directory _path -> fs
| Rmdir (path,delete_dir_name) ->
if mem_model fs (path @ [delete_dir_name])
Expand Down Expand Up @@ -205,6 +237,7 @@ struct
match c with
| File_exists path -> Res (bool, Sys.file_exists (p path))
| Is_directory path -> Res (result bool exn, protect Sys.is_directory (p path))
| Remove (path, file_name) -> Res (result unit exn, protect Sys.remove ((p path) / file_name))
| Mkdir (path, new_dir_name) ->
Res (result unit exn, protect (Sys.mkdir ((p path) / new_dir_name)) 0o755)
| Rmdir (path, delete_dir_name) ->
Expand Down Expand Up @@ -240,6 +273,16 @@ struct
(s = (p path) ^ ": No such file or directory" && find_opt_model fs path = None) ||
(s = p path ^ ": Not a directory" && List.exists (fun pref -> Some File = find_opt_model fs pref) (path_prefixes path))
| _ -> false)
| Remove (path, file_name), Res ((Result (Unit,Exn),_), res) ->
let complete_path = (path @ [file_name]) in
(match res with
| Ok () -> mem_model fs complete_path && path_is_a_dir fs path && not (path_is_a_dir fs complete_path)
| Error (Sys_error s) ->
(s = (p complete_path) ^ ": No such file or directory" && find_opt_model fs complete_path = None) ||
(s = (p complete_path) ^ ": Is a directory" && path_is_a_dir fs complete_path) ||
(s = (p complete_path) ^ ": Not a directory" && not (path_is_a_dir fs path))
| Error _ -> false
)
| Mkdir (path, new_dir_name), Res ((Result (Unit,Exn),_), res) ->
let complete_path = (path @ [new_dir_name]) in
(match res with
Expand Down

0 comments on commit 92aa1de

Please sign in to comment.