Skip to content

Commit

Permalink
Add check for binary handles
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jun 14, 2016
1 parent a09ec12 commit 68abdc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions process.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ test-suite test
main-is: main.hs
type: exitcode-stdio-1.0
build-depends: base
, bytestring
, directory
, process
26 changes: 26 additions & 0 deletions test/main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Control.Exception
import Control.Monad (unless)
import System.Exit
import System.IO.Error
import System.Process
import System.IO (hClose, openBinaryTempFile)
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import System.Directory (getTemporaryDirectory, removeFile)

main :: IO ()
main = do
Expand All @@ -27,4 +32,25 @@ main = do
test "create_new_console" $ \cp -> cp { create_new_console = True }
test "new_session" $ \cp -> cp { new_session = True }

putStrLn "Binary handles"
tmpDir <- getTemporaryDirectory
bracket
(openBinaryTempFile tmpDir "process-binary-test.bin")
(\(fp, h) -> hClose h `finally` removeFile fp)
$ \(fp, h) -> do
let bs = S8.pack "hello\nthere\r\nworld\0"
S.hPut h bs
hClose h

(Nothing, Just out, Nothing, ph) <- createProcess (proc "cat" [fp])
{ std_out = CreatePipe
}
res' <- S.hGetContents out
hClose out
ec <- waitForProcess ph
unless (ec == ExitSuccess)
$ error $ "Unexpected exit code " ++ show ec
unless (bs == res')
$ error $ "Unexpected result: " ++ show res'

putStrLn "Tests passed successfully"

0 comments on commit 68abdc2

Please sign in to comment.