module 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
inTempDirectory :: IO a -> IO a
inTempDirectory :: IO a -> IO a
inTempDirectory action :: IO a
action = String -> (String -> IO a) -> IO a
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
String -> (String -> m a) -> m a
withSystemTempDirectory "mockery" ((String -> IO a) -> IO a) -> (String -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \path :: String
path -> do
IO String -> (String -> IO ()) -> (String -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO String
getCurrentDirectory String -> IO ()
setCurrentDirectory ((String -> IO a) -> IO a) -> (String -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \_ -> do
String -> IO ()
setCurrentDirectory String
path
IO a
action
inTempDirectoryNamed :: FilePath -> IO a -> IO a
inTempDirectoryNamed :: String -> IO a -> IO a
inTempDirectoryNamed name :: String
name action :: IO a
action = IO a -> IO a
forall a. IO a -> IO a
inTempDirectory (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
createDirectory String
name
String -> IO ()
setCurrentDirectory String
name
IO a
action
withFile :: String -> (FilePath -> IO a) -> IO a
withFile :: String -> (String -> IO a) -> IO a
withFile input :: String
input action :: String -> IO a
action = String -> (String -> Handle -> IO a) -> IO a
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
String -> (String -> Handle -> m a) -> m a
withSystemTempFile "mockery" ((String -> Handle -> IO a) -> IO a)
-> (String -> Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \file :: String
file h :: Handle
h -> do
Handle -> String -> IO ()
hPutStr Handle
h String
input
Handle -> IO ()
hClose Handle
h
String -> IO a
action String
file
touch :: FilePath -> IO ()
touch :: String -> IO ()
touch file :: String
file = do
Bool -> String -> IO ()
createDirectoryIfMissing Bool
True (String -> String
takeDirectory String
file)
ByteString
c <- (IOError -> Maybe ())
-> IO ByteString -> (() -> IO ByteString) -> IO ByteString
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchJust (Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> (IOError -> Bool) -> IOError -> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError -> Bool
isDoesNotExistError) (String -> IO ByteString
B.readFile String
file) (IO ByteString -> () -> IO ByteString
forall a b. a -> b -> a
const (IO ByteString -> () -> IO ByteString)
-> IO ByteString -> () -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty)
String -> ByteString -> IO ()
B.writeFile String
file ByteString
c