git.iko.soy/typeable/pandoc
on master help sign in
Use tasty-golden for golden tests in Old.
commit2235c2a8f78efead12e10f24823b452d86efcb2b authorJohn MacFarlane <jgm@berkeley.edu> dateWed 15 Mar 2017, 00:27 (10 years ago) parent93c49a2
2 files changed+54−39
pandoc.cabal+2 test/Tests/Old.hs+52−39
pandoc.cabal+2
@@ -509,10 +509,12 @@ Test-Suite test-pandoc
509509 filepath >= 1.1 && < 1.5,
510510 process >= 1.2.3 && < 1.5,
511511 skylighting >= 0.3.1 && < 0.4,
512+ temporary >= 1.1 && < 1.3,
512513 Diff >= 0.2 && < 0.4,
513514 tasty >= 0.11 && < 0.12,
514515 tasty-hunit >= 0.9 && < 0.10,
515516 tasty-quickcheck >= 0.8 && < 0.9,
517+ tasty-golden >= 2.3 && < 2.4,
516518 QuickCheck >= 2.4 && < 2.10,
517519 HUnit >= 1.2 && < 1.6,
518520 containers >= 0.1 && < 0.6,
test/Tests/Old.hs+52−39
@@ -2,13 +2,12 @@ module Tests.Old (tests) where
22
33import Data.Algorithm.Diff
44import Prelude hiding (readFile)
5−import System.Directory
65import System.Exit
76import System.FilePath (joinPath, splitDirectories, (<.>), (</>))
8−import System.IO (openTempFile, stderr)
7+import System.IO.Temp (withTempFile)
98import System.Process (runProcess, waitForProcess)
109import Test.Tasty (TestTree, testGroup)
11−import Test.Tasty.HUnit
10+import Test.Tasty.Golden.Advanced (goldenTest)
1211import Tests.Helpers hiding (test)
1312import qualified Text.Pandoc.UTF8 as UTF8
1413
@@ -211,40 +210,54 @@ testWithNormalize :: (String -> String) -- ^ Normalize function for output
211210 -> String -- ^ Input filepath
212211 -> FilePath -- ^ Norm (for test results) filepath
213212 -> TestTree
214−testWithNormalize normalizer testname opts inp norm = testCase testname $ do
215− -- find pandoc executable relative to test-pandoc
216− -- First, try in same directory (e.g. if both in ~/.cabal/bin)
217− -- Second, try ../pandoc (e.g. if in dist/XXX/build/test-pandoc)
213+testWithNormalize normalizer testname opts inp norm =
214+ goldenTest testname getExpected getActual
215+ (compareValues norm options) updateGolden
216+ where getExpected = normalizer <$> readFile' norm
217+ getActual =
218+ withTempFile "." "pandoc-test" $ \outputPath hOut -> do
219+ withTempFile "." "pandoc-test" $ \errorPath hErr -> do
220+ pandocPath <- findPandoc
221+ let mbDynlibDir = findDynlibDir (reverse $
222+ splitDirectories pandocPath)
223+ let dynlibEnv = case mbDynlibDir of
224+ Nothing -> []
225+ Just d -> [("DYLD_LIBRARY_PATH", d),
226+ ("LD_LIBRARY_PATH", d)]
227+ let env = dynlibEnv ++
228+ [("TMP","."),("LANG","en_US.UTF-8"),("HOME", "./")]
229+ ph <- runProcess pandocPath options Nothing
230+ (Just env) Nothing (Just hOut) (Just hErr)
231+ ec <- waitForProcess ph
232+ if ec == ExitSuccess
233+ then
234+ -- filter \r so the tests will work on Windows machines
235+ (filter (/='\r') . normalizer) <$> readFile' outputPath
236+ else do
237+ errcontents <- UTF8.readFile errorPath
238+ fail $ "Pandoc failed with " ++ show ec ++
239+ if null errcontents
240+ then ""
241+ else '\n':errcontents
242+ updateGolden = UTF8.writeFile norm
243+ options = ["--quiet", "--data-dir", ".." </> "data"] ++ [inp] ++ opts
244+
245+compareValues :: FilePath -> [String] -> String -> String -> IO (Maybe String)
246+compareValues norm options expected actual = do
218247 pandocPath <- findPandoc
219− (outputPath, hOut) <- openTempFile "" "pandoc-test"
220− let inpPath = inp
221− let normPath = norm
222− let options = ["--quiet", "--data-dir", ".." </> "data"] ++ [inpPath] ++ opts
223− let cmd = pandocPath ++ " " ++ unwords options
224− let findDynlibDir [] = Nothing
225− findDynlibDir ("build":xs) = Just $ joinPath (reverse xs) </> "build"
226− findDynlibDir (_:xs) = findDynlibDir xs
227− let mbDynlibDir = findDynlibDir (reverse $ splitDirectories pandocPath)
228− let dynlibEnv = case mbDynlibDir of
229− Nothing -> []
230− Just d -> [("DYLD_LIBRARY_PATH", d),
231− ("LD_LIBRARY_PATH", d)]
232− let env = dynlibEnv ++ [("TMP","."),("LANG","en_US.UTF-8"),("HOME", "./")]
233− ph <- runProcess pandocPath options Nothing
234− (Just env) Nothing (Just hOut) (Just stderr)
235− ec <- waitForProcess ph
236− result <- if ec == ExitSuccess
237− then do
238− -- filter \r so the tests will work on Windows machines
239− outputContents <- readFile' outputPath >>=
240− return . filter (/='\r') . normalizer
241− normContents <- readFile' normPath >>=
242− return . filter (/='\r') . normalizer
243− if outputContents == normContents
244− then return TestPassed
245− else return
246− $ TestFailed cmd normPath
247− $ getDiff (lines outputContents) (lines normContents)
248− else return $ TestError ec
249− removeFile outputPath
250− assertBool (show result) (result == TestPassed)
248+ let cmd = pandocPath ++ " " ++ unwords options
249+ let dash = replicate 72 '-'
250+ let diff = getDiff (lines actual) (lines expected)
251+ if expected == actual
252+ then return Nothing
253+ else return $ Just $
254+ '\n' : dash ++
255+ "\n--- " ++ norm ++
256+ "\n+++ " ++ cmd ++ "\n" ++
257+ showDiff (1,1) diff ++ dash
258+
259+findDynlibDir :: [FilePath] -> Maybe FilePath
260+findDynlibDir [] = Nothing
261+findDynlibDir ("build":xs) = Just $ joinPath (reverse xs) </> "build"
262+findDynlibDir (_:xs) = findDynlibDir xs
263+