Skip to content

Commit 1855436

Browse files
committed
Add Sys.remove
1 parent be5d514 commit 1855436

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
@@ -85,6 +86,7 @@ struct
8586
Gen.(oneof [
8687
map (fun path -> File_exists path) (path_gen s);
8788
map (fun path -> Is_directory path) (path_gen s);
89+
map (fun (path,new_dir_name) -> Remove (path, new_dir_name)) (pair_gen s);
8890
map (fun (path,new_dir_name) -> Mkdir (path, new_dir_name)) (pair_gen s);
8991
map (fun (path,delete_dir_name) -> Rmdir (path, delete_dir_name)) (pair_gen s);
9092
map (fun path -> Readdir path) (path_gen s);
@@ -111,6 +113,35 @@ struct
111113

112114
let mem_model fs path = find_opt_model fs path <> None
113115

116+
let rec remove_model fs path file_name =
117+
match fs with
118+
| File -> fs
119+
| Directory d ->
120+
(match path with
121+
| [] ->
122+
(match Map_names.find_opt file_name d.fs_map with
123+
| None
124+
| Some (Directory _) -> fs
125+
| Some File -> Directory { fs_map = Map_names.remove file_name d.fs_map }
126+
)
127+
| dir::dirs ->
128+
Directory
129+
{ fs_map = Map_names.update dir (function
130+
| None -> None
131+
| Some File -> Some File
132+
| Some (Directory _ as d') -> Some (remove_model d' dirs file_name)) d.fs_map
133+
}
134+
(*
135+
(match Map_names.find_opt dir d.fs_map with
136+
| None
137+
| Some File -> fs
138+
| Some (Directory _ as d') ->
139+
let fs' = remove_model d' dirs file_name in
140+
Directory { fs_map = Map_names.update dir d.fs_map }
141+
)
142+
*)
143+
)
144+
114145
let rec mkdir_model fs path new_dir_name =
115146
match fs with
116147
| File -> fs
@@ -182,6 +213,7 @@ struct
182213
if mem_model fs (path @ [new_dir_name])
183214
then fs
184215
else mkdir_model fs path new_dir_name
216+
| Remove (path, file_name) -> remove_model fs path file_name
185217
| Is_directory _path -> fs
186218
| Rmdir (path,delete_dir_name) ->
187219
if mem_model fs (path @ [delete_dir_name])
@@ -216,6 +248,7 @@ struct
216248
match c with
217249
| File_exists path -> Res (bool, Sys.file_exists (p path))
218250
| Is_directory path -> Res (result bool exn, protect Sys.is_directory (p path))
251+
| Remove (path, file_name) -> Res (result unit exn, protect Sys.remove ((p path) / file_name))
219252
| Mkdir (path, new_dir_name) ->
220253
Res (result unit exn, protect (Sys.mkdir ((p path) / new_dir_name)) 0o755)
221254
| Rmdir (path, delete_dir_name) ->
@@ -251,6 +284,16 @@ struct
251284
(s = (p path) ^ ": No such file or directory" && find_opt_model fs path = None) ||
252285
(s = p path ^ ": Not a directory" && List.exists (fun pref -> Some File = find_opt_model fs pref) (path_prefixes path))
253286
| _ -> false)
287+
| Remove (path, file_name), Res ((Result (Unit,Exn),_), res) ->
288+
let complete_path = (path @ [file_name]) in
289+
(match res with
290+
| Ok () -> mem_model fs complete_path && path_is_a_dir fs path && not (path_is_a_dir fs complete_path)
291+
| Error (Sys_error s) ->
292+
(s = (p complete_path) ^ ": No such file or directory" && find_opt_model fs complete_path = None) ||
293+
(s = (p complete_path) ^ ": Is a directory" && path_is_a_dir fs complete_path) ||
294+
(s = (p complete_path) ^ ": Not a directory" && not (path_is_a_dir fs path))
295+
| Error _ -> false
296+
)
254297
| Mkdir (path, new_dir_name), Res ((Result (Unit,Exn),_), res) ->
255298
let complete_path = (path @ [new_dir_name]) in
256299
(match res with

0 commit comments

Comments
 (0)