mockery 0.3.3 → 0.3.4
raw patch · 4 files changed
+73/−41 lines, 4 files
Files
- LICENSE +1/−1
- mockery.cabal +3/−3
- src/Test/Mockery/Environment.hs +43/−21
- test/Test/Mockery/EnvironmentSpec.hs +26/−16
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2015 Simon Hengel <sol@typeful.net>+Copyright (c) 2015-2016 Simon Hengel <sol@typeful.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
mockery.cabal view
@@ -1,16 +1,16 @@--- This file has been generated from package.yaml by hpack version 0.11.0.+-- This file has been generated from package.yaml by hpack version 0.15.0. -- -- see: https://github.com/sol/hpack name: mockery-version: 0.3.3+version: 0.3.4 synopsis: Support functions for automated testing description: Support functions for automated testing category: Testing bug-reports: https://github.com/hspec/mockery/issues author: Simon Hengel <sol@typeful.net> maintainer: Simon Hengel <sol@typeful.net>-copyright: (c) 2015 Simon Hengel+copyright: (c) 2015-2016 Simon Hengel license: MIT license-file: LICENSE build-type: Simple
src/Test/Mockery/Environment.hs view
@@ -1,31 +1,53 @@-module Test.Mockery.Environment (withEnvironment) where+module Test.Mockery.Environment (+ withEnvironment+, withModifiedEnvironment+) where import Control.Exception.Base import Control.Monad import System.Environment.Compat -- |--- Run the given action with the specified environment.+-- Run the given action within the specified environment. ----- Before executing the action, `withEnvironment` backs up the current environment,--- clears out the environment, and then applies the supplied 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+--+-- __Note__: The environment is global to a process, so tests that modify 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+withEnvironment environment action = bracketEnvironment $ do+ setEnvironment environment+ action++-- |+-- Run the given action within an augmented environment.+--+-- Before executing the action, `withModifiedEnvironment` backs up the current+-- environment and then augments it with the supplied values.+-- After the action has completed the original environment is restored.+--+-- __Note__: The environment is global to a process, so tests that modify the+-- environment can no longer be run in parallel.+withModifiedEnvironment :: [(String, String)] -> IO a -> IO a+withModifiedEnvironment environment action = bracketEnvironment $ do+ modifyEnvironment environment+ action++bracketEnvironment :: IO a -> IO a+bracketEnvironment = bracket getEnvironment setEnvironment . const++setEnvironment :: [(String, String)] -> IO ()+setEnvironment environment = do+ clearEnvironment+ modifyEnvironment environment++modifyEnvironment :: [(String, String)] -> IO ()+modifyEnvironment environment = forM_ environment $ uncurry setEnv++clearEnvironment :: IO ()+clearEnvironment = do+ environment <- getEnvironment+ forM_ environment (unsetEnv . fst)
test/Test/Mockery/EnvironmentSpec.hs view
@@ -6,19 +6,29 @@ 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"+spec = do+ describe "withEnvironment" $ do+ it "runs the given action within the specified environment" $ do+ withEnvironment [("foo", "bar")] $ do+ getEnvironment `shouldReturn` [("foo", "bar")]++ it "restores the original environment" $ do+ old <- getEnvironment+ withEnvironment [("foo", "bar")] $ do+ return ()+ new <- getEnvironment+ new `shouldMatchList` old++ describe "withModifiedEnvironment" $ do+ it "runs the given action within a modified environment" $ do+ original <- getEnvironment+ withModifiedEnvironment [("foo", "bar")] $ do+ modified <- getEnvironment+ modified `shouldMatchList` original ++ [("foo", "bar")]++ it "restores the original environment" $ do+ old <- getEnvironment+ withModifiedEnvironment [("foo", "bar")] $ do+ return ()+ new <- getEnvironment+ new `shouldMatchList` old