t_file.ml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. open Seppo_lib
  27. open Alcotest
  28. let set_up = "setup", `Quick, (fun () ->
  29. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  30. Unix.chdir "../../../test/"
  31. )
  32. (*
  33. let cp' ?(buf = 16 * 0x400 |> Bytes.create) src oc =
  34. let ic = open_in_gen [ Open_rdonly; Open_binary ] 0 src in
  35. ic |> copy_channel ~buf oc;
  36. close_in ic
  37. *)
  38. let tc_www_base = "tc_www_base", `Quick, (fun () ->
  39. (match
  40. "/a/b/c"
  41. |> String.split_on_char '/'
  42. |> List.rev
  43. |> List.cons "."
  44. with
  45. | [ d; c; b; a; v ] ->
  46. v |> Assrt.equals_string __LOC__ "";
  47. a |> Assrt.equals_string __LOC__ "a";
  48. b |> Assrt.equals_string __LOC__ "b";
  49. c |> Assrt.equals_string __LOC__ "c";
  50. d |> Assrt.equals_string __LOC__ ".";
  51. | _ ->
  52. "woring" |> Assrt.equals_string __LOC__ "");
  53. (match
  54. "/a/b/c"
  55. |> String.split_on_char '/'
  56. |> List.rev
  57. |> File.find_path_tail (fun s -> Ok (s |> String.equal "/b/c"))
  58. with
  59. | Ok s -> s |> Assrt.equals_string __LOC__ "/b/c"
  60. | Error e -> e |> Assrt.equals_string __LOC__ "");
  61. let wefi u =
  62. let wkwf = ".well-known/webfinger/" in
  63. let i = u |> Uri.path |> String.split_on_char '/' |> List.length in
  64. ((List.init (i-2) (fun _ -> "../") |> String.concat "") ^ wkwf)
  65. in
  66. "https://ursi@demo.mro.name/" |> Uri.of_string
  67. |> wefi |> Assrt.equals_string __LOC__ ".well-known/webfinger/";
  68. "https://ursi@demo.mro.name/seppo/" |> Uri.of_string
  69. |> wefi |> Assrt.equals_string __LOC__ "../.well-known/webfinger/"
  70. )
  71. module Path = struct
  72. let tc_climb = "tc_climb", `Quick, (fun () ->
  73. let u = Uri.make ~scheme:"https" ~userinfo:"foo" ~host:"example.com" ~path:"/a/b/c/" () in
  74. u |> Uri.path |> Assrt.equals_string __LOC__ "/a/b/c/";
  75. let i = u |> Uri.path |> String.split_on_char '/' |> List.length in
  76. List.init (i-2) (fun _ -> "..") |> String.concat "/"
  77. |> Assrt.equals_string __LOC__ "../../..";
  78. "z" |> Assrt.equals_string __LOC__ "z"
  79. )
  80. let tc_hd_tl = "tc_hd_tl", `Quick, (fun () ->
  81. "/foo/bar/baz"
  82. |> File.Path.hd '/'
  83. |> Option.get |> Assrt.equals_string __LOC__ "";
  84. let t0 = "/foo/bar/baz" |> File.Path.tl '/' |> Option.get in
  85. t0 |> Assrt.equals_string __LOC__ "foo/bar/baz";
  86. t0 |> File.Path.hd '/'
  87. |> Option.get |> Assrt.equals_string __LOC__ "foo";
  88. let t1 = t0 |> File.Path.tl '/' |> Option.get in
  89. t1 |> Assrt.equals_string __LOC__ "bar/baz";
  90. let t2 = t1 |> File.Path.tl '/' |> Option.get in
  91. t2 |> Assrt.equals_string __LOC__ "baz";
  92. assert (t2 |> File.Path.tl '/' |> Option.is_none);
  93. assert true
  94. )
  95. end
  96. let tc_out_channel_atomic = "tc_out_channel_atomic", `Quick, (fun () ->
  97. "tmp/a.txt" |> File.out_channel_append (fun oc -> output_string oc "huhu");
  98. "tmp/b.txt" |> File.out_channel_replace (fun oc -> output_string oc "huhu");
  99. "tmp/c.txt" |> File.out_channel_replace ~mode:[Open_binary;Open_creat;Open_excl;Open_trunc;Open_wronly] (fun oc -> output_string oc "huhu");
  100. "" |> Assrt.equals_string __LOC__ ""
  101. )
  102. let () =
  103. run
  104. "seppo_suite" [
  105. __FILE__ , [
  106. set_up;
  107. tc_www_base;
  108. Path.tc_climb;
  109. Path.tc_hd_tl;
  110. tc_out_channel_atomic;
  111. ]
  112. ];
  113. assert true