haskoin-store-1.16.0: integration/Haskoin/Store/Integration/Temp.hs
-- |
-- Module : Haskoin.Store.Integration.Temp
-- Copyright : (c) Haskoin Developers
-- License : MIT
-- Maintainer : jprupp@protonmail.ch
-- Stability : experimental
-- Portability : POSIX
--
-- Temporary directories for the tests, the benchmark and the stress test:
-- made for an action, and removed with all that is in them when it ends.
--
-- These are the functions of the same name of "System.IO.Temp", with one
-- difference: the directory is removed by the 'bracket' of
-- "Haskoin.Store.Exception", whose cleanup cannot be cut short. A test that
-- is stopped from outside while it removes its database, by a timeout or by
-- a second Ctrl-C, still removes all of it, where the others could leave
-- half of it behind. A database of a test is tens of megabytes, often in
-- memory ('Haskoin.Store.Integration.Podman.withRamDir'), so what is left
-- behind matters.
module Haskoin.Store.Integration.Temp
( withSystemTempDirectory,
withTempDirectory,
)
where
import Control.Exception (IOException, handle)
import Haskoin.Store.Exception (bracket)
import System.Directory (removeDirectoryRecursive)
import System.IO.Temp (createTempDirectory, getCanonicalTemporaryDirectory)
-- | Run an action with a new directory under the temporary directory of
-- the system, and remove it afterwards, however the action ends. The name
-- of the directory starts with the text given.
withSystemTempDirectory :: String -> (FilePath -> IO a) -> IO a
withSystemTempDirectory template action = do
tmp <- getCanonicalTemporaryDirectory
withTempDirectory tmp template action
-- | 'withSystemTempDirectory' for a directory under the one given. A
-- failure to remove the directory is ignored: the result or the error of
-- the action is what matters to the caller.
withTempDirectory :: FilePath -> String -> (FilePath -> IO a) -> IO a
withTempDirectory parent template = bracket (createTempDirectory parent template) remove
where
remove = handle (\(_ :: IOException) -> return ()) . removeDirectoryRecursive