Skip to content

Commit

Permalink
Merge branch 'do-not-care-about-cyclic-values'
Browse files Browse the repository at this point in the history
With this, we adopt the simple (and more common) design where paths
which are cyclic ocaml values are *not* supported and *not*
checked. In particular, some functions may not terminate when
called with such values.

This answers issue #4, which dealt with possible non termination of
functions of the pure library: all legal paths are acyclic ocaml
values, and as our functions on `Path.t` values in the pure library
simply traverse them, there is no risk of infinite loop, hence no
particular precaution to take.

Note that there is no danger of non-termination when dealing with the
file system either:
  - either the functions in async-unix do their job by traversing a
    `Path.t` value, which guarantees termination
  - either they can discover symbolic links whose resolution run
    an infinite computation by just asking the filesystem, which
    considers such links as ill-defined and marks them as broken.
  • Loading branch information
pveber committed Dec 4, 2015
2 parents bd6b6bf + 4897252 commit 5577296
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 275 deletions.
64 changes: 16 additions & 48 deletions app/run_ounit_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,22 @@ let rec random_rel_dir_item ?(no_link = false) ?root ?level () =
match Random.bool () || bottom, Random.float 1. > 0.2 with
| true, b ->
if no_link || b then
Phat.Item.dir (new_name ())
Phat.Dir (new_name ())
else (
Phat.map_any_kind (random_dir_path ~no_link ?root ?level ()) { Phat.map = fun p ->
match Phat.Item.link (new_name ()) p with
| `Ok l -> l
| `Broken _ -> assert false (* cannot happen, since we're generating a new name for each link *)
}
Phat.map_any_kind
(random_dir_path ~no_link ?root ?level ())
{ Phat.map = fun p -> Phat.Link (new_name (), p) }
)
| false, true -> Phat.Item.dot
| false, false -> Phat.Item.dotdot
| false, true -> Phat.Dot
| false, false -> Phat.Dotdot

and random_rel_file_item ?(no_link = false) ?root ?level () =
if no_link || Random.float 1. > 0.1 then
Phat.Item.file (new_name ())
Phat.File (new_name ())
else (
Phat.map_any_kind (random_file_path ~no_link ?root ?level ()) { Phat.map = fun p ->
match Phat.Item.link (new_name ()) p with
| `Ok l -> l
| `Broken _ -> assert false (* cannot happen, since we're generating a new name for each link *)
}
Phat.map_any_kind
(random_file_path ~no_link ?root ?level ())
{ Phat.map = fun p -> Phat.Link (new_name (), p) }
)

and random_dir_path ?no_link ?root ?level () =
Expand Down Expand Up @@ -83,9 +79,7 @@ and random_rel_file_path ?no_link ?root ?level () =
[p] resolves (the name is a tiny bit misleading) *)
and random_path_resolving_to ?root ?level p () =
let link = Phat.map_any_kind p { Phat.map = fun p ->
match Phat.Item.link (new_name ()) p with
| `Ok l -> l
| `Broken _ -> assert false (* cannot happen, since we're generating a new name for each link *)
Phat.Link (new_name (), p)
} in
if Random.float 1. < 0.1 then
`Rel (Phat.Item link)
Expand Down Expand Up @@ -254,15 +248,11 @@ let filesys_exists ctx =
in
assert_failure msg
in
let foo = Phat.(Item (Item.dir (name_exn "foo"))) in
let foo_bar = Phat.(cons foo (Item.dir (name_exn "bar"))) in
let foo_bar_baz = Phat.(cons foo_bar (Item.file (name_exn "baz"))) in
let qux = Phat.(match Item.link (name_exn "qux") foo_bar_baz with
| `Ok l -> Item l
| _ -> assert false
)
in
let broken = Phat.(Item (Item.broken_link (name_exn "broken") ["foo" ; "bar" ; "booz" ])) in
let foo = Phat.(Item (Dir (name_exn "foo"))) in
let foo_bar = Phat.(cons foo (Dir (name_exn "bar"))) in
let foo_bar_baz = Phat.(cons foo_bar (File (name_exn "baz"))) in
let qux = Phat.(Item (Link (name_exn "qux", foo_bar_baz))) in
let broken = Phat.(Item (Broken_link (name_exn "broken", ["foo" ; "bar" ; "booz" ]))) in
create_test_directory tmpdir >>= fun () ->
check foo >>= fun () ->
check foo_bar >>= fun () ->
Expand Down Expand Up @@ -330,27 +320,6 @@ let fold_works_on_test_directory ctx =
| Ok l -> assert_equal ~printer:(List.to_string ~f:ident) expected (List.rev l)
| Error _ -> assert_failure "Fold failed on test directory"

(* let filesys_mkdir_cycles ctx = *)
(* let tmpdir = OUnit2.bracket_tmpdir ctx in *)
(* let tmpdir_path = ok_exn (Phat.abs_dir tmpdir) in *)
(* let rec foo_bar = Phat.(Item (Link (name_exn "bar", baz_qux))) *)
(* and baz_qux = Phat.(Item (Link (name_exn "qux", foo_bar))) *)
(* in *)
(* let p = Phat.concat tmpdir_path foo_bar in *)
(* Phat.mkdir p >>= function *)
(* | Ok () -> *)
(* Phat.exists p >>| fun file_exists -> *)
(* if file_exists <> `Yes then ( *)
(* assert_failure "mkdir failed to create the cyclic path correctly." *)
(* ) *)
(* | Error e -> *)
(* let msg = *)
(* sprintf *)
(* "mkdir failed to create cyclic path: %s" *)
(* (Sexp.to_string_hum (Error.sexp_of_t e)) *)
(* in *)
(* assert_failure msg *)

let suite = "Phat test suite" >::: [
"Name constructor" >:: name_constructor ;
"Sexp serialization" >:: sexp_serialization ;
Expand All @@ -362,7 +331,6 @@ let suite = "Phat test suite" >::: [
"Exists test" >::= filesys_exists ;
"Create dir paths" >::= filesys_mkdir ;
"Fold works on test dir" >::= fold_works_on_test_directory ;
(* "Create dirs with cycles" >::= filesys_mkdir_cycles ; *)
]


Expand Down
Loading

0 comments on commit 5577296

Please sign in to comment.