file_test.ml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. let test_www_base () =
  28. (match
  29. "/a/b/c"
  30. |> String.split_on_char '/'
  31. |> List.rev
  32. |> List.cons "."
  33. with
  34. | [ d; c; b; a; v ] ->
  35. v |> Assrt.equals_string __LOC__ "";
  36. a |> Assrt.equals_string __LOC__ "a";
  37. b |> Assrt.equals_string __LOC__ "b";
  38. c |> Assrt.equals_string __LOC__ "c";
  39. d |> Assrt.equals_string __LOC__ ".";
  40. | _ ->
  41. "woring" |> Assrt.equals_string __LOC__ "");
  42. (match
  43. "/a/b/c"
  44. |> String.split_on_char '/'
  45. |> List.rev
  46. |> File.find_path_tail (fun s -> Ok (s |> String.equal "/b/c"))
  47. with
  48. | Ok s -> s |> Assrt.equals_string __LOC__ "/b/c"
  49. | Error e -> e |> Assrt.equals_string __LOC__ "");
  50. let wefi u =
  51. let wkwf = ".well-known/webfinger/" in
  52. let i = u |> Uri.path |> String.split_on_char '/' |> List.length in
  53. ((List.init (i-2) (fun _ -> "../") |> String.concat "") ^ wkwf)
  54. in
  55. "https://ursi@demo.mro.name/" |> Uri.of_string
  56. |> wefi |> Assrt.equals_string __LOC__ ".well-known/webfinger/";
  57. "https://ursi@demo.mro.name/seppo/" |> Uri.of_string
  58. |> wefi |> Assrt.equals_string __LOC__ "../.well-known/webfinger/"
  59. let test_path_climb () =
  60. let u = Uri.make ~scheme:"https" ~userinfo:"foo" ~host:"example.com" ~path:"/a/b/c/" () in
  61. u |> Uri.path |> Assrt.equals_string __LOC__ "/a/b/c/";
  62. let i = u |> Uri.path |> String.split_on_char '/' |> List.length in
  63. List.init (i-2) (fun _ -> "..") |> String.concat "/"
  64. |> Assrt.equals_string __LOC__ "../../..";
  65. "z" |> Assrt.equals_string __LOC__ "z"
  66. let test_path_hd_tl () =
  67. "/foo/bar/baz"
  68. |> File.Path.hd '/'
  69. |> Option.get |> Assrt.equals_string __LOC__ "";
  70. let t0 = "/foo/bar/baz" |> File.Path.tl '/' |> Option.get in
  71. t0 |> Assrt.equals_string __LOC__ "foo/bar/baz";
  72. t0 |> File.Path.hd '/'
  73. |> Option.get |> Assrt.equals_string __LOC__ "foo";
  74. let t1 = t0 |> File.Path.tl '/' |> Option.get in
  75. t1 |> Assrt.equals_string __LOC__ "bar/baz";
  76. let t2 = t1 |> File.Path.tl '/' |> Option.get in
  77. t2 |> Assrt.equals_string __LOC__ "baz";
  78. assert (t2 |> File.Path.tl '/' |> Option.is_none);
  79. assert true
  80. let test_out_channel_atomic () =
  81. File.out_channel ~tmp:None "tmp/a.txt" (fun oc -> output_string oc "huhu");
  82. File.out_channel "tmp/b.txt" (fun oc -> output_string oc "huhu");
  83. File.out_channel ~mode:[Open_binary;Open_creat;Open_trunc;Open_wronly] "tmp/c.txt" (fun oc -> output_string oc "huhu");
  84. "" |> Assrt.equals_string __LOC__ ""
  85. let () =
  86. Unix.chdir "../../../test/";
  87. test_www_base ();
  88. test_path_climb ();
  89. test_path_hd_tl ();
  90. test_out_channel_atomic ();
  91. assert true