module Test.Mockery.Environment (withEnvironment) where
import Control.Exception.Base
import Control.Monad
import System.Environment.Compat
withEnvironment :: [(String, String)] -> IO a -> IO a
withEnvironment environment action = bracket saveEnv restoreEnv $ const action
where
saveEnv :: IO [(String, String)]
saveEnv = do
env <- clearEnv
forM_ environment $ uncurry setEnv
return env
restoreEnv :: [(String, String)] -> IO ()
restoreEnv env = do
_ <- clearEnv
forM_ env $ uncurry setEnv
clearEnv :: IO [(String, String)]
clearEnv = do
env <- getEnvironment
forM_ env (unsetEnv . fst)
return env