From 89ed9a3a6c0ada2b1c252a5e24283b84eb0fa4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 15 Sep 2025 21:10:39 +0200 Subject: =?UTF-8?q?Add=20timeout=20argument=20for=20the=20=E2=80=98expect?= =?UTF-8?q?=E2=80=99=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changelog: Added `timeout` argument for the `expect` command --- README.md | 5 ++++- src/Parser/Statement.hs | 1 + src/Run.hs | 10 +++++----- src/Test.hs | 2 +- test/asset/run/callstack.et | 7 ++++++- test/script/run.et | 12 +++++++++++- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4beb96a..be41714 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,7 @@ send to Send line with `` to the standard input of ``. ``` -expect from [capture [, ... ]] +expect from [timeout ] [capture [, ... ]] ``` Check whether `` produces line matching `` on standard output, and if this does not happen within current timeout, the test fails. Output lines produced before starting this command and not matched by some previous `expect` are accepted as well. @@ -274,6 +274,9 @@ The regular expression can contain capture groups – parts enclosed in parenthe In that case the expect command has to have the `capture` clause with matching number of variable names. Results of the captures are then assigned to the newly created variables as strings. +If the `timeout` clause is used, the current timeout value is multiplied by the given `` for this `expect` call. +Timeout of zero can be used to expect a matching output line to have been already produced in the past. + ``` flush [from ] [matching ] ``` diff --git a/src/Parser/Statement.hs b/src/Parser/Statement.hs index 474fa03..571a38e 100644 --- a/src/Parser/Statement.hs +++ b/src/Parser/Statement.hs @@ -434,6 +434,7 @@ testExpect = command "expect" $ Expect <$> cmdLine <*> (fromExprParam <$> paramOrContext "from") <*> param "" + <*> (maybe 1 fromExprParam <$> param "timeout") <*> param "capture" <*> innerBlockFunList diff --git a/src/Run.hs b/src/Run.hs index 4a08742..1a1dea0 100644 --- a/src/Run.hs +++ b/src/Run.hs @@ -202,8 +202,8 @@ runStep = \case outProc OutputChildStdin p line send p line - Expect line p expr captures inner -> do - expect line p expr captures $ runStep . inner + Expect line p expr timeout captures inner -> do + expect line p expr timeout captures $ runStep . inner Flush p regex -> do atomicallyTest $ flushProcessOutput p regex @@ -318,9 +318,9 @@ exprFailed desc stack pname = do outLine (OutputMatchFail stack) (Just prompt) $ desc <> " failed" throwError Failed -expect :: SourceLine -> Process -> Traced Regex -> [TypedVarName Text] -> ([ Text ] -> TestRun ()) -> TestRun () -expect sline p (Traced trace re) tvars inner = do - timeout <- getCurrentTimeout +expect :: SourceLine -> Process -> Traced Regex -> Scientific -> [TypedVarName Text] -> ([ Text ] -> TestRun ()) -> TestRun () +expect sline p (Traced trace re) etimeout tvars inner = do + timeout <- (etimeout *) <$> getCurrentTimeout delay <- liftIO $ registerDelay $ ceiling $ 1000000 * timeout mbmatch <- atomicallyTest $ (Nothing <$ (check =<< readTVar delay)) <|> do line <- readTVar (procOutput p) diff --git a/src/Test.hs b/src/Test.hs index 18933b1..1481b2b 100644 --- a/src/Test.hs +++ b/src/Test.hs @@ -48,7 +48,7 @@ data TestStep a where Spawn :: TypedVarName Process -> Either Network Node -> [ Text ] -> (Process -> TestStep a) -> TestStep a SpawnShell :: Maybe (TypedVarName Process) -> Node -> ShellScript -> (Process -> TestStep a) -> TestStep a Send :: Process -> Text -> TestStep () - Expect :: SourceLine -> Process -> Traced Regex -> [ TypedVarName Text ] -> ([ Text ] -> TestStep a) -> TestStep a + Expect :: SourceLine -> Process -> Traced Regex -> Scientific -> [ TypedVarName Text ] -> ([ Text ] -> TestStep a) -> TestStep a Flush :: Process -> Maybe Regex -> TestStep () Guard :: CallStack -> Bool -> TestStep () DisconnectNode :: Node -> TestStep a -> TestStep a diff --git a/test/asset/run/callstack.et b/test/asset/run/callstack.et index 954b9ad..f500fb6 100644 --- a/test/asset/run/callstack.et +++ b/test/asset/run/callstack.et @@ -1,3 +1,8 @@ -test A: +test AG: let x = 1 guard (x == 0) + +test AE: + spawn as p + let x = 2 + expect /$x/ from p timeout 0.0 diff --git a/test/script/run.et b/test/script/run.et index dc2b812..f7e4f69 100644 --- a/test/script/run.et +++ b/test/script/run.et @@ -111,10 +111,20 @@ test CallStack: send "load ${scripts.path}/callstack.et" expect /load-done/ - send "run A" + send "run AG" expect /match-fail guard failed/ expect /match-fail-line .*\/callstack.et:3:5: .*/ expect /match-fail-var x 1/ local: expect /(run-.*)/ capture done guard (done == "run-failed") + flush + + send "run AE" + expect /match-fail expect failed/ + expect /match-fail-line .*\/callstack.et:8:5: .*/ + expect /match-fail-var x 2/ + local: + expect /(run-.*)/ capture done + guard (done == "run-failed") + flush -- cgit v1.2.3