Skip to content

Commit

Permalink
Escape regex special characters in the completion mode (#174)
Browse files Browse the repository at this point in the history
Also bump to 0.13.2
  • Loading branch information
melgenek authored Mar 25, 2023
1 parent 9311624 commit 07b11cc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.13.2] - 2023-03-24

* `Runner::update_test_file` properly escapes regex special characters.

## [0.13.1] - 2023-03-16

* Support postgres options.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["examples/*", "sqllogictest", "sqllogictest-bin", "sqllogictest-engines", "tests"]

[workspace.package]
version = "0.13.1"
version = "0.13.2"
edition = "2021"
homepage = "https://github.com/risinglightdb/sqllogictest-rs"
keywords = ["sql", "database", "parser", "cli"]
Expand Down
13 changes: 13 additions & 0 deletions examples/basic/basic.slt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ select * from example_basic
Alice
Bob
Eve

# error is a regex. Special characters change the meaning of a regex and have to be escaped
statement error The operation \(describe\) is not supported. Did you mean \[describe\]\?
desc table example_basic;

statement error The operation \(describe\) is not supported. Did you mean [describe]?
desc table example_basic;

query error The operation \(describe\) is not supported. Did you mean \[describe\]\?
desc table example_basic;

query error The operation \(describe\) is not supported. Did you mean [describe]?
desc table example_basic;
11 changes: 8 additions & 3 deletions examples/basic/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use sqllogictest::{DBOutput, DefaultColumnType};
pub struct FakeDB;

#[derive(Debug)]
pub struct FakeDBError;
pub struct FakeDBError(String);

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

Expand Down Expand Up @@ -39,7 +39,12 @@ impl sqllogictest::DB for FakeDB {
if sql.starts_with("drop") {
return Ok(DBOutput::StatementComplete(0));
}
Err(FakeDBError)
if sql.starts_with("desc") {
return Err(FakeDBError(
"The operation (describe) is not supported. Did you mean [describe]?".to_string(),
));
}
Err(FakeDBError("Hey you got FakeDBError!".to_string()))
}
}

Expand Down
84 changes: 82 additions & 2 deletions sqllogictest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ pub fn update_record_with_output<T: ColumnType>(
// Error mismatch, update expected error
(Some(e), _) => Some(Record::Statement {
sql,
expected_error: Some(Regex::new(&e.to_string()).unwrap()),
expected_error: Some(Regex::new(&regex::escape(&e.to_string())).unwrap()),
loc,
conditions,
expected_count: None,
Expand Down Expand Up @@ -1190,7 +1190,7 @@ pub fn update_record_with_output<T: ColumnType>(
(Some(e), _) => {
return Some(Record::Query {
sql,
expected_error: Some(Regex::new(&e.to_string()).unwrap()),
expected_error: Some(Regex::new(&regex::escape(&e.to_string())).unwrap()),
loc,
conditions,
expected_types: vec![],
Expand Down Expand Up @@ -1481,6 +1481,86 @@ mod tests {
.run()
}

#[test]
fn test_statement_error_special_chars() {
TestCase {
// statement expected error
input: "statement error tbd\n\
inser into foo values(2);",

// Model a run that produced an error message that contains regex special characters
record_output: statement_output_error("The operation (inser) is not supported. Did you mean [insert]?"),

// expect the output includes foo
expected: Some(
"statement error TestError: The operation \\(inser\\) is not supported\\. Did you mean \\[insert\\]\\?\n\
inser into foo values(2);",
),
}
.run()
}

#[test]
fn test_statement_keep_error_regex_when_matches() {
TestCase {
// statement expected error
input: "statement error TestError: The operation \\([a-z]+\\) is not supported.*\n\
inser into foo values(2);",

// Model a run that produced an error message that contains regex special characters
record_output: statement_output_error(
"The operation (inser) is not supported. Did you mean [insert]?",
),

// expect the output includes foo
expected: Some(
"statement error TestError: The operation \\([a-z]+\\) is not supported.*\n\
inser into foo values(2);",
),
}
.run()
}

#[test]
fn test_query_error_special_chars() {
TestCase {
// statement expected error
input: "query error tbd\n\
selec *;",

// Model a run that produced an error message that contains regex special characters
record_output: query_output_error("The operation (selec) is not supported. Did you mean [select]?"),

// expect the output includes foo
expected: Some(
"query error TestError: The operation \\(selec\\) is not supported\\. Did you mean \\[select\\]\\?\n\
selec *;",
),
}
.run()
}

#[test]
fn test_query_error_special_chars_when_matches() {
TestCase {
// statement expected error
input: "query error TestError: The operation \\([a-z]+\\) is not supported.*\n\
selec *;",

// Model a run that produced an error message that contains regex special characters
record_output: query_output_error(
"The operation (selec) is not supported. Did you mean [select]?",
),

// expect the output includes foo
expected: Some(
"query error TestError: The operation \\([a-z]+\\) is not supported.*\n\
selec *;",
),
}
.run()
}

#[derive(Debug)]
struct TestCase {
input: &'static str,
Expand Down

0 comments on commit 07b11cc

Please sign in to comment.