Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667(* Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. *)
let compile p = match Git.Pathspec.compile p with | Some t -> t | None -> Alcotest.failf "pattern %S failed to compile" p
let matches ?(base = "") pat path = Git.Pathspec.matches (compile pat) ~base ~path
let case name expected ?base pat path = Alcotest.test_case name `Quick (fun () -> Alcotest.(check bool) (Fmt.str "%S vs %S" pat path) expected (matches ?base pat path))
(* An empty (or all-slash) pattern compiles to nothing. *)let compile_tests = [ Alcotest.test_case "empty is None" `Quick (fun () -> Alcotest.(check bool) "empty" true (Git.Pathspec.compile "" = None)); Alcotest.test_case "bare slash is None" `Quick (fun () -> Alcotest.(check bool) "slash" true (Git.Pathspec.compile "/" = None)); ]
(* No internal slash: matches the basename at any depth. *)let basename_tests = [ case "basename root" true "*.ml" "a.ml"; case "basename nested" true "*.ml" "lib/a.ml"; case "star stops at slash" false "a*c" "a/c"; ]
(* A leading or internal slash anchors the match to [base]. *)let anchored_tests = [ case "leading slash root only" true "/a.ml" "a.ml"; case "leading slash not nested" false "/a.ml" "x/a.ml"; case "internal slash anchored" true "lib/a.ml" "lib/a.ml"; case "internal slash not floating" false "lib/a.ml" "x/lib/a.ml"; ]
(* A leading [**/] matches at any depth, including zero. *)let globstar_tests = [ case "globstar zero dirs" true "**/a.ml" "a.ml"; case "globstar n dirs" true "**/a.ml" "x/y/a.ml"; case "globstar subpath" true "**/d/a.ml" "x/d/a.ml"; ]
(* [base] confines a pattern to the subtree it was declared in. *)let base_tests = [ case "under base matches" ~base:"sub" true "*.ml" "sub/a.ml"; case "outside base no match" ~base:"sub" false "*.ml" "other/a.ml"; case "anchored relative to base" ~base:"sub" true "d/a.ml" "sub/d/a.ml"; ]
let suite = ( "pathspec", compile_tests @ basename_tests @ anchored_tests @ globstar_tests @ base_tests )