|
3 | 3 | namespace NpgsqlRestClient.Testing; |
4 | 4 |
|
5 | 5 | /// <summary> |
6 | | -/// Splits a .test.sql file into an ordered list of steps (SQL statements and embedded HTTP requests), |
7 | | -/// preserving interleaving. A char-by-char state machine handles ';' statement splitting, line/block |
8 | | -/// comments, single quotes (with '' escapes), and dollar-quoted strings / DO blocks (so semicolons inside |
9 | | -/// them don't split). A block comment whose first line is a request line becomes an <see cref="HttpStep"/>; |
10 | | -/// any other comment is ignored. |
| 6 | +/// Splits a .test.sql file into an ordered list of steps (SQL statements, embedded HTTP requests, and |
| 7 | +/// include lines), preserving interleaving. A char-by-char state machine handles ';' statement splitting, |
| 8 | +/// line/block comments, single quotes (with '' escapes), and dollar-quoted strings / DO blocks (so |
| 9 | +/// semicolons inside them don't split). A block comment whose first line is a request line becomes an |
| 10 | +/// <see cref="HttpStep"/>; any other comment is ignored. A line starting with <c>\i</c>/<c>\ir</c> between |
| 11 | +/// statements becomes an <see cref="IncludeStep"/> (expanded by <see cref="TestFileLoader"/>). |
11 | 12 | /// </summary> |
12 | 13 | public static class SqlTestFileParser |
13 | 14 | { |
14 | 15 | private enum State { Normal, BlockComment, SingleQuote, DollarQuote } |
15 | 16 |
|
| 17 | + /// <summary> |
| 18 | + /// Parses one line as an include: <c>\i path</c> (cwd-relative) or <c>\ir path</c> (relative to the |
| 19 | + /// including file). The path may be single-quoted; a trailing ';' is forgiven. Returns false for any |
| 20 | + /// other line (including other backslash commands). |
| 21 | + /// </summary> |
| 22 | + internal static bool TryParseIncludeLine(string line, out string path, out bool relativeToFile) |
| 23 | + { |
| 24 | + path = ""; |
| 25 | + relativeToFile = false; |
| 26 | + var t = line.Trim(); |
| 27 | + bool relative = t.StartsWith("\\ir", StringComparison.OrdinalIgnoreCase) |
| 28 | + && (t.Length == 3 || char.IsWhiteSpace(t[3])); |
| 29 | + bool plain = !relative && t.StartsWith("\\i", StringComparison.OrdinalIgnoreCase) |
| 30 | + && (t.Length == 2 || char.IsWhiteSpace(t[2])); |
| 31 | + if (!relative && !plain) return false; |
| 32 | + |
| 33 | + var p = t[(relative ? 3 : 2)..].Trim(); |
| 34 | + if (p.EndsWith(';')) p = p[..^1].TrimEnd(); // forgive a trailing ; |
| 35 | + if (p.Length > 1 && p[0] == '\'' && p[^1] == '\'') |
| 36 | + { |
| 37 | + p = p[1..^1]; // psql-style quoted path |
| 38 | + } |
| 39 | + if (p.Length == 0) return false; |
| 40 | + |
| 41 | + path = p; |
| 42 | + relativeToFile = relative; |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
16 | 46 | public static List<TestStep> Parse(string content) |
17 | 47 | { |
18 | 48 | var steps = new List<TestStep>(); |
@@ -63,6 +93,29 @@ void FlushSql() |
63 | 93 | switch (state) |
64 | 94 | { |
65 | 95 | case State.Normal: |
| 96 | + // include: \i <path> (cwd-relative) or \ir <path> (relative to the including file) — |
| 97 | + // psql semantics. Recognized only between statements (pending statement is blank), so a |
| 98 | + // backslash inside SQL text/strings/dollar-quotes is never misread. Consumes the line. |
| 99 | + if (c == '\\' && !stmtHasContent) |
| 100 | + { |
| 101 | + int lineEnd = i; |
| 102 | + while (lineEnd < len && content[lineEnd] != '\n') lineEnd++; |
| 103 | + var includeLine = content[i..lineEnd].TrimEnd('\r').TrimEnd(); |
| 104 | + if (TryParseIncludeLine(includeLine, out var path, out var relative)) |
| 105 | + { |
| 106 | + steps.Add(new IncludeStep(path, relative, currentLine)); |
| 107 | + i = lineEnd; // consume the include line; the newline is handled by the main loop |
| 108 | + continue; |
| 109 | + } |
| 110 | + // Not \i/\ir: keep just the backslash as SQL text and let normal parsing continue |
| 111 | + // (';' still splits; PostgreSQL will report the stray backslash clearly). |
| 112 | + stmt.Append(c); |
| 113 | + MarkContent(); |
| 114 | + lastToken = ""; |
| 115 | + lastTokenIsWord = false; |
| 116 | + i++; |
| 117 | + continue; |
| 118 | + } |
66 | 119 | // line comment: -- (skip to end of line, never an HTTP step) |
67 | 120 | if (c == '-' && i + 1 < len && content[i + 1] == '-') |
68 | 121 | { |
|
0 commit comments