@@ -1,4 +1,5 @@
11{-# LANGUAGE NoImplicitPrelude #-}
2+{-# LANGUAGE TupleSections #-}
23{- |
34 Module : Tests.Command
45 Copyright : © 2006-2021 John MacFarlane
@@ -10,11 +11,12 @@
1011
1112Run commands, and test results, defined in markdown files.
1213-}
13−module Tests.Command (findPandoc, runTest, tests)
14+module Tests.Command (runTest, tests)
1415where
1516
1617import Prelude
1718import Data.Algorithm.Diff
19+import System.Environment.Executable (getExecutablePath)
1820import qualified Data.ByteString as BS
1921import qualified Data.Text as T
2022import Data.List (isSuffixOf, intercalate)
@@ -34,27 +36,21 @@ import Text.Pandoc
3436import qualified Text.Pandoc.UTF8 as UTF8
3537
3638-- | Run a test with and return output.
37−execTest :: FilePath -- ^ Path to pandoc
39+execTest :: String -- ^ Path to test executable
3840 -> String -- ^ Shell command
3941 -> String -- ^ Input text
4042 -> IO (ExitCode, String) -- ^ Exit code and actual output
41−execTest pandocpath cmd inp = do
43+execTest testExePath cmd inp = do
4244 mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
4345 mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
44− let findDynlibDir [] = Nothing
45− findDynlibDir ("build":xs) = Just $ joinPath (reverse xs) </> "build"
46− findDynlibDir (_:xs) = findDynlibDir xs
47− let mbDynlibDir = findDynlibDir (reverse $ splitDirectories $
48− takeDirectory $ takeWhile (/=' ') cmd)
49− let dynlibEnv = [("DYLD_LIBRARY_PATH",
50− intercalate ":" $ catMaybes [mbDynlibDir, mdyldpath])
51− ,("LD_LIBRARY_PATH",
52− intercalate ":" $ catMaybes [mbDynlibDir, mldpath])]
53− let env' = dynlibEnv ++ [("PATH",takeDirectory pandocpath),("TMP","."),
54− ("LANG","en_US.UTF-8"),
55− ("HOME", "./"),
56− ("pandoc_datadir", "..")]
57− let pr = (shell cmd){ env = Just env' }
46+ let env' = ("PATH",takeDirectory testExePath) :
47+ ("TMP",".") :
48+ ("LANG","en_US.UTF-8") :
49+ ("HOME", "./") :
50+ ("pandoc_datadir", "..") :
51+ maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
52+ maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
53+ let pr = (shell (pandocToEmulate True cmd)){ env = Just env' }
5854 (ec, out', err') <- readCreateProcessWithExitCode pr inp
5955 -- filter \r so the tests will work on Windows machines
6056 let out = filter (/= '\r') $ err' ++ out'
@@ -63,15 +59,23 @@ execTest pandocpath cmd inp = do
6359 ExitSuccess -> return ()
6460 return (ec, out)
6561
62+pandocToEmulate :: Bool -> String -> String
63+pandocToEmulate True ('p':'a':'n':'d':'o':'c':cs) =
64+ "test-pandoc --emulate" ++ pandocToEmulate False cs
65+pandocToEmulate False ('|':' ':'p':'a':'n':'d':'o':'c':cs) =
66+ "| " ++ "test-pandoc --emulate" ++ pandocToEmulate False cs
67+pandocToEmulate _ (c:cs) = c : pandocToEmulate False cs
68+pandocToEmulate _ [] = []
69+
6670-- | Run a test, return True if test passed.
67−runTest :: String -- ^ Title of test
68− -> FilePath -- ^ Path to pandoc
71+runTest :: String -- ^ Path to test executable
72+ -> String -- ^ Title of test
6973 -> String -- ^ Shell command
7074 -> String -- ^ Input text
7175 -> String -- ^ Expected output
7276 -> TestTree
73−runTest testname pandocpath cmd inp norm = testCase testname $ do
74− (ec, out) <- execTest pandocpath cmd inp
77+runTest testExePath testname cmd inp norm = testCase testname $ do
78+ (ec, out) <- execTest testExePath cmd inp
7579 result <- if ec == ExitSuccess
7680 then
7781 if out == norm
@@ -82,12 +86,13 @@ runTest testname pandocpath cmd inp norm = testCase testname $ do
8286 else return $ TestError ec
8387 assertBool (show result) (result == TestPassed)
8488
85−tests :: FilePath -> TestTree
89+tests :: TestTree
8690{-# NOINLINE tests #-}
87−tests pandocPath = unsafePerformIO $ do
91+tests = unsafePerformIO $ do
8892 files <- filter (".md" `isSuffixOf`) <$>
8993 getDirectoryContents "command"
90− let cmds = map (extractCommandTest pandocPath) files
94+ testExePath <- getExecutablePath
95+ let cmds = map (extractCommandTest testExePath) files
9196 return $ testGroup "Command:" cmds
9297
9398isCodeBlock :: Block -> Bool
@@ -103,7 +108,7 @@ dropPercent ('%':xs) = dropWhile (== ' ') xs
103108dropPercent xs = xs
104109
105110runCommandTest :: FilePath -> FilePath -> Int -> String -> TestTree
106−runCommandTest pandocpath fp num code =
111+runCommandTest testExePath fp num code =
107112 goldenTest testname getExpected getActual compareValues updateGolden
108113 where
109114 testname = "#" <> show num
@@ -116,7 +121,7 @@ runCommandTest pandocpath fp num code =
116121 input = unlines inplines
117122 norm = unlines normlines
118123 getExpected = return norm
119− getActual = snd <$> execTest pandocpath cmd input
124+ getActual = snd <$> execTest testExePath cmd input
120125 compareValues expected actual
121126 | actual == expected = return Nothing
122127 | otherwise = return $ Just $ "--- test/command/" ++ fp ++ "\n+++ " ++
@@ -132,10 +137,10 @@ runCommandTest pandocpath fp num code =
132137 UTF8.writeFile fp' updated
133138
134139extractCommandTest :: FilePath -> FilePath -> TestTree
135−extractCommandTest pandocpath fp = unsafePerformIO $ do
140+extractCommandTest testExePath fp = unsafePerformIO $ do
136141 contents <- UTF8.toText <$> BS.readFile ("command" </> fp)
137142 Pandoc _ blocks <- runIOorExplode (readMarkdown
138143 def{ readerExtensions = pandocExtensions } contents)
139144 let codeblocks = map extractCode $ filter isCodeBlock blocks
140− let cases = zipWith (runCommandTest pandocpath fp) [1..] codeblocks
145+ let cases = zipWith (runCommandTest testExePath fp) [1..] codeblocks
141146 return $ testGroup fp cases