Tests: factor out setupEnvironment in Test.Helpers.

This avoids code duplication between Command and Old.
This commit is contained in:
John MacFarlane
2021-03-19 21:17:13 -07:00
parent 2ca1b20a85
commit dc94601eb5
3 changed files with 26 additions and 25 deletions
+2 -13
View File
@@ -1,4 +1,3 @@
{-# LANGUAGE TupleSections #-}
{- |
Module : Tests.Command
Copyright : © 2006-2021 John MacFarlane
@@ -19,9 +18,8 @@ import qualified Data.ByteString as BS
import qualified Data.Text as T
import Data.List (isSuffixOf)
import System.Directory
import qualified System.Environment as Env
import System.Exit
import System.FilePath (takeDirectory, (</>))
import System.FilePath ((</>))
import System.IO (hPutStr, stderr)
import System.IO.Unsafe (unsafePerformIO)
import System.Process
@@ -38,16 +36,7 @@ execTest :: String -- ^ Path to test executable
-> String -- ^ Input text
-> IO (ExitCode, String) -- ^ Exit code and actual output
execTest testExePath cmd inp = do
mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
mpdd <- Env.lookupEnv "pandoc_datadir"
let env' = ("PATH",takeDirectory testExePath) :
("TMP",".") :
("LANG","en_US.UTF-8") :
("HOME", "./") :
maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
env' <- setupEnvironment testExePath
let pr = (shell (pandocToEmulate True cmd)){ env = Just env' }
(ec, out', err') <- readCreateProcessWithExitCode pr inp
-- filter \r so the tests will work on Windows machines
+23
View File
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE FlexibleInstances #-}
{- |
Module : Tests.Helpers
@@ -13,6 +14,7 @@ Utility functions for the test suite.
-}
module Tests.Helpers ( test
, TestResult(..)
, setupEnvironment
, showDiff
, (=?>)
, purely
@@ -25,6 +27,8 @@ import Data.Algorithm.Diff
import qualified Data.Map as M
import Data.Text (Text, unpack)
import System.Exit
import System.FilePath (takeDirectory)
import qualified System.Environment as Env
import Test.Tasty
import Test.Tasty.HUnit
import Text.Pandoc.Builder (Blocks, Inlines, doc, plain)
@@ -57,6 +61,25 @@ test fn name (input, expected) =
dashes "" = replicate 72 '-'
dashes x = replicate (72 - length x - 5) '-' ++ " " ++ x ++ " ---"
-- | Set up environment for pandoc command tests.
setupEnvironment :: FilePath -> IO [(String, String)]
setupEnvironment testExePath = do
mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
mpdd <- Env.lookupEnv "pandoc_datadir"
-- Note that Cabal sets the pandoc_datadir environment variable
-- to point to the source directory, since otherwise getDataFilename
-- will look in the data directory into which pandoc will be installed
-- (but has not yet been). So when we spawn a new process with
-- pandoc, we need to make sure this environment variable is set.
return $ ("PATH",takeDirectory testExePath) :
("TMP",".") :
("LANG","en_US.UTF-8") :
("HOME", "./") :
maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
data TestResult = TestPassed
| TestError ExitCode
| TestFailed String FilePath [Diff String]
+1 -12
View File
@@ -1,4 +1,3 @@
{-# LANGUAGE TupleSections #-}
{- |
Module : Tests.Old
Copyright : © 2006-2021 John MacFarlane
@@ -15,7 +14,6 @@ module Tests.Old (tests) where
import Data.Algorithm.Diff
import System.Exit
import System.FilePath ((<.>), (</>))
import qualified System.Environment as Env
import System.Environment.Executable (getExecutablePath)
import Text.Pandoc.Process (pipeProcess)
import Test.Tasty (TestTree, testGroup)
@@ -319,16 +317,7 @@ testWithNormalize normalizer pandocPath testname opts inp norm =
(compareValues norm options) updateGolden
where getExpected = normalizer <$> readFile' norm
getActual = do
mldpath <- Env.lookupEnv "LD_LIBRARY_PATH"
mdyldpath <- Env.lookupEnv "DYLD_LIBRARY_PATH"
mpdd <- Env.lookupEnv "pandoc_datadir"
let env = ("TMP",".") :
("LANG","en_US.UTF-8") :
("HOME", "./") :
maybe [] ((:[]) . ("pandoc_datadir",)) mpdd ++
maybe [] ((:[]) . ("LD_LIBRARY_PATH",)) mldpath ++
maybe [] ((:[]) . ("DYLD_LIBRARY_PATH",)) mdyldpath
env <- setupEnvironment pandocPath
(ec, out) <- pipeProcess (Just env) pandocPath
("--emulate":options) mempty
if ec == ExitSuccess