Skip to content

Commit 824a111

Browse files
authored
Merge pull request #997 from IntersectMBO/mgalazyn/fix/make-tests-run-in-resourcet-io
Make tests run in `PropertyT (ResourceT IO)`
2 parents f226a23 + 3ac097b commit 824a111

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

cardano-cli/cardano-cli.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,14 @@ library cardano-cli-test-lib
288288
exceptions,
289289
filepath,
290290
hedgehog,
291-
hedgehog-extras >=0.6.1 && <0.6.5.1,
291+
hedgehog-extras >=0.6.1,
292292
http-types,
293293
lifted-base,
294+
mmorph,
294295
monad-control,
295296
network,
296297
process,
298+
resourcet,
297299
text,
298300
transformers-base,
299301
utf8-string,
@@ -322,9 +324,11 @@ test-suite cardano-cli-test
322324
filepath,
323325
hedgehog,
324326
hedgehog-extras,
327+
mmorph,
325328
monad-control,
326329
parsec,
327330
regex-tdfa,
331+
resourcet,
328332
tasty,
329333
tasty-hedgehog,
330334
text,

cardano-cli/test/cardano-cli-golden/Test/Golden/Byron/Tx.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import Cardano.CLI.Byron.Tx
99

1010
import Control.Monad (void)
1111
import Data.ByteString (ByteString)
12+
import GHC.Stack
1213

1314
import Test.Cardano.CLI.Util
1415

15-
import Hedgehog (Property, (===))
16-
import qualified Hedgehog as H
16+
import Hedgehog (MonadTest, Property, (===))
1717
import qualified Hedgehog.Extras.Test.Base as H
1818
import Hedgehog.Internal.Property (failWith)
1919

@@ -67,14 +67,14 @@ hprop_byronTx = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
6767

6868
compareByronTxs createdTx expectedTx
6969

70-
getTxByteString :: FilePath -> H.PropertyT IO (ATxAux ByteString)
70+
getTxByteString :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> m (ATxAux ByteString)
7171
getTxByteString txFp = do
7272
eATxAuxBS <- liftIO . runExceptT $ readByronTx $ File txFp
7373
case eATxAuxBS of
7474
Left err -> failWith Nothing . docToString $ renderByronTxError err
7575
Right aTxAuxBS -> return aTxAuxBS
7676

77-
compareByronTxs :: FilePath -> FilePath -> H.PropertyT IO ()
77+
compareByronTxs :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> FilePath -> m ()
7878
compareByronTxs createdTx expectedTx = do
7979
createdATxAuxBS <- getTxByteString createdTx
8080
expectedATxAuxBS <- getTxByteString expectedTx

cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import Control.Exception.Lifted (bracket_)
3636
import Control.Monad (when)
3737
import Control.Monad.Base
3838
import Control.Monad.Catch hiding (bracket_)
39+
import Control.Monad.Morph (hoist)
3940
import Control.Monad.Trans.Control (MonadBaseControl)
41+
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
4042
import qualified Data.Aeson as Aeson
4143
import qualified Data.Aeson.Encode.Pretty as Aeson
4244
import qualified Data.Aeson.Key as Aeson
@@ -62,7 +64,7 @@ import qualified Hedgehog as H
6264
import Hedgehog.Extras (ExecConfig)
6365
import qualified Hedgehog.Extras as H
6466
import Hedgehog.Extras.Test (ExecConfig (..))
65-
import Hedgehog.Internal.Property (Diff, MonadTest, PropertyT, liftTest, mkTest)
67+
import Hedgehog.Internal.Property (Diff, MonadTest, liftTest, mkTest)
6668
import qualified Hedgehog.Internal.Property as H
6769
import Hedgehog.Internal.Show (ValueDiff (ValueSame), mkValue, showPretty, valueDiff)
6870
import Hedgehog.Internal.Source (getCaller)
@@ -161,9 +163,10 @@ execDetailFlex execConfig pkgBin envBin arguments = GHC.withFrozenCallStack $ do
161163
H.evalIO $ IO.readCreateProcessWithExitCode cp ""
162164

163165
tryExecCardanoCLI
164-
:: [String]
166+
:: (MonadCatch m, MonadIO m, HasCallStack)
167+
=> [String]
165168
-- ^ Arguments to the CLI command
166-
-> H.PropertyT IO (Either H.Failure String)
169+
-> H.PropertyT m (Either H.Failure String)
167170
-- ^ Captured stdout, or error in case of failures
168171
tryExecCardanoCLI args =
169172
GHC.withFrozenCallStack (H.execFlex "cardano-cli" "CARDANO_CLI") args
@@ -278,8 +281,8 @@ withSnd f a = (a, f a)
278281

279282
-- These were lifted from hedgehog and slightly modified
280283

