-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Capture stdout/stderr/exit code, and replace stdin of your main function.
--   
--   See README.md for detail.
@package main-tester
@version 0.2.0.1

module Test.Main.Internal

-- | Used for the result of <a>captureProcessResult</a>.
data ProcessResult
ProcessResult :: !ByteString -> !ByteString -> !ExitCode -> !Maybe SomeException -> ProcessResult
[prStdout] :: ProcessResult -> !ByteString
[prStderr] :: ProcessResult -> !ByteString
[prExitCode] :: ProcessResult -> !ExitCode
[prException] :: ProcessResult -> !Maybe SomeException

-- | Use to avoid errors in related to new line code in tests. Currently I
--   use this function only for this module's test.
normalizeNewLines :: ProcessResult -> ProcessResult
instance GHC.Generics.Generic Test.Main.Internal.ProcessResult
instance GHC.Show.Show Test.Main.Internal.ProcessResult
instance GHC.Classes.Eq Test.Main.Internal.ProcessResult

module Test.Main

-- | Capture stdout, stderr, and exit code of the given IO action.
--   
--   <pre>
--   &gt;&gt;&gt; let main = putStr "hello"
--   
--   &gt;&gt;&gt; captureProcessResult main
--   ProcessResult {prStdout = "hello", prStderr = "", prExitCode = ExitSuccess, prException = Nothing}
--   </pre>
--   
--   If the IO action exit with error message, the exit code of result is
--   <a>ExitFailure</a>.
--   
--   <pre>
--   &gt;&gt;&gt; import System.IO
--   
--   &gt;&gt;&gt; import System.Exit
--   
--   &gt;&gt;&gt; let main = hPutStr stderr "OMG!" &gt;&gt; exitWith (ExitFailure 1)
--   
--   &gt;&gt;&gt; captureProcessResult main
--   ProcessResult {prStdout = "", prStderr = "OMG!", prExitCode = ExitFailure 1, prException = Nothing}
--   </pre>
--   
--   Since v0.2.0.0, this function catches <tt>SomeException</tt>, not only
--   <tt>ExitCode</tt> to prevent it from losing output when an exception
--   other than <tt>ExitCode</tt> is thrown. To get the thrown error, use
--   <tt>prException</tt>.
--   
--   Note: <a>prStderr</a> doesn't contain the error message of the thrown
--   exception. See the example below.
--   
--   <pre>
--   &gt;&gt;&gt; import Control.Exception
--   
--   &gt;&gt;&gt; let main = ioError $ userError "OMG!"
--   
--   &gt;&gt;&gt; captureProcessResult main
--   ProcessResult {prStdout = "", prStderr = "", prExitCode = ExitFailure 1, prException = Just user error (OMG!)}
--   </pre>
captureProcessResult :: IO () -> IO ProcessResult

-- | Used for the result of <a>captureProcessResult</a>.
data ProcessResult
ProcessResult :: !ByteString -> !ByteString -> !ExitCode -> !Maybe SomeException -> ProcessResult
[prStdout] :: ProcessResult -> !ByteString
[prStderr] :: ProcessResult -> !ByteString
[prExitCode] :: ProcessResult -> !ExitCode
[prException] :: ProcessResult -> !Maybe SomeException

-- | Pass the ByteString to stdin of the given IO action.
--   
--   <pre>
--   &gt;&gt;&gt; import Data.ByteString.Char8 ()
--   
--   &gt;&gt;&gt; :set -XOverloadedStrings
--   
--   &gt;&gt;&gt; let main = putStrLn . reverse =&lt;&lt; getLine
--   
--   &gt;&gt;&gt; withStdin "abcde" main
--   edcba
--   </pre>
withStdin :: ByteString -> IO a -> IO a

-- | Run the given IO action with the specified environment variables set.
--   The environment variables are specified as pairs of
--   <tt>ENV_VAR_NAME</tt> and <tt>ENV_VAR_VALUE</tt>. If
--   <tt>ENV_VAR_VALUE</tt> is <tt>Nothing</tt>, the <tt>ENV_VAR_NAME</tt>
--   is unset with <a>unsetEnv</a>.
--   
--   <pre>
--   &gt;&gt;&gt; import System.Environment
--   
--   &gt;&gt;&gt; setEnv "ENV_VAR_TO_UNSET"    "value_to_unset"
--   
--   &gt;&gt;&gt; setEnv "ENV_VAR_TO_OVERWRITE" "value_to_overwrite"
--   
--   &gt;&gt;&gt; let main = (print =&lt;&lt; lookupEnv "ENV_VAR_TO_UNSET") &gt;&gt; (print =&lt;&lt; lookupEnv "ENV_VAR_TO_OVERWRITE")
--   
--   &gt;&gt;&gt; withEnv [("ENV_VAR_TO_UNSET", Nothing), ("ENV_VAR_TO_OVERWRITE" , Just "new_value")] main
--   Nothing
--   Just "new_value"
--   
--   &gt;&gt;&gt; main
--   Just "value_to_unset"
--   Just "value_to_overwrite"
--   </pre>
withEnv :: [(String, Maybe String)] -> IO a -> IO a

-- | <a>withArgs</a> <tt>args act</tt> - while executing action
--   <tt>act</tt>, have <a>getArgs</a> return <tt>args</tt>.
withArgs :: () => [String] -> IO a -> IO a

-- | Defines the exit codes that a program can return.
data ExitCode

-- | indicates successful termination;
ExitSuccess :: ExitCode

-- | indicates program failure with an exit code. The exact interpretation
--   of the code is operating-system dependent. In particular, some values
--   may be prohibited (e.g. 0 on a POSIX-compliant system).
ExitFailure :: Int -> ExitCode
