From 20d7f2907be25d92df2fb9a574fd531ff2eddeb2 Mon Sep 17 00:00:00 2001 From: Etienne Wodey Date: Sun, 23 Mar 2025 22:02:35 +0100 Subject: [PATCH] watch: handle WATCH_INTERVAL --- src/uu/watch/src/watch.rs | 1 + tests/by-util/test_watch.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/uu/watch/src/watch.rs b/src/uu/watch/src/watch.rs index 6343c79..b1e7d6b 100644 --- a/src/uu/watch/src/watch.rs +++ b/src/uu/watch/src/watch.rs @@ -118,6 +118,7 @@ pub fn uu_app() -> Command { .long("interval") .help("Seconds to wait between updates") .default_value("2") + .env("WATCH_INTERVAL") .value_name("SECONDS"), ) .arg( diff --git a/tests/by-util/test_watch.rs b/tests/by-util/test_watch.rs index 9555c6f..4492625 100644 --- a/tests/by-util/test_watch.rs +++ b/tests/by-util/test_watch.rs @@ -14,6 +14,12 @@ const TRUE_CMD: &str = "%SYSTEMROOT%\\System32\\rundll32.exe"; #[cfg(not(windows))] const TRUE_CMD: &str = "true"; +#[cfg(windows)] +const ECHO_HELLO_CMD: &str = "echo | set /p dummyName=hello"; + +#[cfg(not(windows))] +const ECHO_HELLO_CMD: &str = "printf hello"; + #[test] fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); @@ -62,3 +68,20 @@ fn test_valid_interval_comma() { .no_stderr() .no_stdout(); } + +#[test] +fn test_interval_environment_variable() { + let mut p = new_ucmd!() + .arg(ECHO_HELLO_CMD) + .env("WATCH_INTERVAL", "0.3") + .run_no_wait(); + // With 0.5 seconds runtime, the watched command is called twice if + // `WATCH_INTERVAL` (0.3 seconds) is taken into account, but only once if the default + // interval (2 seconds) is used. + p.make_assertion_with_delay(500).is_alive(); + p.kill() + .make_assertion() + .with_all_output() + .no_stderr() + .stdout_is_bytes(b"hellohello"); +}