281-
propertyOnce :: H.PropertyT IO () -> H.Property
282-
propertyOnce = H.withTests 1 . H.withShrinks 0 . H.property
284+
propertyOnce :: H.PropertyT (ResourceT IO) () -> H.Property
285+
propertyOnce = H.withTests 1 . H.withShrinks 0 . H.property . hoist runResourceT
283286

284287
-- | Check for equivalence between two types and perform a file cleanup on failure.
285288
equivalence
@@ -388,9 +391,10 @@ bracketSem (FileSem path semaphore) act =
388391
act path
389392

390393
-- | Invert the behavior of a MonadTest: success becomes failure and vice versa.
391-
expectFailure :: HasCallStack => H.TestT IO m -> PropertyT IO ()
394+
expectFailure
395+
:: (MonadTrans t, MonadTest (t m), MonadCatch (t m), MonadIO m, HasCallStack) => H.TestT m a -> t m ()
392396
expectFailure prop = GHC.withFrozenCallStack $ do
393-
(res, _) <- H.evalIO $ H.runTestT prop
397+
(res, _) <- H.evalM . lift $ H.runTestT prop
394398
case res of
395399
Left _ -> pure () -- Property failed so we succeed
396400
_ -> H.failWith Nothing "Expected the test to fail but it passed" -- Property passed but we expected a failure

cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Test.Cli.Hash where
44

55
import Control.Monad (void)
6+
import Control.Monad.IO.Class
67
import Data.List (intercalate)
78
import GHC.IO.Exception (ExitCode (..))
89
import System.Directory (getCurrentDirectory)
@@ -77,7 +78,7 @@ hprop_generate_anchor_data_hash_from_file_uri =
7778
]
7879
result === exampleAnchorDataHash
7980
where
80-
toPOSIX :: FilePath -> PropertyT IO [Char]
81+
toPOSIX :: MonadIO m => FilePath -> PropertyT m [Char]
8182
toPOSIX path =
8283
case map dropTrailingPathSeparator (splitDirectories path) of
8384
letter : restOfPath -> do

cardano-cli/test/cardano-cli-test/Test/Cli/Pipes.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Test.Cli.Pipes
1515
-- Need this to avoid an unused-package error on Windows when compiling with
1616
-- cabal-3.10 and ghc-9.6.
1717
import System.FilePath ()
18+
import Control.Monad.Morph ()
1819
#endif
1920

2021
import qualified Hedgehog as H
@@ -24,11 +25,12 @@ import System.FilePath ((</>))
2425
#ifdef UNIX
2526
import Cardano.CLI.Read
2627
import Cardano.CLI.OS.Posix
28+
import Test.Cardano.CLI.Util
2729

28-
30+
import Control.Monad.Morph (hoist)
31+
import Control.Monad.Trans.Resource (runResourceT)
2932
import qualified Data.ByteString.Char8 as BSC
3033
import qualified Data.ByteString.Lazy as LBS
31-
import Test.Cardano.CLI.Util
3234

3335
import Hedgehog ((===), forAll)
3436
import qualified Hedgehog.Gen as G
@@ -38,7 +40,7 @@ import qualified Hedgehog.Extras.Test.Base as H
3840
import qualified Hedgehog.Extras.Test.File as H
3941

4042
hprop_readFromPipe :: Property
41-
hprop_readFromPipe = H.withTests 10 . H.property . H.moduleWorkspace "tmp" $ \ws -> do
43+
hprop_readFromPipe = H.withTests 10 . H.property . hoist runResourceT . H.moduleWorkspace "tmp" $ \ws -> do
4244

4345
s <- forAll $ G.string (R.linear 1 8192) G.ascii
4446

cardano-cli/test/cardano-cli-test/Test/Cli/Shelley/Run/Hash.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
module Test.Cli.Shelley.Run.Hash where
44

55
import Control.Monad (void)
6+
import Control.Monad.Catch (MonadCatch)
7+
import Control.Monad.Trans.Resource (MonadResource)
8+
import GHC.Stack
69

710
import Test.Cardano.CLI.Util
811

9-
import Hedgehog (Property)
12+
import Hedgehog (MonadTest, Property)
1013
import qualified Hedgehog as H
1114
import qualified Hedgehog.Extras as H
1215

@@ -21,7 +24,7 @@ hprop_hash_trip =
2124
-- Test that @cardano-cli hash --text > file1@ and
2225
-- @cardano-cli --text --out-file file2@ yields
2326
-- similar @file1@ and @file2@ files.
24-
hash_trip_fun :: String -> H.PropertyT IO ()
27+
hash_trip_fun :: (MonadTest m, MonadCatch m, MonadResource m, HasCallStack) => String -> m ()
2528
hash_trip_fun input =
2629
H.moduleWorkspace "tmp" $ \tempDir -> do
2730
hashFile <- noteTempFile tempDir "hash.txt"

0 commit comments

Comments
 (0)