mockery 0.3.2 → 0.3.3
raw patch · 3 files changed
+61/−2 lines, 3 filesdep +base-compat
Dependencies added: base-compat
Files
- mockery.cabal +6/−2
- src/Test/Mockery/Environment.hs +31/−0
- test/Test/Mockery/EnvironmentSpec.hs +24/−0
mockery.cabal view
@@ -1,9 +1,9 @@--- This file has been generated from package.yaml by hpack version 0.5.4.+-- This file has been generated from package.yaml by hpack version 0.11.0. -- -- see: https://github.com/sol/hpack name: mockery-version: 0.3.2+version: 0.3.3 synopsis: Support functions for automated testing description: Support functions for automated testing category: Testing@@ -26,6 +26,7 @@ ghc-options: -Wall build-depends: base == 4.*+ , base-compat , bytestring , temporary , directory@@ -33,6 +34,7 @@ , logging-facade exposed-modules: Test.Mockery.Directory+ Test.Mockery.Environment Test.Mockery.Logging default-language: Haskell2010 @@ -44,6 +46,7 @@ ghc-options: -Wall build-depends: base == 4.*+ , base-compat , bytestring , temporary , directory@@ -53,5 +56,6 @@ , hspec == 2.* other-modules: Test.Mockery.DirectorySpec+ Test.Mockery.EnvironmentSpec Test.Mockery.LoggingSpec default-language: Haskell2010
+ src/Test/Mockery/Environment.hs view
@@ -0,0 +1,31 @@+module Test.Mockery.Environment (withEnvironment) where++import Control.Exception.Base+import Control.Monad+import System.Environment.Compat++-- |+-- Run the given action with the specified environment.+--+-- Before executing the action, `withEnvironment` backs up the current environment,+-- clears out the environment, and then applies the supplied environment.+-- After the action has completed the original environment is restored.+-- __Note__: The environment is global for a process, so tests that depend on the+-- environment can no longer be run in parallel.+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
+ test/Test/Mockery/EnvironmentSpec.hs view
@@ -0,0 +1,24 @@+module Test.Mockery.EnvironmentSpec where++import System.Environment.Compat+import Test.Hspec++import Test.Mockery.Environment++spec :: Spec+spec = describe "withEnvironment" $ do+ it "hides the environment" $ do+ withEnvironment [("foo", "bar")] $ do+ env <- getEnvironment+ env `shouldMatchList` [("foo", "bar")]+ it "restores the environment" $ do+ oldEnv <- getEnvironment+ withEnvironment [("foo", "bar")] $ do+ return ()+ newEnv <- getEnvironment+ newEnv `shouldMatchList` oldEnv+ it "should allow the environment to be modified" $ do+ withEnvironment [] $ do+ setEnv "foo2" "bar2"+ var <- lookupEnv "foo2"+ var `shouldBe` Just "bar2"