From 605648cbbf03932f04ca7561a02926598b3a7242 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 15 Dec 2010 17:52:35 -0800
Subject: [PATCH] Added 'tests' Cabal flag.

+ This ensures that test-pandoc gets built.
+ 'cabal test' now runs this.
+ The old tests/RunTests.hs has been removed, and
  src/test-pandoc.hs added.
---
 Setup.hs                                | 25 ++++++++++++++--------
 pandoc.cabal                            | 28 ++++++++++++++++++-------
 tests/RunTests.hs => src/test-pandoc.hs |  0
 3 files changed, 37 insertions(+), 16 deletions(-)
 rename tests/RunTests.hs => src/test-pandoc.hs (100%)

diff --git a/Setup.hs b/Setup.hs
index 186a8bb2b..b68435216 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -9,8 +9,8 @@ import Distribution.Verbosity ( Verbosity, silent )
 import Distribution.Simple.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
 import Distribution.Simple.Utils (copyFiles)
 import Control.Exception ( bracket_ )
-import Control.Monad ( unless )
-import System.Process ( runCommand, runProcess, waitForProcess )
+import Control.Monad ( unless, when )
+import System.Process ( rawSystem, runCommand, runProcess, waitForProcess )
 import System.FilePath ( (</>), (<.>) )
 import System.Directory
 import System.IO ( stderr )
@@ -38,13 +38,20 @@ main = do
 
 -- | Run test suite.
 runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
-runTestSuite _ _ pkg _ = do
-  let isHighlightingKate (Dependency (PackageName "highlighting-kate") _) = True
-      isHighlightingKate _ = False
-  let highlightingSupport = any isHighlightingKate $ buildDepends pkg
-  let testArgs = ["lhs" | highlightingSupport]
-  let testCmd  = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
-  inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
+runTestSuite _ _ pkg lbi = do
+  let testDir = buildDir lbi </> "test-pandoc"
+  testDir' <- canonicalizePath testDir
+  if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
+     then do
+         let isHighlightingKate (Dependency (PackageName "highlighting-kate") _) = True
+             isHighlightingKate _ = False
+         let highlightingSupport = any isHighlightingKate $ buildDepends pkg
+         let testArgs = ["lhs" | highlightingSupport]
+         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/man1/.
 makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
diff --git a/pandoc.cabal b/pandoc.cabal
index 727bae405..eb400d1c4 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -74,7 +74,7 @@ Extra-Source-Files:
                  -- code to create pandoc.1 man page
                  MakeManPage.hs,
                  manpage.template,
-                 -- benchmarking
+                 -- benchmarks
                  Benchmark.hs,
                  -- tests
                  tests/bodybg.gif,
@@ -136,8 +136,7 @@ Extra-Source-Files:
                  tests/lhs-test.latex+lhs,
                  tests/lhs-test.html,
                  tests/lhs-test.html+lhs,
-                 tests/lhs-test.fragment.html+lhs,
-                 tests/RunTests.hs
+                 tests/lhs-test.fragment.html+lhs
 Extra-Tmp-Files: man/man1/pandoc.1, man/man1/markdown2pdf.1
 
 Flag threaded
@@ -155,6 +154,12 @@ Flag library
 Flag wrappers
   Description:   Build the wrappers (markdown2pdf).
   Default:       True
+Flag tests
+  Description:   Build test-pandoc.
+  Default:       False
+Flag benchmarks
+  Description:   Build benchmark-pandoc.
+  Default:       False
 
 Library
   -- Note: the following material must be in both Library and Executable stanzas.
@@ -261,9 +266,9 @@ Executable pandoc
 
   Main-Is:            pandoc.hs
   if flag(executable) || flag(wrappers)
-    Buildable:      True
+    Buildable:        True
   else
-    Buildable:      False
+    Buildable:        False
 
 Executable markdown2pdf
   Hs-Source-Dirs:     src
@@ -275,7 +280,16 @@ Executable markdown2pdf
   Ghc-Prof-Options:   -auto-all
   Extensions:         CPP
   if flag(wrappers)
-    Buildable:      True
+    Buildable:        True
   else
-    Buildable:      False
+    Buildable:        False
+
+Executable test-pandoc
+  Hs-Source-Dirs:     src
+  Main-Is:            test-pandoc.hs
+  if !flag(tests)
+    Buildable:        False
+  else
+    Ghc-Options:      -Wall
+    Build-Depends:    base >= 4 && < 5, Diff, MissingH
 
diff --git a/tests/RunTests.hs b/src/test-pandoc.hs
similarity index 100%
rename from tests/RunTests.hs
rename to src/test-pandoc.hs