Skip to content

Commit

Permalink
feat: support read env config (#11)
Browse files Browse the repository at this point in the history
* feat: support read env config

* change config to path
  • Loading branch information
jiacai2050 authored Dec 28, 2022
1 parent 0f0143a commit 6c4c894
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $ tree examples/
examples/
├── basic-case # Testcase root directory
│ └── simple # One environment
│ ├── config.toml # Config file for current environment
│ ├── select.result # Expected result file
│ └── select.sql # Input SQL testcase
├── basic.rs # Entrypoint of this example
Expand All @@ -42,7 +43,7 @@ When there is any diffs, the runner will keep `*.output` for later investigation
Below is the output of this example:
```bash
Run testcase...
Start, env:simple, config:None.
Start, env:simple, config:Some("examples/basic-case/simple/config.toml").
Test case "examples/basic-case/simple/select" finished, cost: 0ms
Environment simple run finished, cost:1ms
Stop, env:simple.
Expand Down
1 change: 1 addition & 0 deletions examples/basic-case/simple/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description = "This is config for running basic test"
6 changes: 3 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

use std::{env, fmt::Display, process};
use std::{env, fmt::Display, path::Path, process};

use async_trait::async_trait;
use sqlness::{Database, EnvController, Runner};
Expand All @@ -18,7 +18,7 @@ impl Database for MyDB {
}

impl MyDB {
fn new(_env: &str, _config: Option<String>) -> Self {
fn new(_env: &str, _config: Option<&Path>) -> Self {
MyDB
}

Expand All @@ -31,7 +31,7 @@ impl MyDB {
impl EnvController for MyController {
type DB = MyDB;

async fn start(&self, env: &str, config: Option<String>) -> Self::DB {
async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB {
println!("Start, env:{}, config:{:?}.", env, config);
MyDB::new(env, config)
}
Expand Down
4 changes: 3 additions & 1 deletion src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

use std::path::Path;

use async_trait::async_trait;

use crate::database::Database;
Expand All @@ -25,7 +27,7 @@ pub trait EnvController {
/// And the config file's path to this environment if it's find, it's defined
/// by the `env_config_file` field in the root config toml, and the default
/// value is `config.toml`.
async fn start(&self, env: &str, config: Option<String>) -> Self::DB;
async fn start(&self, env: &str, config: Option<&Path>) -> Self::DB;

/// Stop one [`Database`].
async fn stop(&self, env: &str, database: Self::DB);
Expand Down
19 changes: 17 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ impl<E: EnvController> Runner<E> {
pub async fn run(&self) -> Result<()> {
let environments = self.collect_env().await?;
for env in environments {
// todo: read env config
let db = self.env_controller.start(&env, None).await;
let env_config = self.read_env_config(&env).await;
let config_path = env_config.as_path();
let config_path = if config_path.exists() {
Some(config_path)
} else {
None
};
let db = self.env_controller.start(&env, config_path).await;
if let Err(e) = self.run_env(&env, &db).await {
println!("Environment {} run failed with error {:?}", env, e);
}
Expand All @@ -80,6 +86,15 @@ impl<E: EnvController> Runner<E> {
Ok(())
}

async fn read_env_config(&self, env: &str) -> PathBuf {
let mut path_buf = std::path::PathBuf::new();
path_buf.push(&self.config.case_dir);
path_buf.push(env);
path_buf.push(&self.config.env_config_file);

path_buf
}

async fn collect_env(&self) -> Result<Vec<String>> {
let mut dirs = read_dir(&self.config.case_dir).await?;
let mut result = vec![];
Expand Down

0 comments on commit 6c4c894

Please sign in to comment.