(* Copyright (c) 2024-2026 Thomas Gazagnaire 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 normalizes ?(autocrlf = Git.Gitattributes.False) ?(content = "hello\n") attrs path = Git.Gitattributes.normalizes_eol (Git.Gitattributes.of_file_content ~base:"" attrs) ~autocrlf ~path ~content let case name expected attrs path = Alcotest.test_case name `Quick (fun () -> Alcotest.(check bool) (path ^ " vs " ^ attrs) expected (normalizes attrs path)) (* Explicit [text] (and [eol=...], which implies text) normalises CRLF to LF. *) let text_tests = [ case "text set" true "*.cmd text\n" "x.cmd"; case "eol=crlf implies text" true "*.cmd text eol=crlf\n" "x.cmd"; case "eol=lf implies text" true "*.sh eol=lf\n" "x.sh"; case "-text never normalises" false "*.bin -text\n" "x.bin"; case "binary never normalises" false "*.png binary\n" "x.png"; case "unmatched path unspecified" false "*.cmd text\n" "x.txt"; ] (* [text=auto] normalises only content without NUL bytes (git's text test). *) let auto_tests = [ Alcotest.test_case "auto text content" `Quick (fun () -> Alcotest.(check bool) "auto on text" true (normalizes ~content:"plain text\n" "* text=auto\n" "f.txt")); Alcotest.test_case "auto binary content" `Quick (fun () -> Alcotest.(check bool) "auto on binary" false (normalizes ~content:"a\000b" "* text=auto\n" "f.bin")); ] (* With no attribute, [core.autocrlf] decides, again gated on text content. *) let autocrlf_tests = [ Alcotest.test_case "autocrlf true normalises text" `Quick (fun () -> Alcotest.(check bool) "autocrlf=true" true (normalizes ~autocrlf:Git.Gitattributes.True ~content:"text\n" "" "f")); Alcotest.test_case "autocrlf input normalises text" `Quick (fun () -> Alcotest.(check bool) "autocrlf=input" true (normalizes ~autocrlf:Git.Gitattributes.Input ~content:"text\n" "" "f")); Alcotest.test_case "autocrlf true skips binary" `Quick (fun () -> Alcotest.(check bool) "autocrlf binary" false (normalizes ~autocrlf:Git.Gitattributes.True ~content:"a\000b" "" "f")); Alcotest.test_case "autocrlf false never normalises" `Quick (fun () -> Alcotest.(check bool) "autocrlf=false" false (normalizes ~autocrlf:Git.Gitattributes.False ~content:"text\n" "" "f")); ] (* The last matching rule wins, like git. *) let override_tests = [ case "later -text overrides text" false "*.x text\n*.x -text\n" "a.x" ] (* The [merge] attribute (gitattributes(5), "Performing a three-way merge"). *) let driver = Alcotest.testable (fun ppf (d : Git.Gitattributes.merge_driver) -> match d with | Text_merge -> Fmt.string ppf "text" | Binary_merge -> Fmt.string ppf "binary" | Union_merge -> Fmt.string ppf "union" | Driver n -> Fmt.pf ppf "driver %s" n) ( = ) let merge_case name expected attrs path = Alcotest.test_case name `Quick (fun () -> Alcotest.check driver (path ^ " vs " ^ attrs) expected (Git.Gitattributes.merge_driver (Git.Gitattributes.of_file_content ~base:"" attrs) ~path)) let merge_tests = Git.Gitattributes. [ merge_case "merge unspecified" Text_merge "*.c text\n" "a.c"; merge_case "merge set" Text_merge "*.c -merge\n*.c merge\n" "a.c"; merge_case "merge=text" Text_merge "*.c merge=text\n" "a.c"; merge_case "-merge" Binary_merge "*.c -merge\n" "a.c"; merge_case "merge=binary" Binary_merge "*.c merge=binary\n" "a.c"; merge_case "binary macro" Binary_merge "*.png binary\n" "a.png"; merge_case "merge=union" Union_merge "*.log merge=union\n" "a.log"; merge_case "named driver" (Driver "ours") "*.c merge=ours\n" "a.c"; merge_case "!merge unspecifies" Text_merge "*.c merge=union\n*.c !merge\n" "a.c"; merge_case "last rule wins" Union_merge "*.c binary\n*.c merge=union\n" "a.c"; merge_case "unmatched path" Text_merge "*.log merge=union\n" "a.c"; ] let suite = ( "gitattributes", text_tests @ auto_tests @ autocrlf_tests @ override_tests @ merge_tests )