git.iko.soy/typeable/pandoc
on 4e12293 help sign in
typeable/pandoc/tests/RunTests.hs
123 lines 5.1 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2-- RunTests.hs - run test suite for pandoc
3-- This script is designed to be run from the tests directory.
4-- It assumes the pandoc executable is in dist/build/pandoc.
6module Main where
7import System.Exit
8import System.IO.UTF8
9import System.IO ( openTempFile, stderr )
10import Prelude hiding ( putStrLn, putStr, readFile )
11import System.Process ( runProcess, waitForProcess )
12import System.FilePath ( (</>), (<.>) )
13import System.Directory
14import System.Exit
15import Text.Printf
16import Diff
18pandocPath :: FilePath
19pandocPath = ".." </> "dist" </> "build" </> "pandoc" </> "pandoc"
21data TestResult = TestPassed
22 | TestError ExitCode
23 | TestFailed [(DI, String)]
24 deriving (Eq)
26instance Show TestResult where
27 show TestPassed = "PASSED"
28 show (TestError ec) = "ERROR " ++ show ec
29 show (TestFailed d) = "FAILED\n" ++ showDiff d
31showDiff :: [(DI, String)] -> String
32showDiff [] = ""
33showDiff ((F, ln) : ds) = "|TEST| " ++ ln ++ "\n" ++ showDiff ds
34showDiff ((S, ln) : ds) = "|NORM| " ++ ln ++ "\n" ++ showDiff ds
35showDiff ((B, _ ) : ds) = showDiff ds
37writerFormats :: [String]
38writerFormats = [ "native"
39 , "html"
40 , "docbook"
41 , "opendocument"
42 , "latex"
43 , "context"
44 , "texinfo"
45 , "man"
46 , "markdown"
47 , "rst"
48 , "mediawiki"
49 , "rtf"
50 ]
52main :: IO ()
53main = do
54 r1s <- mapM runWriterTest writerFormats
55 r2 <- runS5WriterTest "basic" ["-s"] "s5"
56 r3 <- runS5WriterTest "fancy" ["-s","-m","-i"] "s5"
57 r4 <- runS5WriterTest "fragment" [] "html"
58 r5 <- runS5WriterTest "inserts" ["-s", "-H", "insert",
59 "-B", "insert", "-A", "insert", "-c", "main.css"] "html"
60 r6 <- runTest "markdown reader" ["-r", "markdown", "-w", "native", "-s", "-S"]
61 "testsuite.txt" "testsuite.native"
62 r7 <- runTest "markdown reader (tables)" ["-r", "markdown", "-w", "native"]
63 "tables.txt" "tables.native"
64 r7a <- runTest "markdown reader (more)" ["-r", "markdown", "-w", "native"]
65 "markdown-reader-more.txt" "markdown-reader-more.native"
66 r8 <- runTest "rst reader" ["-r", "rst", "-w", "native", "-s", "-S"]
67 "rst-reader.rst" "rst-reader.native"
68 r9 <- runTest "html reader" ["-r", "html", "-w", "native", "-s"]
69 "html-reader.html" "html-reader.native"
70 r10 <- runTest "latex reader" ["-r", "latex", "-w", "native", "-s", "-R"]
71 "latex-reader.latex" "latex-reader.native"
72 r11 <- runTest "native reader" ["-r", "native", "-w", "native", "-s"]
73 "testsuite.native" "testsuite.native"
74 let results = r1s ++ [r2, r3, r4, r5, r6, r7, r7a, r8, r9, r10, r11]
75 if all id results
76 then do
77 putStrLn "\nAll tests passed."
78 exitWith ExitSuccess
79 else do
80 let failures = length $ filter not results
81 putStrLn $ "\n" ++ show failures ++ " tests failed."
82 exitWith (ExitFailure failures)
84-- makes sure file is fully closed after reading
85readFile' :: FilePath -> IO String
86readFile' f = do s <- readFile f
87 return $! (length s `seq` s)
89runWriterTest :: String -> IO Bool
90runWriterTest format = do
91 r1 <- runTest (format ++ " writer") ["-r", "native", "-s", "-w", format] "testsuite.native" ("writer" <.> format)
92 r2 <- runTest (format ++ " writer (tables)") ["-r", "native", "-w", format] "tables.native" ("tables" <.> format)
93 return (r1 && r2)
95runS5WriterTest :: String -> [String] -> String -> IO Bool
96runS5WriterTest modifier opts format = runTest (format ++ " writer (" ++ modifier ++ ")")
97 (["-r", "native", "-w", format] ++ opts) "s5.native" ("s5." ++ modifier <.> "html")
99-- | Run a test, return True if test passed.
100runTest :: String -- ^ Title of test
101 -> [String] -- ^ Options to pass to pandoc
102 -> String -- ^ Input filepath
103 -> FilePath -- ^ Norm (for test results) filepath
104 -> IO Bool
105runTest testname opts inp norm = do
106 (outputPath, hOut) <- openTempFile "" "pandoc-test"
107 let inpPath = inp
108 let normPath = norm
109 -- Note: COLUMNS must be set for markdown table reader
110 ph <- runProcess pandocPath (opts ++ [inpPath]) Nothing (Just [("COLUMNS", "80")]) Nothing (Just hOut) (Just stderr)
111 ec <- waitForProcess ph
112 result <- if ec == ExitSuccess
113 then do
114 -- filter \r so the tests will work on Windows machines
115 outputContents <- readFile' outputPath >>= return . filter (/='\r')
116 normContents <- readFile' normPath >>= return . filter (/='\r')
117 if outputContents == normContents
118 then return TestPassed
119 else return $ TestFailed $ getDiff (lines outputContents) (lines normContents)
120 else return $ TestError ec
121 removeFile outputPath
122 putStrLn $ printf "%-28s ---> %s" testname (show result)
123 return (result == TestPassed)