{-# OPTIONS_HADDOCK prune not-home #-}
{-|
Copyright   : (c) 2020-2021 Tim Emiola
SPDX-License-Identifier: BSD3
Maintainer  : Tim Emiola <adetokunbo@users.noreply.github.com >

Simplify use of @tmp-proc@ with @hspec@.

-}
module Test.Hspec.TmpProc
  ( -- * Combinators
    tdescribe

    -- * Re-export
  , module System.TmpProc
  )
where

import           System.TmpProc
import           Test.Hspec


{-| Like 'describe', but makes the specs @pending@ if @docker@ is unavailable. -}
tdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a
tdescribe :: String -> SpecWith a -> SpecWith a
tdescribe String
label SpecWith a
action = do
  Bool
noDocker <- Bool -> Bool
not (Bool -> Bool) -> SpecM a Bool -> SpecM a Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Bool -> SpecM a Bool
forall r a. IO r -> SpecM a r
runIO IO Bool
hasDocker
  if Bool
noDocker then String -> SpecWith a -> SpecWith a
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
tmpPending String
label SpecWith a
action else String -> SpecWith a -> SpecWith a
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
label SpecWith a
action


tmpPending :: HasCallStack => String -> SpecWith a -> SpecWith a
tmpPending :: String -> SpecWith a -> SpecWith a
tmpPending String
label SpecWith a
spec = IO () -> SpecWith a -> SpecWith a
forall a. IO () -> SpecWith a -> SpecWith a
before_ (HasCallStack => String -> IO ()
String -> IO ()
pendingWith String
noDockerMessage) (SpecWith a -> SpecWith a) -> SpecWith a -> SpecWith a
forall a b. (a -> b) -> a -> b
$ String -> SpecWith a -> SpecWith a
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
label SpecWith a
spec


noDockerMessage :: String
noDockerMessage :: String
noDockerMessage = String
"docker could not be detected"