-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
85 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.