Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape regex special characters in the completion mode #174

Merged
merged 3 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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