Skip to content

Commit

Permalink
Introduce a new test helper function: tryExecCardanoCLI
Browse files Browse the repository at this point in the history
  This is meant as a way to assert on *expected failures*! Sadly, `try` or other exception handling mechanisms do not work inside of the `TestT` monad, so I had to extract and lift the error to be able to catch it and assert on it. Yet, I need to assert on failures and thus, failures should not crash the test early but be assertable as a possible execution outcome.

  There's maybe something more clever to do but I only had a day and a half to spend on all this so I'd rather "get it done".
  • Loading branch information
KtorZ authored and Jimbo4350 committed Apr 17, 2023
1 parent 1b86de4 commit 0d0744e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cardano-cli/test/Test/OptParse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Test.OptParse
, checkTextEnvelopeFormat
, equivalence
, execCardanoCLI
, tryExecCardanoCLI
, propertyOnce
, withSnd
, noteInputFile
Expand All @@ -16,7 +17,10 @@ import Cardano.Api

import Cardano.CLI.Shelley.Run.Read

import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except (runExceptT)
import Control.Monad.IO.Class (MonadIO (..))
import Data.Function ((&))
import GHC.Stack (CallStack, HasCallStack)
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Process as H
Expand All @@ -37,6 +41,20 @@ execCardanoCLI
-- ^ Captured stdout
execCardanoCLI = GHC.withFrozenCallStack $ H.execFlex "cardano-cli" "CARDANO_CLI"

tryExecCardanoCLI
:: [String]
-- ^ Arguments to the CLI command
-> H.PropertyT IO (Either H.Failure String)
-- ^ Captured stdout, or error in case of failures
tryExecCardanoCLI args =
GHC.withFrozenCallStack (H.execFlex "cardano-cli" "CARDANO_CLI") args
& H.unPropertyT
& H.unTest
& runExceptT
& lift
& H.TestT
& H.PropertyT

-- | Checks that the 'tvType' and 'tvDescription' are equivalent between two files.
checkTextEnvelopeFormat
:: (MonadTest m, MonadIO m, HasCallStack)
Expand Down

0 comments on commit 0d0744e

Please sign in to comment.