1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
module Main (main) where
import Test.Hspec
import System.IO
import System.IO.Silently
import System.Directory
import System.IO.Temp
import Control.Exception
import Control.Monad
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "hSilence" $ do
it "prevents output to a given handle" $ let file = "foo.txt" in do
h <- openFile file ReadWriteMode
hSilence [h] $ do
hPutStrLn h "foo bar baz"
hFlush h
hSeek h AbsoluteSeek 0
hGetContents h `shouldReturn` ""
`finally` removeFile file
describe "capture" $ do
it "captures stdout" $ do
capture (putStr "foo" >> return 23) `shouldReturn` ("foo", 23 :: Int)
describe "hCapture" $ do
forM_ [NoBuffering, LineBuffering, BlockBuffering Nothing] $ \buffering -> do
it ("preserves " ++ show buffering) $ do
withSystemTempFile "silently" $ \_file h -> do
hSetBuffering h buffering
_ <- hCapture [h] (return ())
hGetBuffering h `shouldReturn` buffering
|