mockery-0.3.2/0000755000000000000000000000000012561267176011370 5ustar0000000000000000mockery-0.3.2/LICENSE0000644000000000000000000000206212561267176012375 0ustar0000000000000000Copyright (c) 2015 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.2/mockery.cabal0000644000000000000000000000245112561267176014027 0ustar0000000000000000-- This file has been generated from package.yaml by hpack version 0.5.4. -- -- see: https://github.com/sol/hpack name: mockery version: 0.3.2 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 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.* , bytestring , temporary , directory , filepath , logging-facade exposed-modules: Test.Mockery.Directory Test.Mockery.Logging 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.* , bytestring , temporary , directory , filepath , logging-facade , mockery , hspec == 2.* other-modules: Test.Mockery.DirectorySpec Test.Mockery.LoggingSpec default-language: Haskell2010 mockery-0.3.2/Setup.lhs0000644000000000000000000000011412561267176013174 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain mockery-0.3.2/test/0000755000000000000000000000000012561267176012347 5ustar0000000000000000mockery-0.3.2/test/Spec.hs0000644000000000000000000000005412561267176013574 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} mockery-0.3.2/test/Test/0000755000000000000000000000000012561267176013266 5ustar0000000000000000mockery-0.3.2/test/Test/Mockery/0000755000000000000000000000000012561267176014677 5ustar0000000000000000mockery-0.3.2/test/Test/Mockery/DirectorySpec.hs0000644000000000000000000000247512561267176020022 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.2/test/Test/Mockery/LoggingSpec.hs0000644000000000000000000000223512561267176017436 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.2/src/0000755000000000000000000000000012561267176012157 5ustar0000000000000000mockery-0.3.2/src/Test/0000755000000000000000000000000012561267176013076 5ustar0000000000000000mockery-0.3.2/src/Test/Mockery/0000755000000000000000000000000012561267176014507 5ustar0000000000000000mockery-0.3.2/src/Test/Mockery/Directory.hs0000644000000000000000000000351212561267176017010 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.2/src/Test/Mockery/Logging.hs0000644000000000000000000000206112561267176016430 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE RecordWildCards #-} module Test.Mockery.Logging ( captureLogMessages , captureLogMessages_ , LogLevel(..) ) where #if !MIN_VERSION_base(4,8,0) import Control.Applicative #endif import Control.Exception import Data.IORef 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