File: sys_fs.ml

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (277 lines) | stat: -rw-r--r-- 7,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2021 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open Util

let%expect_test "readdir fails on non-existing directory and existing files" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  let oc = open_out "aaa/bbb" in
  close_out oc;
  (try ignore(Sys.readdir "aaa/bb") with
  | Sys_error _ -> ()
  | e -> print_endline (Printexc.to_string e));
  (try ignore(Sys.readdir "aaa/bbb") with
  | Sys_error _ -> ()
     | e -> print_endline (Printexc.to_string e));
  Array.iter print_endline (Sys.readdir "aaa");
  Sys.remove "aaa/bbb";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    bbb
    bbb
    |}]

let%expect_test "rmdir work on empty directory only" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  let l = Sys.readdir "aaa" |> Array.to_list in
  List.iter print_endline l;
  (match Sys.rmdir "aaa" with
  | exception _ -> print_endline "EXPECTED ERROR"
  | _ -> print_endline "BUG");
  Sys.rmdir "aaa/bbb";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    bbb
    EXPECTED ERROR
    bbb
    EXPECTED ERROR
    |}]

let%expect_test "Rename a directory" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  Sys.mkdir "aaa/bbb/ccc" 0o777;
  let oc = open_out "aaa/bbb/ccc/ddd" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  Sys.rename "aaa/bbb" "aaa/bbb2";
  let ic = open_in "aaa/bbb2/ccc/ddd" in
  let line = input_line ic in
  close_in ic;
  Printf.printf "new file contents: %s\n%!" line;
  Sys.remove "aaa/bbb2/ccc/ddd";
  Sys.rmdir "aaa/bbb2/ccc";
  Sys.rmdir "aaa/bbb2";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect
    {|
    new file contents: Hello world
    new file contents: Hello world
    |}]

let%expect_test "Rename a directory over another (empty) directory" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  Sys.mkdir "aaa/bbb/ccc" 0o777;
  let oc = open_out "aaa/bbb/ccc/ddd" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  Sys.mkdir "aaa/bbb2" 0o777;
  Sys.rename "aaa/bbb" "aaa/bbb2";
  let ic = open_in "aaa/bbb2/ccc/ddd" in
  let line = input_line ic in
  close_in ic;
  Printf.printf "new file contents: %s\n%!" line;
  Sys.remove "aaa/bbb2/ccc/ddd";
  Sys.rmdir "aaa/bbb2/ccc";
  Sys.rmdir "aaa/bbb2";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect
    {|
    new file contents: Hello world
    new file contents: Hello world
    |}]

let%expect_test "Can't rename a directory over another non-empty directory" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  Sys.mkdir "aaa/bbb/ccc" 0o777;
  let oc = open_out "aaa/bbb/ccc/ddd" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  Sys.mkdir "aaa/bbb2" 0o777;
  let oc = open_out "aaa/bbb2/ccc" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  (match Sys.rename "aaa/bbb" "aaa/bbb2" with
  | exception Sys_error _ -> print_endline "EXPECTED ERROR"
  | _ -> failwith "BUG: rename should have failed");
  Sys.remove "aaa/bbb/ccc/ddd";
  Sys.rmdir "aaa/bbb/ccc";
  Sys.rmdir "aaa/bbb";
  Sys.remove "aaa/bbb2/ccc";
  Sys.rmdir "aaa/bbb2";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    EXPECTED ERROR
    EXPECTED ERROR
    |}]

let%expect_test "Rename a file to a pre-existing file" =
  compile_and_run
    {|
let f () =
  let mk f content =
    let oc = open_out f in
    Printf.fprintf oc "%s\n" content;
    close_out oc
  in
  mk "aaa" "aaa";
  mk "bbb" "bbb";
  Sys.rename "aaa" "bbb";
  let ic = open_in "bbb" in
  let line = input_line ic in
  close_in ic;
  Printf.printf "contents of 'bbb': %s\n%!" line;
  Sys.remove "bbb";
  (match Sys.remove "aaa" with
  | exception _ -> print_endline "EXPECTED ERROR"
  | _ -> print_endline "BUG")
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect
    {|
    contents of 'bbb': aaa
    EXPECTED ERROR
    contents of 'bbb': aaa
    EXPECTED ERROR
    |}]

let%expect_test "Can't rename a directory over a file" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  Sys.mkdir "aaa/bbb/ccc" 0o777;
  let oc = open_out "aaa/bbb/ccc/ddd" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  let oc = open_out "aaa/bbb2" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  (match Sys.rename "aaa/bbb" "aaa/bbb2"
   with
   | () -> failwith "BUG: rename should have failed"
   | exception Sys_error _ -> print_endline "EXPECTED ERROR");
  Sys.remove "aaa/bbb/ccc/ddd";
  Sys.rmdir "aaa/bbb/ccc";
  Sys.rmdir "aaa/bbb";
  Sys.remove "aaa/bbb2";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    EXPECTED ERROR
    EXPECTED ERROR
    |}]

let%expect_test "Can't rename a file over a directory" =
  compile_and_run
    {|
let f () =
  Sys.mkdir "aaa" 0o777;
  Sys.mkdir "aaa/bbb" 0o777;
  Sys.mkdir "aaa/bbb/ccc" 0o777;
  let oc = open_out "aaa/bbb/ccc/ddd" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  let oc = open_out "aaa/bbb2" in
  Printf.fprintf oc "Hello world\n";
  close_out oc;
  (match Sys.rename "aaa/bbb2" "aaa/bbb" with
   | exception Sys_error _ -> print_endline "EXPECTED ERROR"
   | _ -> failwith "BUG: rename should have failed");
  Sys.remove "aaa/bbb/ccc/ddd";
  Sys.rmdir "aaa/bbb/ccc";
  Sys.rmdir "aaa/bbb";
  Sys.remove "aaa/bbb2";
  Sys.rmdir "aaa"
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    EXPECTED ERROR
    EXPECTED ERROR
    |}]

let%expect_test "mkdir in non-existing directory" =
  compile_and_run
    {|
let f () =
  (match Sys.mkdir "not/exists" 0o777 with
  | exception Sys_error path -> print_endline "EXPECTED ERROR"
  | exception err -> print_endline (Printexc.to_string err)
  | _ -> print_endline "BUG")
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    EXPECTED ERROR
    EXPECTED ERROR
    |}]

let%expect_test "rmdir non-existing directory" =
  compile_and_run
    {|
let f () =
  (match Sys.rmdir "not/exists" with
  | exception Sys_error path -> print_endline "EXPECTED ERROR"
  | exception err -> print_endline (Printexc.to_string err)
  | _ -> print_endline "BUG")
in
f (); Sys.chdir "/static"; f ()
  |};
  [%expect {|
    EXPECTED ERROR
    EXPECTED ERROR
    |}]