2018-03-18 18:46:28 +01:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2019-02-04 22:52:31 +01:00
|
|
|
{- |
|
|
|
|
Module : Tests.Command
|
2020-03-13 17:52:47 +01:00
|
|
|
Copyright : © 2006-2020 John MacFarlane
|
2019-02-04 22:52:31 +01:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley@edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Run commands, and test results, defined in markdown files.
|
|
|
|
-}
|
2017-02-04 17:38:03 +01:00
|
|
|
module Tests.Command (findPandoc, runTest, tests)
|
|
|
|
where
|
|
|
|
|
2018-03-18 18:46:28 +01:00
|
|
|
import Prelude
|
2017-03-04 13:03:41 +01:00
|
|
|
import Data.Algorithm.Diff
|
2017-10-28 05:28:29 +02:00
|
|
|
import qualified Data.ByteString as BS
|
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884.
+ Use pandoc-types 1.20 and texmath 0.12.
+ Text is now used instead of String, with a few exceptions.
+ In the MediaBag module, some of the types using Strings
were switched to use FilePath instead (not Text).
+ In the Parsing module, new parsers `manyChar`, `many1Char`,
`manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`,
`mantyUntilChar` have been added: these are like their
unsuffixed counterparts but pack some or all of their output.
+ `glob` in Text.Pandoc.Class still takes String since it seems
to be intended as an interface to Glob, which uses strings.
It seems to be used only once in the package, in the EPUB writer,
so that is not hard to change.
2019-11-04 22:12:37 +01:00
|
|
|
import qualified Data.Text as T
|
2017-03-04 13:03:41 +01:00
|
|
|
import Data.List (isSuffixOf)
|
2017-02-04 17:38:03 +01:00
|
|
|
import System.Directory
|
|
|
|
import System.Exit
|
2017-03-04 13:03:41 +01:00
|
|
|
import System.FilePath (joinPath, splitDirectories, takeDirectory, (</>))
|
2017-10-28 05:28:29 +02:00
|
|
|
import System.IO (hPutStr, stderr)
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
2017-03-04 13:03:41 +01:00
|
|
|
import System.Process
|
2017-03-14 17:05:36 +01:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.HUnit
|
2017-03-04 13:03:41 +01:00
|
|
|
import Tests.Helpers
|
2017-02-04 21:07:03 +01:00
|
|
|
import Text.Pandoc
|
2017-03-04 13:03:41 +01:00
|
|
|
import qualified Text.Pandoc.UTF8 as UTF8
|
2017-02-04 17:38:03 +01:00
|
|
|
|
|
|
|
-- | Run a test with normalize function, return True if test passed.
|
2017-02-04 21:07:03 +01:00
|
|
|
runTest :: String -- ^ Title of test
|
2017-08-14 08:59:38 +02:00
|
|
|
-> FilePath -- ^ Path to pandoc
|
2017-02-04 21:07:03 +01:00
|
|
|
-> String -- ^ Shell command
|
2017-02-04 17:38:03 +01:00
|
|
|
-> String -- ^ Input text
|
|
|
|
-> String -- ^ Expected output
|
2017-03-14 17:05:36 +01:00
|
|
|
-> TestTree
|
2017-08-14 08:59:38 +02:00
|
|
|
runTest testname pandocpath cmd inp norm = testCase testname $ do
|
2017-03-04 13:03:41 +01:00
|
|
|
let findDynlibDir [] = Nothing
|
2017-02-04 17:38:03 +01:00
|
|
|
findDynlibDir ("build":xs) = Just $ joinPath (reverse xs) </> "build"
|
2017-03-04 13:03:41 +01:00
|
|
|
findDynlibDir (_:xs) = findDynlibDir xs
|
2017-02-04 21:07:03 +01:00
|
|
|
let mbDynlibDir = findDynlibDir (reverse $ splitDirectories $
|
|
|
|
takeDirectory $ takeWhile (/=' ') cmd)
|
2017-02-04 17:38:03 +01:00
|
|
|
let dynlibEnv = case mbDynlibDir of
|
|
|
|
Nothing -> []
|
|
|
|
Just d -> [("DYLD_LIBRARY_PATH", d),
|
|
|
|
("LD_LIBRARY_PATH", d)]
|
2017-08-14 22:02:34 +02:00
|
|
|
let env' = dynlibEnv ++ [("PATH",takeDirectory pandocpath),("TMP","."),("LANG","en_US.UTF-8"),("HOME", "./"),("pandoc_datadir", "..")]
|
|
|
|
let pr = (shell cmd){ env = Just env' }
|
2017-05-25 11:51:50 +02:00
|
|
|
(ec, out', err') <- readCreateProcessWithExitCode pr inp
|
2017-02-04 17:38:03 +01:00
|
|
|
-- filter \r so the tests will work on Windows machines
|
2017-05-25 11:51:50 +02:00
|
|
|
let out = filter (/= '\r') $ err' ++ out'
|
2017-02-04 17:38:03 +01:00
|
|
|
result <- if ec == ExitSuccess
|
2018-01-20 06:25:24 +01:00
|
|
|
then
|
2017-02-04 17:38:03 +01:00
|
|
|
if out == norm
|
|
|
|
then return TestPassed
|
|
|
|
else return
|
|
|
|
$ TestFailed cmd "expected"
|
|
|
|
$ getDiff (lines out) (lines norm)
|
2017-08-11 20:59:05 +02:00
|
|
|
else do
|
|
|
|
hPutStr stderr err'
|
|
|
|
return $ TestError ec
|
2017-02-04 17:38:03 +01:00
|
|
|
assertBool (show result) (result == TestPassed)
|
|
|
|
|
2019-02-01 05:56:20 +01:00
|
|
|
tests :: FilePath -> TestTree
|
2018-01-20 06:25:24 +01:00
|
|
|
{-# NOINLINE tests #-}
|
2019-02-01 05:56:20 +01:00
|
|
|
tests pandocPath = unsafePerformIO $ do
|
2017-02-04 21:07:03 +01:00
|
|
|
files <- filter (".md" `isSuffixOf`) <$>
|
|
|
|
getDirectoryContents "command"
|
2019-02-01 05:56:20 +01:00
|
|
|
let cmds = map (extractCommandTest pandocPath) files
|
2017-02-04 21:07:03 +01:00
|
|
|
return $ testGroup "Command:" cmds
|
|
|
|
|
|
|
|
isCodeBlock :: Block -> Bool
|
|
|
|
isCodeBlock (CodeBlock _ _) = True
|
2017-03-04 13:03:41 +01:00
|
|
|
isCodeBlock _ = False
|
2017-02-04 21:07:03 +01:00
|
|
|
|
|
|
|
extractCode :: Block -> String
|
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884.
+ Use pandoc-types 1.20 and texmath 0.12.
+ Text is now used instead of String, with a few exceptions.
+ In the MediaBag module, some of the types using Strings
were switched to use FilePath instead (not Text).
+ In the Parsing module, new parsers `manyChar`, `many1Char`,
`manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`,
`mantyUntilChar` have been added: these are like their
unsuffixed counterparts but pack some or all of their output.
+ `glob` in Text.Pandoc.Class still takes String since it seems
to be intended as an interface to Glob, which uses strings.
It seems to be used only once in the package, in the EPUB writer,
so that is not hard to change.
2019-11-04 22:12:37 +01:00
|
|
|
extractCode (CodeBlock _ code) = T.unpack code
|
2017-03-04 13:03:41 +01:00
|
|
|
extractCode _ = ""
|
2017-02-04 21:07:03 +01:00
|
|
|
|
|
|
|
dropPercent :: String -> String
|
|
|
|
dropPercent ('%':xs) = dropWhile (== ' ') xs
|
2017-03-04 13:03:41 +01:00
|
|
|
dropPercent xs = xs
|
2017-02-04 21:07:03 +01:00
|
|
|
|
2020-02-07 08:38:25 +01:00
|
|
|
runCommandTest :: FilePath -> Int -> String -> TestTree
|
|
|
|
runCommandTest pandocpath num code =
|
2017-02-04 21:07:03 +01:00
|
|
|
let codelines = lines code
|
2017-03-14 17:27:30 +01:00
|
|
|
(continuations, r1) = span ("\\" `isSuffixOf`) codelines
|
|
|
|
(cmd, r2) = (dropPercent (unwords (map init continuations ++ take 1 r1)),
|
2017-02-04 21:07:03 +01:00
|
|
|
drop 1 r1)
|
2017-03-14 17:27:30 +01:00
|
|
|
(inplines, r3) = break (=="^D") r2
|
|
|
|
normlines = takeWhile (/=".") (drop 1 r3)
|
|
|
|
input = unlines inplines
|
|
|
|
norm = unlines normlines
|
2017-08-14 08:59:38 +02:00
|
|
|
shcmd = cmd -- trimr $ takeDirectory pandocpath </> cmd
|
|
|
|
in runTest ("#" ++ show num) pandocpath shcmd input norm
|
2017-02-04 21:07:03 +01:00
|
|
|
|
2017-03-14 23:39:28 +01:00
|
|
|
extractCommandTest :: FilePath -> FilePath -> TestTree
|
|
|
|
extractCommandTest pandocpath fp = unsafePerformIO $ do
|
2017-06-11 21:18:42 +02:00
|
|
|
contents <- UTF8.toText <$> BS.readFile ("command" </> fp)
|
2017-02-04 21:07:03 +01:00
|
|
|
Pandoc _ blocks <- runIOorExplode (readMarkdown
|
2017-06-11 21:18:42 +02:00
|
|
|
def{ readerExtensions = pandocExtensions } contents)
|
2018-01-20 06:25:24 +01:00
|
|
|
let codeblocks = map extractCode $ filter isCodeBlock blocks
|
2020-02-07 08:38:25 +01:00
|
|
|
let cases = zipWith (runCommandTest pandocpath) [1..] codeblocks
|
2017-02-04 21:07:03 +01:00
|
|
|
return $ testGroup fp cases
|