module Test.Hspec.Dirstream
( testFiles
, F.extension
, Test.Hspec.Dirstream.hasExtension
) where
import Data.DirStream
import Data.Text (Text)
import qualified Filesystem.Path.CurrentOS as F
import Pipes
import qualified Pipes.Prelude as P
import Pipes.Safe
import System.FilePath
import Test.Hspec
import Test.Hspec.Core.Spec
deriving instance MonadCatch (SpecM a)
deriving instance MonadThrow (SpecM a)
deriving instance MonadMask (SpecM a)
deriving instance MonadIO (SpecM a)
hasExtension :: Text -> F.FilePath -> Bool
hasExtension = flip F.hasExtension
mapS :: (a -> SpecM () ()) -> Proxy () a y' y (SafeT (SpecM ())) r
mapS = P.mapM_ . (lift .)
paths :: MonadSafe m => String -> (F.FilePath -> Bool) -> Producer String m ()
paths dir p = every (childOf path) >-> P.filter p >-> P.map F.encodeString
where path = F.decodeString dir
testFiles :: (Eq a, Show a)
=> FilePath
-> (F.FilePath -> Bool)
-> (String -> Either a String)
-> SpecWith ()
testFiles dir p f = runSafeT $ runEffect $ paths dir p >-> mapS (testFile f)
testFile :: (Eq a, Show a) => (String -> Either a String) -> String -> SpecWith ()
testFile fun f = it f $ do
sample <- readFile f
expected <- readFile (replaceExtension f ".out")
fun sample `shouldBe` Right expected