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 committed Apr 6, 2023
1 parent de92fb6 commit 763dfe1
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 763dfe1

Please sign in to comment.