Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC for polysemy
Browse files Browse the repository at this point in the history
newhoggy committed May 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8ebae88 commit 7805ddc
Showing 8 changed files with 240 additions and 93 deletions.
13 changes: 12 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ repository cardano-haskell-packages
-- See CONTRIBUTING for information about these, including some Nix commands
-- you need to run if you change them
index-state:
, hackage.haskell.org 2024-05-02T11:03:23Z
, hackage.haskell.org 2024-05-18T10:18:09Z
, cardano-haskell-packages 2024-05-14T04:43:46Z

packages:
@@ -41,3 +41,14 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

-- Add upper bound to io-classes-mtl:

constraints:
io-classes-mtl < 0.1.2.0

source-repository-package
type: git
location: https://github.com/newhoggy/polysemy-conc
tag: 7d45ccd462c973f15156070ba24084ff15758f15
subdir: packages/conc
25 changes: 23 additions & 2 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
@@ -258,18 +258,27 @@ library cardano-cli-test-lib
import: project-config
visibility: public
hs-source-dirs: test/cardano-cli-test-lib
exposed-modules: Test.Cardano.CLI.Aeson
exposed-modules: Test.Cardano.API.Polysemy
Test.Cardano.CLI.Aeson
Test.Cardano.CLI.Polysemy
Test.Cardano.CLI.Util
ghc-options: -fplugin=Polysemy.Plugin
build-depends: aeson
, aeson-pretty
, bytestring
, cardano-api
, cardano-api:internal
, cardano-cli
, directory
, exceptions
, filepath
, hedgehog
, hedgehog-extras ^>= 0.6.1.0
, hw-polysemy:core ^>= 0.2.4.2
, hw-polysemy:hedgehog ^>= 0.2.4.2
, mtl
, polysemy
, polysemy-plugin
, lifted-base
, monad-control
, process
@@ -293,17 +302,23 @@ test-suite cardano-cli-test
, cardano-ledger-alonzo
, cardano-slotting
, containers
, exceptions
, filepath
, hedgehog
, hedgehog-extras ^>= 0.6.1.0
, exceptions
, hw-polysemy:core ^>= 0.2.4.2
, hw-polysemy:hedgehog ^>= 0.2.4.2
, polysemy
, polysemy-plugin
, regex-tdfa
, tasty
, tasty-hedgehog
, text
, time
, transformers

ghc-options: -fplugin=Polysemy.Plugin

build-tool-depends: tasty-discover:tasty-discover

other-modules: Test.Cli.AddCostModels
@@ -354,6 +369,10 @@ test-suite cardano-cli-golden
, filepath
, hedgehog ^>= 1.4
, hedgehog-extras ^>= 0.6.1.0
, hw-polysemy:core ^>= 0.2.4.2
, hw-polysemy:hedgehog ^>= 0.2.4.2
, polysemy
, polysemy-plugin
, regex-compat
, regex-tdfa
, tasty
@@ -365,6 +384,8 @@ test-suite cardano-cli-golden
build-tool-depends: cardano-cli:cardano-cli
, tasty-discover:tasty-discover

ghc-options: -fplugin=Polysemy.Plugin

other-modules: Test.Golden.Babbage.Transaction.CalculateMinFee
Test.Golden.Byron.SigningKeys
Test.Golden.Byron.Tx
69 changes: 39 additions & 30 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Byron/Tx.hs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

{- HLINT ignore "Use camelCase" -}

module Test.Golden.Byron.Tx where

import Cardano.Api
import Cardano.Api hiding (Error)

import Cardano.Chain.UTxO (ATxAux)
import Cardano.CLI.Byron.Tx

import Control.Monad (void)
import Data.ByteString (ByteString)

import Test.Cardano.CLI.Util

import Hedgehog (Property, (===))
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import Hedgehog.Internal.Property (failWith)
import Test.Cardano.CLI.Polysemy

{- HLINT ignore "Use camelCase" -}
import Polysemy
import HaskellWorks.Polysemy
import HaskellWorks.Polysemy.Hedgehog
import HaskellWorks.Prelude

hprop_byronTx_legacy :: Property
hprop_byronTx_legacy = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
signingKey <- noteInputFile "test/cardano-cli-golden/files/input/byron/keys/legacy.skey"
expectedTx <- noteInputFile "test/cardano-cli-golden/files/input/byron/tx/legacy.tx"
createdTx <- noteTempFile tempDir "tx"
void $ execCardanoCLI
hprop_byronTx_legacy = propertyOnce $ localWorkspace $ do
signingKey <- jotPkgInputFile "test/cardano-cli-golden/files/input/byron/keys/legacy.skey"
expectedTx <- jotPkgInputFile "test/cardano-cli-golden/files/input/byron/tx/legacy.tx"
createdTx <- jotTempFile "tx"

