-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support local variable substitution, add
__DATABASE__
variabl…
…e for bin (#253) * support local variable substitution Signed-off-by: Richard Chien <[email protected]> * update changelog Signed-off-by: Richard Chien <[email protected]> --------- Signed-off-by: Richard Chien <[email protected]>
- Loading branch information
Showing
7 changed files
with
118 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,75 @@ | ||
use std::sync::{Arc, OnceLock}; | ||
|
||
use subst::Env; | ||
use tempfile::{tempdir, TempDir}; | ||
|
||
use crate::RunnerLocals; | ||
|
||
pub mod well_known { | ||
pub const TEST_DIR: &str = "__TEST_DIR__"; | ||
pub const NOW: &str = "__NOW__"; | ||
pub const DATABASE: &str = "__DATABASE__"; | ||
} | ||
|
||
/// Substitute environment variables and special variables like `__TEST_DIR__` in SQL. | ||
#[derive(Default, Clone)] | ||
pub(crate) struct Substitution { | ||
/// The temporary directory for `__TEST_DIR__`. | ||
/// Lazily initialized and cleaned up when dropped. | ||
test_dir: Arc<OnceLock<TempDir>>, | ||
pub(crate) struct Substitution<'a> { | ||
runner_locals: &'a RunnerLocals, | ||
subst_env_vars: bool, | ||
} | ||
|
||
impl Substitution<'_> { | ||
pub fn new(runner_locals: &RunnerLocals, subst_env_vars: bool) -> Substitution { | ||
Substitution { | ||
runner_locals, | ||
subst_env_vars, | ||
} | ||
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
#[error("substitution failed: {0}")] | ||
pub(crate) struct SubstError(subst::Error); | ||
|
||
impl Substitution { | ||
pub fn substitute(&self, input: &str, subst_env_vars: bool) -> Result<String, SubstError> { | ||
if !subst_env_vars { | ||
Ok(input | ||
.replace("$__TEST_DIR__", &self.test_dir()) | ||
.replace("$__NOW__", &self.now())) | ||
} else { | ||
fn now_string() -> String { | ||
std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.expect("failed to get current time") | ||
.as_nanos() | ||
.to_string() | ||
} | ||
|
||
impl Substitution<'_> { | ||
pub fn substitute(&self, input: &str) -> Result<String, SubstError> { | ||
if self.subst_env_vars { | ||
subst::substitute(input, self).map_err(SubstError) | ||
} else { | ||
Ok(self.simple_replace(input)) | ||
} | ||
} | ||
|
||
fn test_dir(&self) -> String { | ||
let test_dir = self | ||
.test_dir | ||
.get_or_init(|| tempdir().expect("failed to create testdir")); | ||
test_dir.path().to_string_lossy().into_owned() | ||
} | ||
|
||
fn now(&self) -> String { | ||
std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.expect("failed to get current time") | ||
.as_nanos() | ||
.to_string() | ||
fn simple_replace(&self, input: &str) -> String { | ||
let mut res = input | ||
.replace( | ||
&format!("${}", well_known::TEST_DIR), | ||
&self.runner_locals.test_dir(), | ||
) | ||
.replace(&format!("${}", well_known::NOW), &now_string()); | ||
for (key, value) in self.runner_locals.vars() { | ||
res = res.replace(&format!("${}", key), value); | ||
} | ||
res | ||
} | ||
} | ||
|
||
impl<'a> subst::VariableMap<'a> for Substitution { | ||
impl<'a> subst::VariableMap<'a> for Substitution<'a> { | ||
type Value = String; | ||
|
||
fn get(&'a self, key: &str) -> Option<Self::Value> { | ||
match key { | ||
"__TEST_DIR__" => self.test_dir().into(), | ||
"__NOW__" => self.now().into(), | ||
key => Env.get(key), | ||
well_known::TEST_DIR => self.runner_locals.test_dir().into(), | ||
well_known::NOW => now_string().into(), | ||
key => self | ||
.runner_locals | ||
.get_var(key) | ||
.cloned() | ||
.or_else(|| Env.get(key)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters