module Test.Tasty.Golden
( goldenVsFile
, goldenVsString
, goldenVsFileDiff
)
where
import Test.Tasty.Providers
import Test.Tasty.Golden.Advanced
import Text.Printf
import qualified Data.ByteString.Lazy as LB
import System.IO
import System.Process
import System.Exit
import Control.Exception
import Control.Monad.Error (liftIO)
goldenVsFile
:: TestName
-> FilePath
-> FilePath
-> IO ()
-> TestTree
goldenVsFile name ref new act =
goldenTest
name
(vgReadFile ref)
(liftIO act >> vgReadFile new)
cmp
upd
where
cmp = simpleCmp $ printf "Files '%s' and '%s' differ" ref new
upd = LB.writeFile ref
goldenVsString
:: TestName
-> FilePath
-> IO LB.ByteString
-> TestTree
goldenVsString name ref act =
goldenTest
name
(vgReadFile ref)
(liftIO act)
cmp
upd
where
cmp x y = simpleCmp msg x y
where
msg = printf "Test output was different from '%s'. It was: %s" ref (show y)
upd = LB.writeFile ref
simpleCmp :: Eq a => String -> a -> a -> IO (Maybe String)
simpleCmp e x y =
return $ if x == y then Nothing else Just e
goldenVsFileDiff
:: TestName
-> (FilePath -> FilePath -> [String])
-> FilePath
-> FilePath
-> IO ()
-> TestTree
goldenVsFileDiff name cmdf ref new act =
goldenTest
name
(return ())
(liftIO act)
cmp
upd
where
cmd = cmdf ref new
cmp _ _ | null cmd = error "goldenVsFileDiff: empty command line"
cmp _ _ = do
(_, Just sout, _, pid) <- createProcess (proc (head cmd) (tail cmd)) { std_out = CreatePipe }
out <- hGetContents sout
evaluate $ length out
r <- waitForProcess pid
return $ case r of
ExitSuccess -> Nothing
_ -> Just out
upd _ = LB.readFile new >>= LB.writeFile ref