mockery-0.3.5/0000755000000000000000000000000013117776515011373 5ustar0000000000000000mockery-0.3.5/LICENSE0000644000000000000000000000206713117776515012405 0ustar0000000000000000Copyright (c) 2015-2016 Simon Hengel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mockery-0.3.5/mockery.cabal0000644000000000000000000000267213117776515014037 0ustar0000000000000000-- This file has been generated from package.yaml by hpack version 0.18.0. -- -- see: https://github.com/sol/hpack name: mockery version: 0.3.5 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 maintainer: Simon Hengel copyright: (c) 2015-2016 Simon Hengel license: MIT license-file: LICENSE build-type: Simple cabal-version: >= 1.10 source-repository head type: git location: https://github.com/hspec/mockery library hs-source-dirs: src ghc-options: -Wall build-depends: base == 4.* , base-compat , bytestring , temporary , directory , filepath , logging-facade exposed-modules: Test.Mockery.Directory Test.Mockery.Environment Test.Mockery.Logging other-modules: Paths_mockery default-language: Haskell2010 test-suite spec type: exitcode-stdio-1.0 main-is: Spec.hs hs-source-dirs: test ghc-options: -Wall build-depends: base == 4.* , base-compat , bytestring , temporary , directory , filepath , logging-facade , mockery , hspec == 2.* other-modules: Test.Mockery.DirectorySpec Test.Mockery.EnvironmentSpec Test.Mockery.LoggingSpec default-language: Haskell2010 mockery-0.3.5/Setup.lhs0000644000000000000000000000011413117776515013177 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain mockery-0.3.5/test/0000755000000000000000000000000013117776515012352 5ustar0000000000000000mockery-0.3.5/test/Spec.hs0000644000000000000000000000005413117776515013577 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} mockery-0.3.5/test/Test/0000755000000000000000000000000013117776515013271 5ustar0000000000000000mockery-0.3.5/test/Test/Mockery/0000755000000000000000000000000013117776515014702 5ustar0000000000000000mockery-0.3.5/test/Test/Mockery/DirectorySpec.hs0000644000000000000000000000247513117776515020025 0ustar0000000000000000module Test.Mockery.DirectorySpec where import Test.Hspec import System.Directory import System.Environment import Control.Concurrent import Data.Foldable import Test.Mockery.Directory whenTravis :: IO () -> IO () whenTravis action = do env <- getEnvironment forM_ (lookup "TRAVIS" env) (const action) spec :: Spec spec = do describe "withFile" $ do it "creates a temporary file with the given contents" $ do withFile "foo" $ \file -> readFile file `shouldReturn` "foo" describe "touch" $ do around_ inTempDirectory $ do let name = "foo" it "creates an empty file" $ do touch name readFile name `shouldReturn` "" it "creates any missing directories" $ do touch "foo/bar/baz" readFile "foo/bar/baz" `shouldReturn` "" context "when file already exists" $ do before_ (writeFile name "bar") $ do it "updates modification time" $ do t0 <- getModificationTime name threadDelay 10000 whenTravis (threadDelay 1000000) touch name t1 <- getModificationTime name t0 `shouldSatisfy` (< t1) it "does not modify file content" $ do touch name readFile name `shouldReturn` "bar" mockery-0.3.5/test/Test/Mockery/LoggingSpec.hs0000644000000000000000000000223513117776515017441 0ustar0000000000000000{-# LANGUAGE CPP #-} module Test.Mockery.LoggingSpec (spec) where #if !MIN_VERSION_base(4,8,0) import Control.Applicative #endif import Test.Hspec import Data.IORef import System.Logging.Facade.Types import System.Logging.Facade.Sink import System.Logging.Facade as Log import Test.Mockery.Logging removeLocation :: LogRecord -> LogRecord removeLocation r = r{logRecordLocation = Nothing} spec :: Spec spec = describe "captureLogs" $ do let logToIORef :: IORef [LogRecord] -> LogSink logToIORef ref record = modifyIORef ref (record :) it "returns all log messages of an action" $ do (logs, ()) <- captureLogMessages $ do Log.trace "this should be captured" Log.trace "this should be captured next" logs `shouldBe` [ (TRACE, "this should be captured") , (TRACE, "this should be captured next") ] it "restores the original log sink" $ do ref <- newIORef [] setLogSink $ logToIORef ref _ <- captureLogMessages $ Log.trace "this should be captured" Log.trace "this should not be captured" map removeLocation <$> readIORef ref `shouldReturn` [LogRecord TRACE Nothing "this should not be captured"] mockery-0.3.5/test/Test/Mockery/EnvironmentSpec.hs0000644000000000000000000000207213117776515020356 0ustar0000000000000000module Test.Mockery.EnvironmentSpec where import System.Environment.Compat import Test.Hspec import Test.Mockery.Environment spec :: Spec 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 mockery-0.3.5/src/0000755000000000000000000000000013117776515012162 5ustar0000000000000000mockery-0.3.5/src/Test/0000755000000000000000000000000013117776515013101 5ustar0000000000000000mockery-0.3.5/src/Test/Mockery/0000755000000000000000000000000013117776515014512 5ustar0000000000000000mockery-0.3.5/src/Test/Mockery/Directory.hs0000644000000000000000000000351213117776515017013 0ustar0000000000000000module Test.Mockery.Directory ( inTempDirectory , inTempDirectoryNamed , withFile , touch ) where import Control.Exception import Control.Monad import System.Directory import System.FilePath import System.IO.Error import System.IO hiding (withFile) import System.IO.Temp (withSystemTempDirectory, withSystemTempFile) import qualified Data.ByteString as B -- | -- Run given action with the current working directory set to a temporary -- directory. -- -- The directory is created before the action is run. After the action has -- completed the directory is removed and the current working directory is -- restored to its original value. inTempDirectory :: IO a -> IO a inTempDirectory action = withSystemTempDirectory "mockery" $ \path -> do bracket getCurrentDirectory setCurrentDirectory $ \_ -> do setCurrentDirectory path action -- | -- Similar to `inTempDirectory`, but the temporary directory will have the -- specified name. inTempDirectoryNamed :: FilePath -> IO a -> IO a inTempDirectoryNamed name action = inTempDirectory $ do createDirectory name setCurrentDirectory name action -- | -- Create a temporary file with the given contents and execute the given -- action. -- -- The file is removed after the action has completed. withFile :: String -> (FilePath -> IO a) -> IO a withFile input action = withSystemTempFile "mockery" $ \file h -> do hPutStr h input hClose h action file -- | -- Update the modification time of the specified file. -- Create an empty file (including any missing directories in the file path) if -- the file does not exist. touch :: FilePath -> IO () touch file = do createDirectoryIfMissing True (takeDirectory file) c <- catchJust (guard . isDoesNotExistError) (B.readFile file) (const $ return B.empty) B.writeFile file c mockery-0.3.5/src/Test/Mockery/Environment.hs0000644000000000000000000000345113117776515017355 0ustar0000000000000000module Test.Mockery.Environment ( withEnvironment , withModifiedEnvironment ) where import Control.Exception.Base import Control.Monad import System.Environment.Compat -- | -- 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. -- 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. withEnvironment :: [(String, String)] -> IO a -> IO a 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 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) mockery-0.3.5/src/Test/Mockery/Logging.hs0000644000000000000000000000202613117776515016434 0ustar0000000000000000{-# LANGUAGE RecordWildCards #-} module Test.Mockery.Logging ( captureLogMessages , captureLogMessages_ , LogLevel(..) ) where import Control.Exception import Data.IORef.Compat import Prelude () import Prelude.Compat import System.Logging.Facade.Types import System.Logging.Facade.Sink -- | Capture all log messages produced by an IO action. -- Logs are kept in memory. captureLogMessages :: IO a -> IO ([(LogLevel, String)], a) captureLogMessages action = bracket getLogSink setLogSink act where logToRef ref record = atomicModifyIORef' ref $ \logs -> (record : logs, ()) unwrap LogRecord{..} = (logRecordLevel, logRecordMessage) act _ = do ref <- newIORef [] setLogSink $ logToRef ref val <- action logs <- readIORef ref return (unwrap <$> reverse logs, val) -- | Like 'captureLogsMessages', but ignores the result. captureLogMessages_ :: IO a -> IO [(LogLevel, String)] captureLogMessages_ action = fst <$> captureLogMessages action