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

feat: general variable substitution #196

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.17.0] - 2023-09-19

* Support environment variables substituion for SQL and system commands.
For compatibility, this feature is by default disabled, and can be enabled by adding `control substitution on` to the test file.
```
control substitution on

query TTTT
SELECT
'$foo' -- short
, '${foo}' -- long
, '${bar:default}' -- default value
, '${bar:$foo-default}' -- recursive default value
FROM baz;
----
...
```

Besides, there's a special variable `$__TEST_DIR__` which is the path to a temporary directory specific to the current test case.
This can be helpful if you need to manipulate some external resources during the test.
```
control substitution on

statement ok
COPY (SELECT * FROM foo) TO '$__TEST_DIR__/foo.txt';

system ok
echo "foo" > "$__TEST_DIR__/foo.txt"
```

Changes:
- (parser) **Breaking change**: Add `Control::Substitution`. Mark `Control` as `#[non_exhaustive]`.
- (runner) **Breaking change**: Remove `enable_testdir`. For migration, one should now enable general substitution by the `control` statement and use a dollar-prefixed `$__TEST_DIR__`.

## [0.16.0] - 2023-09-15

* Support running external system commands with the syntax below. This is useful for manipulating some external resources during the test.
Expand Down
51 changes: 48 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[workspace]
resolver = "2"
members = ["sqllogictest", "sqllogictest-bin", "sqllogictest-engines", "tests"]

[workspace.package]
version = "0.16.0"
version = "0.17.0"
edition = "2021"
homepage = "https://github.com/risinglightdb/sqllogictest-rs"
keywords = ["sql", "database", "parser", "cli"]
Expand Down
4 changes: 2 additions & 2 deletions sqllogictest-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ glob = "0.3"
itertools = "0.11"
quick-junit = { version = "0.3" }
rand = "0.8"
sqllogictest = { path = "../sqllogictest", version = "0.16" }
sqllogictest-engines = { path = "../sqllogictest-engines", version = "0.16" }
sqllogictest = { path = "../sqllogictest", version = "0.17" }
sqllogictest-engines = { path = "../sqllogictest-engines", version = "0.17" }
tokio = { version = "1", features = [
"rt",
"rt-multi-thread",
Expand Down
2 changes: 1 addition & 1 deletion sqllogictest-engines/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ postgres-types = { version = "0.2.5", features = ["derive", "with-chrono-0_4"] }
rust_decimal = { version = "1.30.0", features = ["tokio-pg"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqllogictest = { path = "../sqllogictest", version = "0.16" }
sqllogictest = { path = "../sqllogictest", version = "0.17" }
thiserror = "1"
tokio = { version = "1", features = [
"rt",
Expand Down
17 changes: 9 additions & 8 deletions sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ description = "Sqllogictest parser and runner."
[dependencies]
async-trait = "0.1"
educe = "0.4.22"
similar = "2.2.1"
fs-err = "2.9.0"
futures = "0.3"
glob = "0.3"
humantime = "2"
itertools = "0.11"
tracing = "0.1"
tempfile = "3"
thiserror = "1"
futures = "0.3"
libtest-mimic = "0.6"
regex = "1.9.1"
owo-colors = "3.5.0"
md-5 = "0.10"
fs-err = "2.9.0"
owo-colors = "3.5.0"
regex = "1.9.1"
similar = "2.2.1"
subst = "0.3"
tempfile = "3"
thiserror = "1"
tracing = "0.1"

[dev-dependencies]
pretty_assertions = "1"
3 changes: 2 additions & 1 deletion sqllogictest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

pub mod column_type;
pub mod connection;
pub mod harness;
pub mod parser;
pub mod runner;

Expand All @@ -44,4 +45,4 @@ pub use self::connection::*;
pub use self::parser::*;
pub use self::runner::*;

pub mod harness;
mod substitution;
45 changes: 42 additions & 3 deletions sqllogictest/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl<T: ColumnType> std::fmt::Display for Record<T> {
}
Record::Control(c) => match c {
Control::SortMode(m) => write!(f, "control sortmode {}", m.as_str()),
Control::Substitution(s) => write!(f, "control substitution {}", s.as_str()),
},
Record::Condition(cond) => match cond {
Condition::OnlyIf { label } => write!(f, "onlyif {label}"),
Expand Down Expand Up @@ -294,9 +295,38 @@ impl<T: ColumnType> std::fmt::Display for Record<T> {
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum Control {
/// Control sort mode.
SortMode(SortMode),
/// Control whether or not to substitute variables in the SQL.
Substitution(bool),
}

trait ControlItem: Sized {
/// Try to parse from string.
fn try_from_str(s: &str) -> Result<Self, ParseErrorKind>;

/// Convert to string.
fn as_str(&self) -> &'static str;
}

impl ControlItem for bool {
fn try_from_str(s: &str) -> Result<Self, ParseErrorKind> {
match s {
"on" => Ok(true),
"off" => Ok(false),
_ => Err(ParseErrorKind::InvalidControl(s.to_string())),
}
}

fn as_str(&self) -> &'static str {
if *self {
"on"
} else {
"off"
}
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -360,8 +390,8 @@ pub enum SortMode {
ValueSort,
}

impl SortMode {
pub fn try_from_str(s: &str) -> Result<Self, ParseErrorKind> {
impl ControlItem for SortMode {
fn try_from_str(s: &str) -> Result<Self, ParseErrorKind> {
match s {
"nosort" => Ok(Self::NoSort),
"rowsort" => Ok(Self::RowSort),
Expand All @@ -370,7 +400,7 @@ impl SortMode {
}
}

pub fn as_str(&self) -> &'static str {
fn as_str(&self) -> &'static str {
match self {
Self::NoSort => "nosort",
Self::RowSort => "rowsort",
Expand Down Expand Up @@ -651,6 +681,10 @@ fn parse_inner<T: ColumnType>(loc: &Location, script: &str) -> Result<Vec<Record
Ok(sort_mode) => records.push(Record::Control(Control::SortMode(sort_mode))),
Err(k) => return Err(k.at(loc)),
},
["substitution", on_off] => match bool::try_from_str(on_off) {
Ok(on_off) => records.push(Record::Control(Control::Substitution(on_off))),
Err(k) => return Err(k.at(loc)),
},
_ => return Err(ParseErrorKind::InvalidLine(line.into()).at(loc)),
},
["hash-threshold", threshold] => {
Expand Down Expand Up @@ -758,6 +792,11 @@ mod tests {
parse_roundtrip::<DefaultColumnType>("../tests/slt/rowsort.slt")
}

#[test]
fn test_substitution() {
parse_roundtrip::<DefaultColumnType>("../tests/substitution/basic.slt")
}

#[test]
fn test_test_dir_escape() {
parse_roundtrip::<DefaultColumnType>("../tests/test_dir_escape/test_dir_escape.slt")
Expand Down
Loading
Loading