module Test.Mockery.Environment (
withEnvironment
, withModifiedEnvironment
) where
import Control.Exception.Base
import Control.Monad
import System.Environment.Compat
withEnvironment :: [(String, String)] -> IO a -> IO a
withEnvironment environment action = bracketEnvironment $ do
setEnvironment environment
action
withModifiedEnvironment :: [(String, String)] -> IO a -> IO a
withModifiedEnvironment environment action = bracketEnvironment $ do
extendEnvironment environment
action
bracketEnvironment :: IO a -> IO a
bracketEnvironment = bracket getEnvironment setEnvironment . const
setEnvironment :: [(String, String)] -> IO ()
setEnvironment environment = do
clearEnvironment
extendEnvironment environment
extendEnvironment :: [(String, String)] -> IO ()
extendEnvironment environment = forM_ environment $ uncurry setEnv
clearEnvironment :: IO ()
clearEnvironment = do
environment <- getEnvironment
forM_ environment (unsetEnv . fst)