Skip to content

Commit

Permalink
Move pipes test to cardano-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Feb 8, 2023
1 parent 2d27472 commit 00b1b67
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 111 deletions.
4 changes: 3 additions & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ executable cardano-cli
, transformers-except

test-suite cardano-cli-test
import: project-config
import: project-config, maybe-unix

hs-source-dirs: test
main-is: cardano-cli-test.hs
Expand All @@ -171,6 +171,7 @@ test-suite cardano-cli-test
build-depends: aeson
, bech32 >= 1.1.0
, base16-bytestring
, bytestring
, cardano-api
, cardano-api:gen
, cardano-cli
Expand Down Expand Up @@ -201,6 +202,7 @@ test-suite cardano-cli-test
Test.Cli.Pioneers.Exercise4
Test.Cli.Pioneers.Exercise5
Test.Cli.Pioneers.Exercise6
Test.Cli.Pipes
Test.Cli.Shelley.Run.Query
Test.OptParse

Expand Down
6 changes: 6 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ module Cardano.CLI.Shelley.Run.Read
-- * FileOrPipe
, FileOrPipe
, fileOrPipe
, fileOrPipePath
, fileOrPipeCache
, readFileOrPipe
) where

Expand Down Expand Up @@ -761,6 +763,7 @@ readFileInAnyCardanoEra asThing =
-- from pipes, but at present that's not an issue.
data FileOrPipe = FileOrPipe FilePath (IORef (Maybe LBS.ByteString))


instance Show FileOrPipe where
show (FileOrPipe fp _) = show fp

Expand All @@ -773,6 +776,9 @@ fileOrPipe fp = FileOrPipe fp <$> newIORef Nothing
fileOrPipePath :: FileOrPipe -> FilePath
fileOrPipePath (FileOrPipe fp _) = fp

fileOrPipeCache :: FileOrPipe -> IO (Maybe LBS.ByteString)
fileOrPipeCache (FileOrPipe _ c) = readIORef c

-- | Get the contents of a file or pipe. This function reads the entire
-- contents of the file or pipe, and is blocking.
readFileOrPipe :: FileOrPipe -> IO LBS.ByteString
Expand Down
73 changes: 73 additions & 0 deletions cardano-cli/test/Test/Cli/Pipes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Test.Cli.Pipes
( tests
) where

import Prelude

import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString.Char8 as BSC
import qualified Data.ByteString.Lazy as LBS
import System.IO (hClose, hFlush, hPutStr)
import System.Posix.IO (closeFd, createPipe, fdToHandle)

import Cardano.CLI.Shelley.Run.Read
import Test.OptParse

import Hedgehog (Property, discover, success)
import qualified Hedgehog
import qualified Hedgehog.Extras.Test.Base as H
import Hedgehog.Internal.Property (failWith)


prop_readFromPipe :: Property
prop_readFromPipe =
H.propertyOnce . H.moduleWorkspace "tmp" $ \_ -> do

-- We first test that we can read a filepath
txBodyFile <- noteInputFile "test/data/golden/shelley/tx/txbody"
txBodFileOrPipe <- liftIO $ fileOrPipe txBodyFile
txBodyBs <- liftIO $ readFileOrPipe txBodFileOrPipe

if LBS.null txBodyBs
then failWith Nothing
$ "readFileOrPipe failed to read file: " <> fileOrPipePath txBodFileOrPipe
else do
-- We now test that we can read from a pipe.
-- We first check that the IORef has Nothing
mContents <- liftIO $ fileOrPipeCache txBodFileOrPipe
case mContents of
Just{} -> failWith Nothing "readFileOrPipe has incorrectly populated its IORef with contents read from a filepath."
Nothing -> do
-- We can reuse txBodFileOrPipe because we know the cache (IORef) is empty
let txBodyStr = BSC.unpack $ LBS.toStrict txBodyBs
fromPipeBs <- liftIO $ withPipe txBodyStr
if LBS.null fromPipeBs
then failWith Nothing "readFileOrPipe failed to read from a pipe"
else success

