Skip to content

Commit d554415

Browse files
authored
feat: run system commands (#193)
1 parent 8085ab6 commit d554415

File tree

16 files changed

+282
-29
lines changed

16 files changed

+282
-29
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## [0.16.0] - 2023-09-15
11+
12+
* Support running external system commands with the syntax below. This is useful for manipulating some external resources during the test.
13+
```
14+
system ok
15+
echo "Hello, world!"
16+
```
17+
The runner will check the exit code of the command, and the output will be ignored. Currently, only `ok` is supported.
18+
19+
Changes:
20+
- (parser) **Breaking change**: Add `Record::System`, and corresponding `TestErrorKind` and `RecordOutput`. Mark `TestErrorKind` and `RecordOutput` as `#[non_exhaustive]`.
21+
- (runner) Add `run_command` to `AsyncDB` trait. The default implementation will run the command with `std::process::Command::status`. Implementors can override this method to utilize an asynchronous runtime such as `tokio`.
22+
23+
* fix(runner): fix database name duplication for parallel tests by using the **full path** of the test file (instead of the file name) as the database name.
24+
1025
## [0.15.3] - 2023-08-02
1126

1227
* fix(bin): fix error context display. To avoid stack backtrace being printed, unset `RUST_BACKTRACE` environment variable, or use pre-built binaries built with stable toolchain instead.

Cargo.lock

Lines changed: 27 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 = ["sqllogictest", "sqllogictest-bin", "sqllogictest-engines", "tests"]
33

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

sqllogictest-bin/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ glob = "0.3"
2424
itertools = "0.11"
2525
quick-junit = { version = "0.3" }
2626
rand = "0.8"
27-
sqllogictest = { path = "../sqllogictest", version = "0.15" }
28-
sqllogictest-engines = { path = "../sqllogictest-engines", version = "0.15" }
27+
sqllogictest = { path = "../sqllogictest", version = "0.16" }
28+
sqllogictest-engines = { path = "../sqllogictest-engines", version = "0.16" }
2929
tokio = { version = "1", features = [
3030
"rt",
3131
"rt-multi-thread",

sqllogictest-bin/src/engines.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::fmt::Display;
2+
use std::process::ExitStatus;
3+
use std::time::Duration;
24

35
use async_trait::async_trait;
46
use clap::ValueEnum;
@@ -97,14 +99,14 @@ impl std::error::Error for EnginesError {
9799
}
98100
}
99101

100-
impl Engines {
101-
async fn run(&mut self, sql: &str) -> Result<DBOutput<DefaultColumnType>, anyhow::Error> {
102-
Ok(match self {
103-
Engines::Postgres(e) => e.run(sql).await?,
104-
Engines::PostgresExtended(e) => e.run(sql).await?,
105-
Engines::External(e) => e.run(sql).await?,
106-
})
107-
}
102+
macro_rules! dispatch_engines {
103+
($impl:expr, $inner:ident, $body:tt) => {{
104+
match $impl {
105+
Engines::Postgres($inner) => $body,
106+
Engines::PostgresExtended($inner) => $body,
107+
Engines::External($inner) => $body,
108+
}
109+
}};
108110
}
109111

110112
#[async_trait]
@@ -113,6 +115,22 @@ impl AsyncDB for Engines {
113115
type ColumnType = DefaultColumnType;
114116

115117
async fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>, Self::Error> {
116-
self.run(sql).await.map_err(EnginesError)
118+
dispatch_engines!(self, e, {
119+
e.run(sql)
120+
.await
121+
.map_err(|e| EnginesError(anyhow::Error::from(e)))
122+
})
123+
}
124+
125+
fn engine_name(&self) -> &str {
126+
dispatch_engines!(self, e, { e.engine_name() })
127+
}
128+
129+
async fn sleep(dur: Duration) {
130+
tokio::time::sleep(dur).await
131+
}
132+
133+
async fn run_command(command: std::process::Command) -> std::io::Result<ExitStatus> {
134+
Command::from(command).status().await
117135
}
118136
}

sqllogictest-engines/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ postgres-types = { version = "0.2.5", features = ["derive", "with-chrono-0_4"] }
1919
rust_decimal = { version = "1.30.0", features = ["tokio-pg"] }
2020
serde = { version = "1", features = ["derive"] }
2121
serde_json = "1"
22-
sqllogictest = { path = "../sqllogictest", version = "0.15" }
22+
sqllogictest = { path = "../sqllogictest", version = "0.16" }
2323
thiserror = "1"
2424
tokio = { version = "1", features = [
2525
"rt",

sqllogictest-engines/src/external.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
22
use std::marker::PhantomData;
3-
use std::process::Stdio;
3+
use std::process::{ExitStatus, Stdio};
44
use std::time::Duration;
55

66
use async_trait::async_trait;
@@ -120,6 +120,10 @@ impl AsyncDB for ExternalDriver {
120120
async fn sleep(dur: Duration) {
121121
tokio::time::sleep(dur).await
122122
}
123+
124+
async fn run_command(command: std::process::Command) -> std::io::Result<ExitStatus> {
125+
Command::from(command).status().await
126+
}
123127
}
124128

125129
struct JsonDecoder<T>(PhantomData<T>);

sqllogictest-engines/src/postgres/extended.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Write;
2+
use std::process::{Command, ExitStatus};
23
use std::time::Duration;
34

45
use async_trait::async_trait;
@@ -317,4 +318,8 @@ impl sqllogictest::AsyncDB for Postgres<Extended> {
317318
async fn sleep(dur: Duration) {
318319
tokio::time::sleep(dur).await
319320
}
321+
322+
async fn run_command(command: Command) -> std::io::Result<ExitStatus> {
323+
tokio::process::Command::from(command).status().await
324+
}
320325
}

sqllogictest-engines/src/postgres/simple.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::process::{Command, ExitStatus};
12
use std::time::Duration;
23

34
use async_trait::async_trait;
@@ -64,4 +65,8 @@ impl sqllogictest::AsyncDB for Postgres<Simple> {
6465
async fn sleep(dur: Duration) {
6566
tokio::time::sleep(dur).await
6667
}
68+
69+
async fn run_command(command: Command) -> std::io::Result<ExitStatus> {
70+
tokio::process::Command::from(command).status().await
71+
}
6772
}

sqllogictest/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ regex = "1.9.1"
2424
owo-colors = "3.5.0"
2525
md-5 = "0.10"
2626
fs-err = "2.9.0"
27+
28+
[dev-dependencies]
29+
pretty_assertions = "1"

0 commit comments

Comments
 (0)