execCardanoCli_
[ "byron", "transaction", "issue-utxo-expenditure"
, "--mainnet"
, "--byron-legacy-formats"
@@ -34,14 +34,15 @@ hprop_byronTx_legacy = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
, "--txout", "(\"2657WMsDfac6eFirdvKVPVMxNVYuACd1RGM2arH3g1y1yaQCr1yYpb2jr2b2aSiDZ\",999)"
]

compareByronTxs createdTx expectedTx
void $ compareByronTxs createdTx expectedTx

hprop_byronTx :: Property
hprop_byronTx = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do
signingKey <- noteInputFile "test/cardano-cli-golden/files/input/byron/keys/byron.skey"
expectedTx <- noteInputFile "test/cardano-cli-golden/files/input/byron/tx/normal.tx"
createdTx <- noteTempFile tempDir "tx"
void $ execCardanoCLI
hprop_byronTx = propertyOnce $ localWorkspace $ do
signingKey <- jotPkgInputFile "test/cardano-cli-golden/files/input/byron/keys/byron.skey"
expectedTx <- jotPkgInputFile "test/cardano-cli-golden/files/input/byron/tx/normal.tx"
createdTx <- jotTempFile "tx"

execCardanoCli_
[ "byron", "transaction", "issue-utxo-expenditure"
, "--mainnet"
, "--byron-formats"
@@ -53,16 +54,24 @@ hprop_byronTx = propertyOnce $ H.moduleWorkspace "tmp" $ \tempDir -> do

compareByronTxs createdTx expectedTx

getTxByteString :: FilePath -> H.PropertyT IO (ATxAux ByteString)
getTxByteString :: ()
=> Member (Embed IO) r
=> Member (Error ByronTxError) r
=> FilePath
-> Sem r (ATxAux ByteString)
getTxByteString txFp = do
eATxAuxBS <- liftIO . runExceptT $ readByronTx $ File txFp
case eATxAuxBS of
Left err -> failWith Nothing . docToString $ renderByronTxError err
Right aTxAuxBS -> return aTxAuxBS
embed (runExceptT $ readByronTx $ File txFp)
& onLeftM throw

compareByronTxs :: FilePath -> FilePath -> H.PropertyT IO ()
compareByronTxs :: ()
=> HasCallStack
=> Member (Embed IO) r
=> Member Hedgehog r
=> FilePath
-> FilePath
-> Sem r ()
compareByronTxs createdTx expectedTx = do
createdATxAuxBS <- getTxByteString createdTx
expectedATxAuxBS <- getTxByteString expectedTx
createdATxAuxBS <- getTxByteString createdTx & trapFail @ByronTxError
expectedATxAuxBS <- getTxByteString expectedTx & trapFail @ByronTxError

normalByronTxToGenTx expectedATxAuxBS === normalByronTxToGenTx createdATxAuxBS
Original file line number Diff line number Diff line change
@@ -2,41 +2,38 @@

module Test.Golden.Shelley.StakeAddress.DeregistrationCertificate where

import Control.Monad (void)
import System.FilePath ((</>))
import Test.Cardano.CLI.Polysemy

import Test.Cardano.CLI.Util

import Hedgehog (Property)
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Process as H
import qualified Hedgehog.Extras.Test.Golden as H
import Polysemy ()
import HaskellWorks.Polysemy.Hedgehog
import HaskellWorks.Polysemy.Hedgehog.Golden

{- HLINT ignore "Use camelCase" -}

hprop_golden_shelley_stake_address_deregistration_certificate :: Property
hprop_golden_shelley_stake_address_deregistration_certificate = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
base <- H.getProjectBase
hprop_golden_shelley_stake_address_deregistration_certificate = propertyOnce $ localWorkspace $ do
verificationKeyFile <- jotPkgInputFile "test/cardano-cli-golden/files/input/shelley/keys/stake_keys/verification_key"
deregistrationCertFile <- jotTempFile "deregistrationCertFile"
scriptDeregistrationCertFile <- jotTempFile "scripDeregistrationCertFile"
exampleScript <- jotRootInputFile "scripts/plutus/scripts/v1/custom-guess-42-datum-42.plutus"

verificationKeyFile <- noteInputFile "test/cardano-cli-golden/files/input/shelley/keys/stake_keys/verification_key"
deregistrationCertFile <- noteTempFile tempDir "deregistrationCertFile"
scriptDeregistrationCertFile <- noteTempFile tempDir "scripDeregistrationCertFile"
exampleScript <- noteInputFile $ base </> "scripts/plutus/scripts/v1/custom-guess-42-datum-42.plutus"
jot_ exampleScript

void $ execCardanoCLI
execCardanoCli_
[ "babbage", "stake-address","deregistration-certificate"
, "--staking-verification-key-file", verificationKeyFile
, "--out-file", deregistrationCertFile
]

goldenFile1 <- H.note "test/cardano-cli-golden/files/golden/shelley/dereg-cert-1.json"
H.diffFileVsGoldenFile deregistrationCertFile goldenFile1
goldenFile1 <- jotPkgGoldenFile "test/cardano-cli-golden/files/golden/shelley/dereg-cert-1.json"
diffFileVsGoldenFile deregistrationCertFile goldenFile1

void $ execCardanoCLI
execCardanoCli_
[ "babbage", "stake-address","deregistration-certificate"
, "--stake-script-file", exampleScript
, "--out-file", scriptDeregistrationCertFile
]

goldenFile2 <- H.note "test/cardano-cli-golden/files/golden/shelley/dereg-cert-2.json"
H.diffFileVsGoldenFile scriptDeregistrationCertFile goldenFile2
goldenFile2 <- jotPkgGoldenFile "test/cardano-cli-golden/files/golden/shelley/dereg-cert-2.json"

diffFileVsGoldenFile scriptDeregistrationCertFile goldenFile2
28 changes: 28 additions & 0 deletions cardano-cli/test/cardano-cli-test-lib/Test/Cardano/API/Polysemy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}

