diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-02 05:34:00 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-02 05:34:00 +0000 |
commit | 6dfb847facc74e8f05bcd1ba59ab734c319e9f86 (patch) | |
tree | ddf94309dacf5b96abaa6cf0f6014f07c3282775 | |
parent | a258486420c7cc53a925d612465d9d55ffb81566 (diff) | |
parent | 72290bd0c327ace6141337de121252051480293f (diff) | |
download | subplot-6dfb847facc74e8f05bcd1ba59ab734c319e9f86.tar.gz |
Merge branch 'fix-280' into 'main'
(subplotlib): Make scenario run output a little closer to Python runner
Closes #280
See merge request subplot/subplot!265
-rw-r--r-- | share/rust/template/macros.rs.tera | 2 | ||||
-rw-r--r-- | subplotlib-derive/src/lib.rs | 6 | ||||
-rw-r--r-- | subplotlib/src/scenario.rs | 44 | ||||
-rw-r--r-- | subplotlib/src/step.rs | 18 |
4 files changed, 28 insertions, 42 deletions
diff --git a/share/rust/template/macros.rs.tera b/share/rust/template/macros.rs.tera index 164421d..7a65e0f 100644 --- a/share/rust/template/macros.rs.tera +++ b/share/rust/template/macros.rs.tera @@ -26,5 +26,5 @@ ) {% endif -%} {% endfor -%} - .build() + .build(format!("{} {}", "{{step.kind | lower}}", base64_decode("{{step.text | base64}}"))) {%- endmacro builder -%} diff --git a/subplotlib-derive/src/lib.rs b/subplotlib-derive/src/lib.rs index 42b5e71..7fd1810 100644 --- a/subplotlib-derive/src/lib.rs +++ b/subplotlib-derive/src/lib.rs @@ -307,8 +307,6 @@ fn process_step(mut input: ItemFn) -> proc_macro2::TokenStream { }) .collect(); - let stepnamestr = format!("{}", stepname); - let builder_body = if ty_is_scenariocontext(&contexttype) { quote! { #stepname(ctx,#(#buildargs),*) @@ -323,8 +321,8 @@ fn process_step(mut input: ItemFn) -> proc_macro2::TokenStream { impl Builder { #(#fieldfns)* - pub fn build(self) -> ScenarioStep { - ScenarioStep::new(#stepnamestr, move |ctx, _defuse_poison| + pub fn build(self, step_text: String) -> ScenarioStep { + ScenarioStep::new(step_text, move |ctx, _defuse_poison| #builder_body, |scenario| register_contexts(scenario) ) diff --git a/subplotlib/src/scenario.rs b/subplotlib/src/scenario.rs index f382561..7cebb31 100644 --- a/subplotlib/src/scenario.rs +++ b/subplotlib/src/scenario.rs @@ -235,7 +235,7 @@ impl ScenarioContext { /// let run_step = subplotlib::steplibrary::runcmd::run::Builder::default() /// .argv0("true") /// .args("") -/// .build(); +/// .build("when I run true".to_string()); /// scenario.add_step(run_step, None); /// /// ``` @@ -299,7 +299,7 @@ impl Scenario { // Firstly, we start all the contexts let mut ret = Ok(()); let mut highest_start = None; - println!("Scenario Start: {}", self.contexts.title()); + println!("scenario: {}", self.contexts.title()); for (i, hook) in self.contexts.hooks.borrow().iter().enumerate() { let res = hook.scenario_starts(&self.contexts); if res.is_err() { @@ -308,34 +308,27 @@ impl Scenario { } highest_start = Some(i); } - println!( - "*** Context hooks returned {}", - if ret.is_ok() { "OK" } else { "Failure" } - ); + if ret.is_err() { + println!("*** Context hooks returned failure",); + } if ret.is_ok() { let mut highest = None; for (i, step) in self.steps.iter().map(|(step, _)| step).enumerate() { - println!(" !!! Step {}", step.name()); + println!(" step: {}", step.step_text()); let mut highest_prep = None; for (i, prep) in self.contexts.hooks.borrow().iter().enumerate() { - let res = prep.step_starts(&self.contexts, step.name()); + let res = prep.step_starts(&self.contexts, step.step_text()); if res.is_err() { ret = res; break; } highest_prep = Some(i); } - println!( - " *** Context hooks returned {}", - if ret.is_ok() { "OK" } else { "Failure" } - ); + if ret.is_err() { + println!("*** Context hooks returned failure",); + } if ret.is_ok() { - println!(" >>> Run step function"); let res = step.call(&self.contexts, false); - println!( - " Step returned {}", - if res.is_ok() { "OK" } else { "Failure" } - ); if res.is_err() { ret = res; break; @@ -343,7 +336,6 @@ impl Scenario { highest = Some(i); } if let Some(n) = highest_prep { - println!(" *** Unwinding step contexts"); for hookn in (0..=n).rev() { let res = self.contexts.hooks.borrow()[hookn].step_stops(&self.contexts); ret = ret.and(res) @@ -351,15 +343,13 @@ impl Scenario { } } if let Some(n) = highest { - println!(" *** Running cleanup functions"); for stepn in (0..=n).rev() { if let (_, Some(cleanup)) = &self.steps[stepn] { - println!(" >>> Cleanup {}", cleanup.name()); + println!(" cleanup: {}", cleanup.step_text()); let res = cleanup.call(&self.contexts, true); - println!( - " Cleanup returned {}", - if res.is_ok() { "OK" } else { "Failure" } - ); + if res.is_err() { + println!("*** Cleanup returned failure",); + } ret = ret.and(res); } } @@ -367,16 +357,12 @@ impl Scenario { } if let Some(n) = highest_start { - println!("*** Running scenario closedown"); for hookn in (0..=n).rev() { let res = self.contexts.hooks.borrow()[hookn].scenario_stops(&self.contexts); ret = ret.and(res); } } - println!( - "<<< Scenario returns {}", - if ret.is_ok() { "OK" } else { "Failure" } - ); + println!(" return: {}", if ret.is_ok() { "OK" } else { "Failure" }); ret } } diff --git a/subplotlib/src/step.rs b/subplotlib/src/step.rs index b9c2a86..bbea249 100644 --- a/subplotlib/src/step.rs +++ b/subplotlib/src/step.rs @@ -22,10 +22,12 @@ use crate::types::StepResult; /// ``` /// # use subplotlib::prelude::*; /// -/// let step = ScenarioStep::new("step name", |ctx, ok| Ok(()), |scen| ()); +/// let step = ScenarioStep::new( +/// "when everything works".to_string(), |ctx, ok| Ok(()), |scen| () +/// ); /// ``` pub struct ScenarioStep { - name: String, + step_text: String, func: Box<dyn Fn(&ScenarioContext, bool) -> StepResult>, reg: Box<dyn Fn(&Scenario)>, } @@ -36,13 +38,13 @@ impl ScenarioStep { /// This is used to construct a scenario step from a function which /// takes the scenario context container. This will generally be /// called from the generated build method for the step. - pub fn new<F, R>(name: &str, func: F, reg: R) -> Self + pub fn new<F, R>(step_text: String, func: F, reg: R) -> Self where F: Fn(&ScenarioContext, bool) -> StepResult + 'static, R: Fn(&Scenario) + 'static, { Self { - name: name.to_string(), + step_text, func: Box::new(func), reg: Box::new(reg), } @@ -69,12 +71,12 @@ impl ScenarioStep { // subsequent step calls may not be sound. There's not a lot we can // do to ensure things are good except try. let func = AssertUnwindSafe(|| (*self.func)(context, defuse_poison)); - catch_unwind(func).map_err(|e| Self::render_panic(self.name(), e))? + catch_unwind(func).map_err(|e| Self::render_panic(self.step_text(), e))? } - /// Return the name of this step - pub fn name(&self) -> &str { - &self.name + /// Return the full text of this step + pub fn step_text(&self) -> &str { + &self.step_text } /// Register any context types needed by this step |