(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* The terminal path of [sql read] renders a box rather than pipe-separated fields, and answers which table was found the way the piped path does: test/cram/read_door.t pins the piped path. script(1) is what puts a pty under the tool. Its BSD form takes the command after the typescript file, which util-linux spells as an argument to -c, and a sandbox can refuse the pty itself; so the case runs only where that form hands a pty to the child and hands back the child's status, which the probe below asks. *) let sql = Filename.concat (Sys.getcwd ()) "../bin/sql.exe" let command ?(out = "/dev/null") argv = Sys.command (Filename.quote_command "script" ~stdin:"/dev/null" ~stdout:out ~stderr:out ("-q" :: "/dev/null" :: argv)) (* [None] where script(1) hands its child a pty and returns the child's own status, the reason otherwise. *) let pty_refusal () = match command [ "sh"; "-c"; "test -t 1 && exit 7" ] with | 7 -> None | rc -> Fmt.kstr Option.some "script -q /dev/null CMD exits %d where a child on a pty exits 7: no \ BSD script(1), or no pty for it on this host" rc let read_file path = In_channel.with_open_bin path In_channel.input_all let contains ~sub s = let n = String.length sub and m = String.length s in let rec go i = i + n <= m && (String.sub s i n = sub || go (i + 1)) in go 0 (* The live display clears itself as it leaves, so a captured pty holds the cursor control around a successful read and none of its rows; the status is what can be read back of one. A table the schema holds, in the spelling it holds it under and in another, and a name no table has, which is the refusal the piped path prints, with the same status. *) let test_read_on_a_terminal () = (match pty_refusal () with | None -> () | Some reason -> Fmt.epr "SKIP read door on a terminal: %s@." reason; Alcotest.skip ()); let dir = Filename.temp_dir "sqlite-read-door" "" in let db = Filename.concat dir "e.db" and mk = Filename.concat dir "mk.sql" in Out_channel.with_open_bin mk (fun oc -> output_string oc "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);\n\ INSERT INTO users (id, name) VALUES (1, 'ada');\n"); Alcotest.(check int) "the fixture is written" 0 (Sys.command (Filename.quote_command sql [ "script"; db; mk ])); let tty = Filename.concat dir "tty.txt" in let read table = command ~out:tty [ sql; "read"; db; "-t"; table ] in Alcotest.(check int) "the table under its own spelling" 0 (read "users"); Alcotest.(check int) "and under another" 0 (read "USERS"); Alcotest.(check int) "a name no table has" 1 (read "nosuch"); Alcotest.(check bool) "is refused naming it" true (contains ~sub:"no such table: nosuch" (read_file tty)) let suite = ( "sql", [ Alcotest.test_case "read on a terminal" `Quick test_read_on_a_terminal ] )