Something went wrong. Try again.
A local Git merge queue with content-bound gate verdicts
Something went wrong. Try again.
1.8 kB · 77 lines
OCaml
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778(* POSIX.1-2017, XCU 2.2.2: "Enclosing characters in single-quotes shall preserve the literal value of each character within the single-quotes. A single-quote cannot occur within single-quotes." A word printed for the caller to run is read back by the shell as the string it stands for, and the oracle is /bin/sh itself. *)
open Merge_queue
(* What /bin/sh prints for printf '%s' WORD. *)let read_back word = let ic = Unix.open_process_args_in "/bin/sh" [| "/bin/sh"; "-c"; "printf '%s' " ^ word |] in let out = In_channel.input_all ic in match Unix.close_process_in ic with | WEXITED 0 -> out | _ -> Alcotest.failf "sh refused %s" word
let awkward = [ ""; "a b"; "it's"; "''"; "$HOME"; "${x}"; "`id`"; "$(id)"; "a&b"; "a;b"; "a|b"; "a>b"; "~/x"; "x=y"; "*"; "?"; "[a]"; "a\nb"; "a\tb"; "\\"; "\""; "#x"; "!x"; ]
let plain = [ "mq.slots"; "/src/.git/config"; "refs/mq/dropped/fix-x.3"; "a+b@c,d:e_f" ]
let test_read_back () = List.iter (fun s -> Alcotest.(check string) (Fmt.str "%S read back" s) s (read_back (Shell.word s))) (plain @ awkward)
(* A word that needs no quote is printed as it is, so the command reads as the caller would type it; any other is quoted. *)let test_plain () = List.iter (fun s -> Alcotest.(check string) (Fmt.str "%S as it is" s) s (Shell.word s)) plain; List.iter (fun s -> Alcotest.(check bool) (Fmt.str "%S quoted" s) true (String.starts_with ~prefix:"'" (Shell.word s))) awkward
let suite = ( "shell", [ Alcotest.test_case "the shell reads a word back" `Quick test_read_back; Alcotest.test_case "a plain word is not quoted" `Quick test_plain; ] )