Skip to content

Commit 4d858ff

Browse files
committed
Add Sys.remove
1 parent 173a8a8 commit 4d858ff

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/sys/stm_tests.ml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct
88
type cmd =
99
| File_exists of path
1010
| Is_directory of path
11+
| Remove of path * string
1112
| Mkdir of path * string
1213
| Rmdir of path * string
1314
| Readdir of path
@@ -74,6 +75,7 @@ struct
7475
Gen.(oneof [
7576
map (fun path -> File_exists path) (path_gen s);
7677
map (fun path -> Is_directory path) (path_gen s);
78+
map (fun (path,new_dir_name) -> Remove (path, new_dir_name)) (pair_gen s);
7779
map (fun (path,new_dir_name) -> Mkdir (path, new_dir_name)) (pair_gen s);
7880
map (fun (path,delete_dir_name) -> Rmdir (path, delete_dir_name)) (pair_gen s);
7981
map (fun path -> Readdir path) (path_gen s);
@@ -100,6 +102,35 @@ struct
100102

101103
let mem_model fs path = find_opt_model fs path <> None
102104

105+
let rec remove_model fs path file_name =
106+
match fs with
107+
| File -> fs
108+
| Directory d ->
109+
(match path with
110+
| [] ->
111+
(match Map_names.find_opt file_name d.fs_map with
112+
| None
113+
| Some (Directory _) -> fs
114+
| Some File -> Directory { fs_map = Map_names.remove file_name d.fs_map }
115+
)
116+
| dir::dirs ->
117+
Directory
118+
{ fs_map = Map_names.update dir (function
119+
| None -> None
120+
| Some File -> Some File
121+
| Some (Directory _ as d') -> Some (remove_model d' dirs file_name)) d.fs_map
122+
}
123+
(*
124+
(match Map_names.find_opt dir d.fs_map with
125+
| None
126+
| Some File -> fs
127+
| Some (Directory _ as d') ->
128+
let fs' = remove_model d' dirs file_name in
129+
Directory { fs_map = Map_names.update dir d.fs_map }
130+
)
131+
*)
132+
)
133+
103134
let rec mkdir_model fs path new_dir_name =
104135
match fs with
105136
| File -> fs
@@ -171,6 +202,7 @@ struct
171202
if mem_model fs (path @ [new_dir_name])
172203
then fs
173204
else mkdir_model fs path new_dir_name
205+
| Remove (path, file_name) -> remove_model fs path file_name
174206
| Is_directory _path -> fs
175207
| Rmdir (path,delete_dir_name) ->
176208
if mem_model fs (path @ [delete_dir_name])
@@ -204,6 +236,7 @@ struct
204236
match c with
205237
| File_exists path -> Res (bool, Sys.file_exists (p path))
206238
| Is_directory path -> Res (result bool exn, protect Sys.is_directory (p path))
239+
| Remove (path, file_name) -> Res (result unit exn, protect Sys.remove ((p path) / file_name))
207240
| Mkdir (path, new_dir_name) ->
208241
Res (result unit exn, protect (Sys.mkdir ((p path) / new_dir_name)) 0o755)
209242
| Rmdir (path, delete_dir_name) ->
@@ -239,6 +272,16 @@ struct
239272
(s = (p path) ^ ": No such file or directory" && find_opt_model fs path = None) ||
240273
(s = p path ^ ": Not a directory" && List.exists (fun pref -> Some File = find_opt_model fs pref) (path_prefixes path))
241274
| _ -> false)
275+
| Remove (path, file_name), Res ((Result (Unit,Exn),_), res) ->
276+
let complete_path = (path @ [file_name]) in
277+
(match res with
278+
| Ok () -> mem_model fs complete_path && path_is_a_dir fs path && not (path_is_a_dir fs complete_path)
279+
| Error (Sys_error s) ->
280+
(s = (p complete_path) ^ ": No such file or directory" && find_opt_model fs complete_path = None) ||
281+
(s = (p complete_path) ^ ": Is a directory" && path_is_a_dir fs complete_path) ||
282+
(s = (p complete_path) ^ ": Not a directory" && not (path_is_a_dir fs path))
283+
| Error _ -> false
284+
)
242285
| Mkdir (path, new_dir_name), Res ((Result (Unit,Exn),_), res) ->
243286
let complete_path = (path @ [new_dir_name]) in
244287
(match res with

0 commit comments

Comments
 (0)