tasty-golden 2.0.1 → 2.1
raw patch · 2 files changed
+51/−2 lines, 2 filesdep +filepathdep +temporary
Dependencies added: filepath, temporary
Files
- Test/Tasty/Golden.hs +47/−0
- tasty-golden.cabal +4/−2
Test/Tasty/Golden.hs view
@@ -6,6 +6,7 @@ ( goldenVsFile , goldenVsString , goldenVsFileDiff+ , goldenVsStringDiff ) where @@ -14,9 +15,12 @@ import Text.Printf import qualified Data.ByteString.Lazy as LB import System.IO+import System.IO.Temp import System.Process import System.Exit+import System.FilePath import Control.Exception+import Control.Monad -- trick to avoid an explicit dependency on transformers import Control.Monad.Error (liftIO)@@ -98,3 +102,46 @@ _ -> Just out upd _ = LB.readFile new >>= LB.writeFile ref++-- | Same as 'goldenVsString', but invokes an external diff command.+goldenVsStringDiff+ :: TestName -- ^ test name+ -> (FilePath -> FilePath -> [String])+ -- ^ function that constructs the command line to invoke the diff+ -- command.+ --+ -- E.g.+ --+ -- >\ref new -> ["diff", "-u", ref, new]+ -> FilePath -- ^ path to the golden file+ -> IO LB.ByteString -- ^ action that returns a string+ -> TestTree+goldenVsStringDiff name cmdf ref act =+ goldenTest+ name+ (vgReadFile ref)+ (liftIO act)+ cmp+ upd+ where+ template = takeFileName ref <.> "actual"+ cmp _ actBS = withSystemTempFile template $ \tmpFile tmpHandle -> do++ -- Write act output to temporary ("new") file+ LB.hPut tmpHandle actBS >> hFlush tmpHandle++ let cmd = cmdf ref tmpFile++ when (null cmd) $ error "goldenVsFileDiff: empty command line"++ (_, Just sout, _, pid) <- createProcess (proc (head cmd) (tail cmd)) { std_out = CreatePipe }+ -- strictly read the whole output, so that the process can terminate+ out <- hGetContents sout+ _ <- evaluate $ length out++ r <- waitForProcess pid+ return $ case r of+ ExitSuccess -> Nothing+ _ -> Just (printf "Test output was different from '%s'. Output of %s:\n%s" ref (show cmd) out)++ upd = LB.writeFile ref
tasty-golden.cabal view
@@ -1,5 +1,5 @@ name: tasty-golden-version: 2.0.1+version: 2.1 synopsis: Golden tests support for tasty description: This package provides support for «golden testing».@@ -35,4 +35,6 @@ bytestring, process, mtl,- optparse-applicative+ optparse-applicative,+ filepath,+ temporary >= 1.1