-- Reading and writing files on the database host. -- -- Everything is written under /tmp because that is the one directory the tests -- can rely on already existing: SQL has no way to create one. -- Error CONTEXT carries plpgsql line numbers, which would make this -- expected output break on every unrelated edit. \set SHOW_CONTEXT never -- Quiet, so that the expected output is the same whether or not an earlier -- test in the same database already created the extension. set client_min_messages to warning; create extension if not exists pg_statecharts_dev cascade; reset client_min_messages; \set dir '/tmp/pg_statecharts_test' \set chart_a '/tmp/pg_statecharts_test_a-1.0.scxml' \set chart_b '/tmp/pg_statecharts_test_b-1.0.scxml' -- -- Writing files -- -- Content has to survive the COPY based writer untouched, including -- backslashes, dollar quotes, tabs, empty lines and a trailing newline. select fsm.__write_file(:'dir' || '_roundtrip.txt', E'line one\n\nbackslash \\ and \\n literal\n$$ do $tag$ x $tag$ $$\ttab\nlast\n'); __write_file -------------- (1 row) select pg_read_file(:'dir' || '_roundtrip.txt') = E'line one\n\nbackslash \\ and \\n literal\n$$ do $tag$ x $tag$ $$\ttab\nlast\n' as roundtrip_exact; roundtrip_exact ----------------- t (1 row) -- COPY terminates every row, so content that does not end in a newline gets -- one. Documented rather than fixed: everything this extension writes ends in -- a newline already. select fsm.__write_file(:'dir' || '_nonewline.txt', 'abc'); __write_file -------------- (1 row) select pg_read_file(:'dir' || '_nonewline.txt') = E'abc\n' as trailing_newline_added; trailing_newline_added ------------------------ t (1 row) -- writing again replaces rather than appends select fsm.__write_file(:'dir' || '_nonewline.txt', 'xyz'); __write_file -------------- (1 row) select pg_read_file(:'dir' || '_nonewline.txt') as replaced; replaced ---------- xyz + (1 row) -- a missing output directory is reported with the directory that is missing select fsm.__write_file('/tmp/pg_statecharts_no_such_dir/out.sql', 'x'); ERROR: output directory does not exist: /tmp/pg_statecharts_no_such_dir HINT: create it first, for example: mkdir -p /tmp/pg_statecharts_no_such_dir -- carriage returns cannot survive the writer, so they are rejected up front select fsm.__write_file(:'dir' || '_cr.txt', E'a\r\nb'); ERROR: refusing to write /tmp/pg_statecharts_test_cr.txt because its content contains a carriage return or a control character DETAIL: those characters cannot survive the COPY based writer intact -- so is a line that is exactly a backslash and a dot, which COPY would quote select fsm.__write_file(:'dir' || '_eod.txt', E'a\n\\.\nb\n'); ERROR: refusing to write /tmp/pg_statecharts_test_eod.txt because a line consists of just a backslash and a dot DETAIL: COPY would quote that line and the file would no longer be written byte for byte -- a backslash-dot with anything else on the line is fine select fsm.__write_file(:'dir' || '_eod.txt', E'a\n\\.x\nb\n'); __write_file -------------- (1 row) select pg_read_file(:'dir' || '_eod.txt') = E'a\n\\.x\nb\n' as near_miss_written_exactly; near_miss_written_exactly --------------------------- t (1 row) -- -- Finding files -- select fsm.__write_file(:'chart_a', '' '