-- | Create a pipe, write some String into it, read its contents and return the contents
withPipe :: String -> IO LBS.ByteString
withPipe contents = do
(readEnd, writeEnd) <- createPipe

writeHandle <- fdToHandle writeEnd

-- Write contents to pipe
hPutStr writeHandle contents
hFlush writeHandle
hClose writeHandle
pipe <- fileOrPipe $ "/dev/fd/" ++ show readEnd

-- Read contents from pipe
readContents <- readFileOrPipe pipe
closeFd readEnd
pure readContents

-- -----------------------------------------------------------------------------

tests :: IO Bool
tests =
Hedgehog.checkParallel $$discover
2 changes: 2 additions & 0 deletions cardano-cli/test/cardano-cli-test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import qualified Test.Cli.Pioneers.Exercise1
import qualified Test.Cli.Pioneers.Exercise2
import qualified Test.Cli.Pioneers.Exercise3
import qualified Test.Cli.Pioneers.Exercise4
import qualified Test.Cli.Pipes
import qualified Test.Cli.Shelley.Run.Query
import qualified Test.Config.Mainnet

Expand All @@ -22,6 +23,7 @@ main =
, Test.Cli.ITN.tests
, Test.Cli.JSON.tests
, Test.Cli.MultiAssetParsing.tests
, Test.Cli.Pipes.tests
, Test.Cli.Pioneers.Exercise1.tests
, Test.Cli.Pioneers.Exercise2.tests
, Test.Cli.Pioneers.Exercise3.tests
Expand Down
7 changes: 1 addition & 6 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ license-files: LICENSE
build-type: Simple


common maybe-unix
if !os(windows)
build-depends: unix

common project-config
default-language: Haskell2010
default-extensions: NoImplicitPrelude
Expand Down Expand Up @@ -106,15 +102,14 @@ executable cardano-testnet
ghc-options: -threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T

test-suite cardano-testnet-tests
import: project-config, maybe-unix
import: project-config

hs-source-dirs: test

main-is: Main.hs

other-modules: Test.Cli.Alonzo.LeadershipSchedule
Test.Cli.Babbage.LeadershipSchedule
Test.Cli.Babbage.Pipes
Test.Cli.Babbage.StakeSnapshot
Test.Cli.KesPeriodInfo
Test.FoldBlocks
Expand Down
2 changes: 0 additions & 2 deletions cardano-testnet/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import qualified Test.Tasty.Ingredients as T
--import qualified Test.Cli.Alonzo.LeadershipSchedule
import qualified Test.Cli.Babbage.LeadershipSchedule
import qualified Test.Cli.Babbage.StakeSnapshot
import qualified Test.Cli.Babbage.Pipes
import qualified Test.Cli.KesPeriodInfo
import qualified Test.FoldBlocks
import qualified Test.Node.Shutdown
Expand All @@ -34,7 +33,6 @@ tests = pure $ T.testGroup "test/Spec.hs"
, T.testGroup "Babbage"
[ H.ignoreOnWindows "leadership-schedule" Test.Cli.Babbage.LeadershipSchedule.hprop_leadershipSchedule
, H.ignoreOnWindows "stake-snapshot" Test.Cli.Babbage.StakeSnapshot.hprop_stakeSnapshot
[ H.ignoreOnMacAndWindows "pipes" Test.Cli.Babbage.Pipes.hprop_pipes
]
-- Ignored on Windows due to <stdout>: commitBuffer: invalid argument (invalid character)
-- as a result of the kes-period-info output to stdout.
Expand Down
102 changes: 0 additions & 102 deletions cardano-testnet/test/Test/Cli/Babbage/Pipes.hs

This file was deleted.

0 comments on commit 00b1b67

Please sign in to comment.