Skip to content

Commit 07b11cc

Browse files
authored
Escape regex special characters in the completion mode (#174)
Also bump to 0.13.2
1 parent 9311624 commit 07b11cc

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.13.2] - 2023-03-24
9+
10+
* `Runner::update_test_file` properly escapes regex special characters.
11+
812
## [0.13.1] - 2023-03-16
913

1014
* Support postgres options.

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = ["examples/*", "sqllogictest", "sqllogictest-bin", "sqllogictest-engines", "tests"]
33

44
[workspace.package]
5-
version = "0.13.1"
5+
version = "0.13.2"
66
edition = "2021"
77
homepage = "https://github.com/risinglightdb/sqllogictest-rs"
88
keywords = ["sql", "database", "parser", "cli"]

examples/basic/basic.slt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,16 @@ select * from example_basic
6161
Alice
6262
Bob
6363
Eve
64+
65+
# error is a regex. Special characters change the meaning of a regex and have to be escaped
66+
statement error The operation \(describe\) is not supported. Did you mean \[describe\]\?
67+
desc table example_basic;
68+
69+
statement error The operation \(describe\) is not supported. Did you mean [describe]?
70+
desc table example_basic;
71+
72+
query error The operation \(describe\) is not supported. Did you mean \[describe\]\?
73+
desc table example_basic;
74+
75+
query error The operation \(describe\) is not supported. Did you mean [describe]?
76+
desc table example_basic;

examples/basic/examples/basic.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use sqllogictest::{DBOutput, DefaultColumnType};
55
pub struct FakeDB;
66

77
#[derive(Debug)]
8-
pub struct FakeDBError;
8+
pub struct FakeDBError(String);
99

1010
impl std::fmt::Display for FakeDBError {
1111
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12-
write!(f, "{:?}", "Hey you got FakeDBError!")
12+
write!(f, "{:?}", self.0)
1313
}
1414
}
1515

@@ -39,7 +39,12 @@ impl sqllogictest::DB for FakeDB {
3939
if sql.starts_with("drop") {
4040
return Ok(DBOutput::StatementComplete(0));
4141
}
42-
Err(FakeDBError)
42+
if sql.starts_with("desc") {
43+
return Err(FakeDBError(
44+
"The operation (describe) is not supported. Did you mean [describe]?".to_string(),
45+
));
46+
}
47+
Err(FakeDBError("Hey you got FakeDBError!".to_string()))
4348
}
4449
}
4550

sqllogictest/src/runner.rs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ pub fn update_record_with_output<T: ColumnType>(
11511151
// Error mismatch, update expected error
11521152
(Some(e), _) => Some(Record::Statement {
11531153
sql,
1154-
expected_error: Some(Regex::new(&e.to_string()).unwrap()),
1154+
expected_error: Some(Regex::new(&regex::escape(&e.to_string())).unwrap()),
11551155
loc,
11561156
conditions,
11571157
expected_count: None,
@@ -1190,7 +1190,7 @@ pub fn update_record_with_output<T: ColumnType>(
11901190
(Some(e), _) => {
11911191
return Some(Record::Query {
11921192
sql,
1193-
expected_error: Some(Regex::new(&e.to_string()).unwrap()),
1193+
expected_error: Some(Regex::new(&regex::escape(&e.to_string())).unwrap()),
11941194
loc,
11951195
conditions,
11961196
expected_types: vec![],
@@ -1481,6 +1481,86 @@ mod tests {
14811481
.run()
14821482
}
14831483

1484+
#[test]
1485+
fn test_statement_error_special_chars() {
1486+
TestCase {
1487+
// statement expected error
1488+
input: "statement error tbd\n\
1489+
inser into foo values(2);",
1490+
1491+
// Model a run that produced an error message that contains regex special characters
1492+
record_output: statement_output_error("The operation (inser) is not supported. Did you mean [insert]?"),
1493+
1494+
// expect the output includes foo
1495+
expected: Some(
1496+
"statement error TestError: The operation \\(inser\\) is not supported\\. Did you mean \\[insert\\]\\?\n\
1497+
inser into foo values(2);",
1498+
),
1499+
}
1500+
.run()
1501+
}
1502+
1503+
#[test]
1504+
fn test_statement_keep_error_regex_when_matches() {
1505+
TestCase {
1506+
// statement expected error
1507+
input: "statement error TestError: The operation \\([a-z]+\\) is not supported.*\n\
1508+
inser into foo values(2);",
1509+
1510+
// Model a run that produced an error message that contains regex special characters
1511+
record_output: statement_output_error(
1512+
"The operation (inser) is not supported. Did you mean [insert]?",
1513+
),
1514+
1515+
// expect the output includes foo
1516+
expected: Some(
1517+
"statement error TestError: The operation \\([a-z]+\\) is not supported.*\n\
1518+
inser into foo values(2);",
1519+
),
1520+
}
1521+
.run()
1522+
}
1523+
1524+
#[test]
1525+
fn test_query_error_special_chars() {
1526+
TestCase {
1527+
// statement expected error
1528+
input: "query error tbd\n\
1529+
selec *;",
1530+
1531+
// Model a run that produced an error message that contains regex special characters
1532+
record_output: query_output_error("The operation (selec) is not supported. Did you mean [select]?"),
1533+
1534+
// expect the output includes foo
1535+
expected: Some(
1536+
"query error TestError: The operation \\(selec\\) is not supported\\. Did you mean \\[select\\]\\?\n\
1537+
selec *;",
1538+
),
1539+
}
1540+
.run()
1541+
}
1542+
1543+
#[test]
1544+
fn test_query_error_special_chars_when_matches() {
1545+
TestCase {
1546+
// statement expected error
1547+
input: "query error TestError: The operation \\([a-z]+\\) is not supported.*\n\
1548+
selec *;",
1549+
1550+
// Model a run that produced an error message that contains regex special characters
1551+
record_output: query_output_error(
1552+
"The operation (selec) is not supported. Did you mean [select]?",
1553+
),
1554+
1555+
// expect the output includes foo
1556+
expected: Some(
1557+
"query error TestError: The operation \\([a-z]+\\) is not supported.*\n\
1558+
selec *;",
1559+
),
1560+
}
1561+
.run()
1562+
}
1563+
14841564
#[derive(Debug)]
14851565
struct TestCase {
14861566
input: &'static str,

0 commit comments

Comments
 (0)