From b284a0fccd681289870113525828de88a1dcc2e0 Mon Sep 17 00:00:00 2001 From: Daniel Jankowski Date: Wed, 8 May 2024 16:05:33 +0200 Subject: [PATCH] chore: add tests for run_command_if_needed --- Cargo.lock | 48 +++++++++++++++++++++++++++ Cargo.toml | 1 + src/widgets/command.rs | 74 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 52e5cd7..ce34cbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1341,6 +1341,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + [[package]] name = "futures-util" version = "0.3.30" @@ -1407,6 +1413,12 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "gloo-timers" version = "0.2.6" @@ -2764,6 +2776,12 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + [[package]] name = "rmp" version = "0.8.14" @@ -2786,6 +2804,35 @@ dependencies = [ "serde", ] +[[package]] +name = "rstest" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5316d2a1479eeef1ea21e7f9ddc67c191d497abc8fc3ba2467857abbb68330" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version 0.4.0", +] + +[[package]] +name = "rstest_macros" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04a9df72cc1f67020b0d63ad9bfe4a323e459ea7eb68e03bd9824db49f9a4c25" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version 0.4.0", + "syn 2.0.61", + "unicode-ident", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -4242,6 +4289,7 @@ dependencies = [ "kdl", "lazy_static", "regex", + "rstest", "tracing", "tracing-subscriber", "uuid", diff --git a/Cargo.toml b/Cargo.toml index e5f2887..07ac68f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ console = "0.15.8" tracing-subscriber = "0.3.18" tracing = "0.1.40" kdl = "4.6.0" +rstest = "0.19.0" [dev-dependencies] criterion = { version = "0.5.1", default-features = false, features = [ diff --git a/src/widgets/command.rs b/src/widgets/command.rs index 0abe13d..83609c4 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -9,7 +9,7 @@ use std::{ use chrono::{DateTime, Duration, Local}; use regex::Regex; -#[cfg(not(feature = "bench"))] +#[cfg(all(not(feature = "bench"), not(test)))] use zellij_tile::shim::{run_command, run_command_with_env_variables_and_cwd}; use crate::render::{formatted_parts_from_string_cached, FormattedPart}; @@ -39,7 +39,7 @@ struct CommandConfig { render_mode: RenderMode, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct CommandResult { pub exit_code: Option, pub stdout: String, @@ -125,10 +125,10 @@ fn render_dynamic_formatted_content(content: &str) -> String { } #[tracing::instrument(skip(command_config, state))] -fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &ZellijState) { +fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &ZellijState) -> bool { let got_result = state.command_results.contains_key(name); if got_result && command_config.interval == 0 { - return; + return false; } let ts = Local::now(); @@ -146,8 +146,8 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell let command = commandline_parser(&command_config.command); tracing::debug!("Running command: {:?}", command); - #[cfg(not(feature = "bench"))] if command_config.env.is_some() || command_config.cwd.is_some() { + #[cfg(all(not(feature = "bench"), not(test)))] run_command_with_env_variables_and_cwd( &command.iter().map(|x| x.as_str()).collect::>(), command_config.env.unwrap(), @@ -155,15 +155,19 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell context, ); - return; + return true; } - #[cfg(not(feature = "bench"))] + #[cfg(all(not(feature = "bench"), not(test)))] run_command( &command.iter().map(|x| x.as_str()).collect::>(), context, ); + + return true; } + + false } fn parse_config(zj_conf: &BTreeMap) -> BTreeMap { @@ -365,6 +369,7 @@ fn commandline_parser(input: &str) -> Vec { #[cfg(test)] mod test { use super::*; + use rstest::rstest; #[test] pub fn test_commandline_parser() { @@ -383,4 +388,59 @@ mod test { let expected = Vec::from(["bash", "-c", "pwd | base64 -c 'bla' | xxd"]); assert_eq!(result, expected); } + + #[rstest] + // no result, interval 1 second + #[case(1, &ZellijState::default(), true)] + // only run once without a result + #[case(0, &ZellijState::default(), true)] + // do not run with run once and result + #[case(0, &ZellijState { + command_results: BTreeMap::from([( + "test".to_owned(), + CommandResult::default(), + )]), + ..ZellijState::default() + }, false)] + // run if interval is exceeded + #[case(1, &ZellijState { + command_results: BTreeMap::from([( + "test".to_owned(), + CommandResult{ + context: BTreeMap::from([("timestamp".to_owned(), "0".to_owned())]), + ..CommandResult::default() + } + )]), + ..ZellijState::default() + }, true)] + // do not run if interval is not exceeded + #[case(1, &ZellijState { + command_results: BTreeMap::from([( + "test".to_owned(), + CommandResult{ + context: BTreeMap::from([("timestamp".to_owned(), Local::now().format(TIMESTAMP_FORMAT).to_string())]), + ..CommandResult::default() + } + )]), + ..ZellijState::default() + }, false)] + pub fn test_run_command_if_needed( + #[case] interval: i64, + #[case] state: &ZellijState, + #[case] expected: bool, + ) { + let res = run_command_if_needed( + CommandConfig { + command: "echo test".to_owned(), + format: FormattedPart::default(), + env: None, + cwd: None, + interval, + render_mode: RenderMode::Static, + }, + "test", + state, + ); + assert_eq!(res, expected); + } }