module Test.Cardano.API.Polysemy
( File(..),
FileDirection(..),
VRFPrivateKeyFilePermissionError(..),
checkVrfFilePermissions,
) where

import Cardano.Api (File (..), FileDirection (..))
import qualified Cardano.Api.IO as Api
import Cardano.Api.IO.Base (VRFPrivateKeyFilePermissionError (..))

import Control.Monad.Except (runExceptT)

import HaskellWorks.Polysemy
import HaskellWorks.Prelude
import Polysemy ()

checkVrfFilePermissions :: ()
=> Member (Error VRFPrivateKeyFilePermissionError) r
=> Member (Embed IO) r
=> File () In
-> Sem r ()
checkVrfFilePermissions vrfSignKey =
embed (runExceptT (Api.checkVrfFilePermissions vrfSignKey))
& onLeftM throw
89 changes: 89 additions & 0 deletions cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Polysemy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module Test.Cardano.CLI.Polysemy
( cardanoCliPath,
execCardanoCli,
execCardanoCli_,
execDetailCardanoCli,
localWorkspace,
) where

import Polysemy ()
import HaskellWorks.Polysemy
import HaskellWorks.Polysemy.Error.Types
import HaskellWorks.Polysemy.Hedgehog
import HaskellWorks.Polysemy.Hedgehog.Process
import HaskellWorks.Polysemy.Prelude
import HaskellWorks.Polysemy.System.Process

cardanoCliPath :: FilePath
cardanoCliPath = "cardano-cli"

-- | Execute cardano-cli via the command line.
--
-- Waits for the process to finish and returns the stdout.
execCardanoCli :: ()
=> HasCallStack
=> Member (Embed IO) r
=> Member Hedgehog r
=> Member Log r
=> [String]
-- ^ Arguments to the CLI command
-> Sem r String
-- ^ Captured stdout
execCardanoCli args = withFrozenCallStack $
execFlexOk "cardano-cli" "CARDANO_CLI" args
& trapFail @GenericError
& trapFail @IOException

execCardanoCli_ :: ()
=> HasCallStack
=> Member (Embed IO) r
=> Member Hedgehog r
=> Member Log r
=> [String]
-- ^ Arguments to the CLI command
-> Sem r ()
execCardanoCli_ args = withFrozenCallStack $
void $ execCardanoCli args

-- | Execute cardano-cli via the command line, expecting it to fail.
--
-- Waits for the process to finish and returns the exit code, stdout and stderr.
execDetailCardanoCli :: ()
=> HasCallStack
=> Member (Embed IO) r
=> Member Hedgehog r
=> Member Log r
=> [String]
-- ^ Arguments to the CLI command
-> Sem r (ExitCode, String, String)
-- ^ Captured stdout
execDetailCardanoCli arguments = withFrozenCallStack $
execDetailFlex defaultExecConfig "cardano-cli" "CARDANO_CLI" arguments
& trapFail @GenericError
& trapFail @IOException

localWorkspace :: ()
=> Member Hedgehog r
=> Member Log r
=> Member (Embed IO) r
=> Sem
( Reader Workspace
: Reader ProjectRoot
: Reader PackagePath
: Resource
: r)
()
-> Sem r ()
localWorkspace f = do
cabalProjectDir <- findCabalProjectDir "."

f & moduleWorkspace "cardano-cli"
& runReader (ProjectRoot cabalProjectDir)
& runReader (PackagePath "cardano-cli")
& runResource
Loading

0 comments on commit 7805ddc

Please sign in to comment.