{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module TestContainers.Hspec
(
withContainers
, module Reexports
) where
import Control.Exception (bracket)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Resource (InternalState,
getInternalState)
import Control.Monad.Trans.Resource.Internal (stateAlloc,
stateCleanup)
import Data.Acquire (ReleaseType (ReleaseNormal))
import TestContainers as Reexports
withContainers
:: forall a
. (forall m. MonadDocker m => m a)
-> (a -> IO ())
-> IO ()
withContainers startContainers = dropState $ bracket acquire release
where
acquire :: IO (a, InternalState)
acquire = runResourceT $ do
result <- startContainers
releaseMap <- getInternalState
liftIO $ stateAlloc releaseMap
pure (result, releaseMap)
release :: (a, InternalState) -> IO ()
release (_, internalState) =
stateCleanup ReleaseNormal internalState
dropState :: (((a, b) -> c) -> c) -> (a -> c) -> c
dropState = (. (. fst))