From acbb4a5e46f01d15ff1b7082d574b624bb22d1b1 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 26 Jul 2012 09:43:27 -0700
Subject: [PATCH] Integrated test suite with cabal.

To run tests, configure with --enable-tests, then 'cabal test'.
You can specify particular tests using --test-options='-t markdown'.

No output is shown unless tests fail.  In the future, we can move
to the detailed-1.0 interface.
---
 Setup.hs             | 15 +--------------
 pandoc.cabal         | 14 ++++----------
 tests/test-pandoc.hs |  3 ++-
 3 files changed, 7 insertions(+), 25 deletions(-)

diff --git a/Setup.hs b/Setup.hs
index a45281367..6d039823e 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -24,8 +24,7 @@ import Data.List ( (\\) )
 main :: IO ()
 main = do
   defaultMainWithHooks $ simpleUserHooks {
-      runTests  = runTestSuite
-    , postBuild = makeManPages 
+      postBuild = makeManPages 
     , postCopy = \ _ flags pkg lbi ->
          installManpages pkg lbi (fromFlag $ copyVerbosity flags)
               (fromFlag $ copyDest flags)
@@ -34,18 +33,6 @@ main = do
     }
   exitWith ExitSuccess
 
--- | Run test suite.
-runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
-runTestSuite args _ pkg lbi = do
-  let testDir = buildDir lbi </> "test-pandoc"
-  testDir' <- canonicalizePath testDir
-  let testArgs = "--timeout=5" : concatMap (\arg -> ["-t",arg]) args
-  if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
-     then inDirectory "tests" $ rawSystem (testDir' </> "test-pandoc") testArgs >>= exitWith
-     else do
-         putStrLn "Build pandoc with the 'tests' flag to run tests"
-         exitWith $ ExitFailure 3
-
 -- | Build man pages from markdown sources in man/
 makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
 makeManPages _ flags _ lbi = do
diff --git a/pandoc.cabal b/pandoc.cabal
index 6499abb4c..23c18c39e 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -188,9 +188,6 @@ Flag executable
 Flag library
   Description:   Build the pandoc library.
   Default:       True
-Flag tests
-  Description:   Build test-pandoc.
-  Default:       False
 Flag blaze_html_0_5
   Description:   Use blaze-html 0.5 and blaze-markup 0.5
   Default:       False
@@ -373,9 +370,10 @@ Executable pandoc
   else
     Buildable:        False
 
-Executable test-pandoc
-  Main-Is:            test-pandoc.hs
-  Hs-Source-Dirs:     tests
+Test-Suite test-pandoc
+  Type:           exitcode-stdio-1.0
+  Main-Is:        test-pandoc.hs
+  Hs-Source-Dirs: tests
   if impl(ghc >= 6.10)
     Build-depends: base >= 4 && < 5, syb >= 0.1 && < 0.4
   else
@@ -414,10 +412,6 @@ Executable test-pandoc
       Ghc-Options:   -O2 -Wall -fno-warn-unused-do-bind -dno-debug-output
     else
       Ghc-Options:   -O2 -Wall
-  if !flag(tests)
-    Buildable:        False
-  else
-    Buildable:        True
   if impl(ghc >= 7)
     cpp-options:    -D_LIT=lit
   else
diff --git a/tests/test-pandoc.hs b/tests/test-pandoc.hs
index 1a8c05e14..968f31df6 100644
--- a/tests/test-pandoc.hs
+++ b/tests/test-pandoc.hs
@@ -14,6 +14,7 @@ import qualified Tests.Writers.HTML
 import qualified Tests.Writers.Native
 import qualified Tests.Writers.Markdown
 import qualified Tests.Shared
+import Text.Pandoc.Shared (inDirectory)
 
 tests :: [Test]
 tests = [ testGroup "Old" Tests.Old.tests
@@ -33,4 +34,4 @@ tests = [ testGroup "Old" Tests.Old.tests
         ]
 
 main :: IO ()
-main = defaultMain tests
+main = inDirectory "tests" $ defaultMain tests