Skip to content

Commit

Permalink
feat: general variable substitution (#196)
Browse files Browse the repository at this point in the history
Support environment variables and special variables (like `$__TEST_DIR__`) substitution with the crate [`subst`](crates.io/subst). Basically, this is a general form of the previous `replace_keywords`.

This is helpful if we want to don't want to hardcode some information in the SQL, for example, authentication information.

```
control substitution on

query T
select $MY_USERNAME, ${MY_PASSWORD}, ${MY_INEXISTENT_PORT:11451}, ${MY_DATABASE:$MY_USERNAME-db}
----
sqllogictest, rust, 11451, sqllogictest-db

query T
select $__TEST_DIR__
----
/var/folders/h3/ky82klmd2ygfr58ppqm4js6m0000gn/T/.tmp63ZLdk
```

See `substitution/basic.slt` for more detailed usages.

In order to be compatible with other implementations, this feature is under the gate of `control substitution` and is by default disabled.
  • Loading branch information
BugenZhao authored Sep 19, 2023
1 parent d554415 commit 263aa0c
Show file tree
Hide file tree
Showing 18 changed files with 538 additions and 304 deletions.
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
Loading

0 comments on commit 263aa0c

Please sign in to comment.