import Test.Torch import Control.Monad.Trans import Prelude hiding (getContents) import System.IO (hPutStrLn,stderr) import System.IO.Capture (capture,getContents) main = run $ do (out,err) <- liftIO $ capture (putStrLn "foo") [] is out "foo\n" "stdout is captured" ok (null err) "stderr is also captured" (out,err) <- liftIO $ capture (getLine >>= putStrLn >> getLine >>= hPutStrLn stderr) "foo\nbar\n" is out "foo\n" "stdin can be given; stdout is still captured" is err "bar\n" "stdin can be given; stderr is still captured" (_,err) <- liftIO $ capture undefined [] is err "*** Exception: Prelude.undefined\n" "error message in stderr is also captured" -- This getContents is strict. Because lazy getContents don't return -- from action. (out,err) <- liftIO $ capture (getContents >>= putStr) "foobarbaz\nquux\n" is out "foobarbaz\nquux\n" "lazy io also works; stdout captured" ok (null err) "lazy io also works; stderr captured" -- getContents closes stdin, But I don't know why I can't read -- std(out|err) any more. (out,err) <- liftIO $ capture (getLine >>= putStrLn >> getLine >>= hPutStrLn stderr) "foo\nbar\n" isn't out "foo\n" "after lazy reading stdin, stdin is not can be given any more" isn't err "bar\n" "after lazy reading stdin, stdin is not can be given any more" {- (out,err) <- liftIO $ capture (putStrLn "foo") [] is out "foo\n" "but stdout is still captured, after lazy reading" ok (null err) "stderr is still also captured, after lazy reading" -}