summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-unix/win-symlink/test.ml
blob: 72426a23df3f30a2ab3ef7e89882314137416b81 (plain)
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
(* TEST
 include unix;
 libwin32unix;
 has_symlink;
 {
   bytecode;
 }{
   native;
 }
*)

let link1 = "link1"
let link2 = "link2"
let link3 = "link3"
let link_dir = "link_directory"
let dir = "directory"
let did_raise = ref false

let link_exists s =
  try (Unix.lstat s).Unix.st_kind = Unix.S_LNK with _ -> false

let directory_exists s =
  try (Unix.lstat s).Unix.st_kind = Unix.S_DIR with _ -> false

let main () =
  close_out (open_out "test.txt");
  if link_exists link1 then Sys.remove link1;
  if link_exists link2 then Sys.remove link2;
  Unix.symlink ~to_dir:false ".\\test.txt" link1;
  assert ((Unix.stat link1).Unix.st_kind = Unix.S_REG);
  print_endline "Unix.symlink works with backwards slashes";
  Unix.symlink ~to_dir:false "./test.txt" link2;
  assert ((Unix.stat link2).Unix.st_kind = Unix.S_REG);
  print_endline "Unix.symlink works with forward slashes";

  did_raise := false;
  if not (directory_exists dir) then
    Unix.mkdir dir 0o644;
  begin try Unix.unlink dir with
  | Unix.Unix_error((EISDIR (* Linux *) | EPERM (* POSIX *) | EACCES (* Windows *)), _, _) ->
    did_raise := true end;
  assert (!did_raise);
  assert (directory_exists dir);
  print_endline "Unix.unlink cannot delete directories";

  did_raise := false;
  if not (directory_exists dir) then
    Unix.mkdir dir 0o644;
  begin try Sys.remove dir with Sys_error _ -> did_raise := true end;
  assert (!did_raise);
  assert (directory_exists dir);
  print_endline "Sys.remove cannot delete directories";

  if not (directory_exists dir) then
    Unix.mkdir dir 0o644;
  if not (link_exists link_dir) then
    Unix.symlink ~to_dir:true dir link_dir;
  Unix.unlink link_dir;
  print_endline "Unix.unlink can delete symlinks to directories";

  if not (link_exists link3) then
    Unix.symlink ~to_dir:false "test.txt" link3;
  Unix.unlink link3;
  print_endline "Unix.unlink can delete symlinks to files";

  if not (directory_exists dir) then
    Unix.mkdir dir 0o644;
  if not (link_exists link_dir) then
    Unix.symlink ~to_dir:true dir link_dir;
  Sys.remove link_dir;
  print_endline "Sys.remove can delete symlinks to directories";

  if not (link_exists link3) then
    Unix.symlink ~to_dir:false "test.txt" link3;
  Sys.remove link3;
  print_endline "Sys.remove can delete symlinks to files"

let () =
  Unix.handle_unix_error main ()