aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-03-19 21:17:13 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2021-03-19 21:17:13 -0700
commitdc94601eb5d90d315d8eae3bca57a652da3fa598 (patch)
tree8bad8923d2e3deeb1867e38f867932a3d6a65b3b /test
parent2ca1b20a85303dc8656f782f5804afdec215948b (diff)
downloadpandoc-dc94601eb5d90d315d8eae3bca57a652da3fa598.tar.gz
Tests: factor out setupEnvironment in Test.Helpers.
This avoids code duplication between Command and Old.
Diffstat (limited to 'test')
-rw-r--r--test/Tests/Command.hs15
-rw-r--r--test/Tests/Helpers.hs23
-rw-r--r--test/Tests/Old.hs13
3 files changed, 26 insertions, 25 deletions
diff --git a/test/Tests/Command.hs b/test/Tests/Command.hs
index dc0e25dbe..e8863b545 100644
--- a/test/Tests/Command.hs
+++ b/test/Tests/Command.hs
@@ -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
diff --git a/test/Tests/Helpers.hs b/test/Tests/Helpers.hs
index 64c2785ed..a48a5894e 100644
--- a/test/Tests/Helpers.hs
+++ b/test/Tests/Helpers.hs
@@ -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]
diff --git a/test/Tests/Old.hs b/test/Tests/Old.hs
index e890bad0b..c7b164f3c 100644
--- a/test/Tests/Old.hs
+++ b/test/Tests/Old.hs
@@ -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