git.iko.soy/typeable/pandoc
on master help sign in
Tests: factor out setupEnvironment in Test.Helpers.

This avoids code duplication between Command and Old.

commitdc94601eb5d90d315d8eae3bca57a652da3fa598 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 19 Mar 2021, 21:17 (6 years ago) parent2ca1b20
3 files changed+26−25
test/Tests/Command.hs+2−13 test/Tests/Helpers.hs+23 test/Tests/Old.hs+1−12
test/Tests/Command.hs+2−13
@@ -1,4 +1,3 @@
1−{-# LANGUAGE TupleSections #-}
21{- |
32 Module : Tests.Command
43 Copyright : © 2006-2021 John MacFarlane
@@ -19,9 +18,8 @@ import qualified Data.ByteString as BS
1918import qualified Data.Text as T
2019import Data.List (isSuffixOf)
2120import System.Directory
22−import qualified System.Environment as Env
2321import System.Exit
24−import System.FilePath (takeDirectory, (</>))
22+import System.FilePath ((</>))
2523import System.IO (hPutStr, stderr)
2624import System.IO.Unsafe (unsafePerformIO)
2725import System.Process
@@ -38,16 +36,7 @@ execTest :: String -- ^ Path to test executable
3836 -> String -- ^ Input text
3937 -> IO (ExitCode, String) -- ^ Exit code and actual output
4038execTest testExePath cmd inp = do
41− mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
42− mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
43− mpdd <- Env.lookupEnv "pandoc_datadir"
44− let env' = ("PATH",takeDirectory testExePath) :
45− ("TMP",".") :
46− ("LANG","en_US.UTF-8") :
47− ("HOME", "./") :
48− maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
49− maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
50− maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
39+ env' <- setupEnvironment testExePath
5140 let pr = (shell (pandocToEmulate True cmd)){ env = Just env' }
5241 (ec, out', err') <- readCreateProcessWithExitCode pr inp
5342 -- filter \r so the tests will work on Windows machines
test/Tests/Helpers.hs+23
@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE TupleSections #-}
23{-# LANGUAGE FlexibleInstances #-}
34{- |
45 Module : Tests.Helpers
@@ -13,6 +14,7 @@ Utility functions for the test suite.
1314-}
1415module Tests.Helpers ( test
1516 , TestResult(..)
17+ , setupEnvironment
1618 , showDiff
1719 , (=?>)
1820 , purely
@@ -25,6 +27,8 @@ import Data.Algorithm.Diff
2527import qualified Data.Map as M
2628import Data.Text (Text, unpack)
2729import System.Exit
30+import System.FilePath (takeDirectory)
31+import qualified System.Environment as Env
2832import Test.Tasty
2933import Test.Tasty.HUnit
3034import Text.Pandoc.Builder (Blocks, Inlines, doc, plain)
@@ -57,6 +61,25 @@ test fn name (input, expected) =
5761 dashes "" = replicate 72 '-'
5862 dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
5963
64+-- | Set up environment for pandoc command tests.
65+setupEnvironment :: FilePath -> IO [(String, String)]
66+setupEnvironment testExePath = do
67+ mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
68+ mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
69+ mpdd <- Env.lookupEnv "pandoc_datadir"
70+ -- Note that Cabal sets the pandoc_datadir environment variable
71+ -- to point to the source directory, since otherwise getDataFilename
72+ -- will look in the data directory into which pandoc will be installed
73+ -- (but has not yet been). So when we spawn a new process with
74+ -- pandoc, we need to make sure this environment variable is set.
75+ return $ ("PATH",takeDirectory testExePath) :
76+ ("TMP",".") :
77+ ("LANG","en_US.UTF-8") :
78+ ("HOME", "./") :
79+ maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
80+ maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
81+ maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
82+
6083data TestResult = TestPassed
6184 | TestError ExitCode
6285 | TestFailed String FilePath [Diff String]
test/Tests/Old.hs+1−12
@@ -1,4 +1,3 @@
1−{-# LANGUAGE TupleSections #-}
21{- |
32 Module : Tests.Old
43 Copyright : © 2006-2021 John MacFarlane
@@ -15,7 +14,6 @@ module Tests.Old (tests) where
1514import Data.Algorithm.Diff
1615import System.Exit
1716import System.FilePath ((<.>), (</>))
18−import qualified System.Environment as Env
1917import System.Environment.Executable (getExecutablePath)
2018import Text.Pandoc.Process (pipeProcess)
2119import Test.Tasty (TestTree, testGroup)
@@ -319,16 +317,7 @@ testWithNormalize normalizer pandocPath testname opts inp norm =
319317 (compareValues norm options) updateGolden
320318 where getExpected = normalizer <$> readFile' norm
321319 getActual = do
322− mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
323− mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
324− mpdd <- Env.lookupEnv "pandoc_datadir"
325− let env = ("TMP",".") :
326− ("LANG","en_US.UTF-8") :
327− ("HOME", "./") :
328− maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
329− maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
330− maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
331−
320+ env <- setupEnvironment pandocPath
332321 (ec, out) <- pipeProcess (Just env) pandocPath
333322 ("--emulate":options) mempty
334323 if ec == ExitSuccess