on master help sign in
Use cabal-doctest
commit9bc4de69068678b3f13aabeb2e9f90e8ba14a62c authorOleg Grenrus <oleg.grenrus@iki.fi> dateSun 12 Feb 2017, 22:12 (10 years ago) parent60716b1
4 files changed+73−77
servant/Setup.lhs+32−15 servant/servant.cabal+2−3 servant/test/doctests.hs+39 servant/test/doctests.hsc−59
servant/Setup.lhs+32−15
@@ -1,5 +1,19 @@
11\begin{code}
22{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+module Main (main) where
5+
6+#ifndef MIN_VERSION_cabal_doctest
7+#define MIN_VERSION_cabal_doctest(x,y,z) 0
8+#endif
9+
10+
11+#if MIN_VERSION_cabal_doctest(1,0,0)
12+import Distribution.Extra.Doctest ( defaultMainWithDoctests )
13+#else
14+
15+-- Otherwise we provide a shim
16+
317#ifndef MIN_VERSION_Cabal
418#define MIN_VERSION_Cabal(x,y,z) 0
519#endif
@@ -9,10 +23,10 @@
923#if MIN_VERSION_Cabal(1,24,0)
1024#define InstalledPackageId UnitId
1125#endif
12−module Main (main) where
1326
1427import Control.Monad ( when )
1528import Data.List ( nub )
29+import Data.String ( fromString )
1630import Distribution.Package ( InstalledPackageId )
1731import Distribution.Package ( PackageId, Package (..), packageVersion )
1832import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) , Library (..), BuildInfo (..))
@@ -42,15 +56,8 @@ makeAbsolute p | isAbsolute p = return p
4256 return $ cwd </> p
4357#endif
4458
45−main :: IO ()
46−main = defaultMainWithHooks simpleUserHooks
47− { buildHook = \pkg lbi hooks flags -> do
48− generateBuildModule flags pkg lbi
49− buildHook simpleUserHooks pkg lbi hooks flags
50− }
51−
52−generateBuildModule :: BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
53−generateBuildModule flags pkg lbi = do
59+generateBuildModule :: String -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
60+generateBuildModule testsuiteName flags pkg lbi = do
5461 let verbosity = fromFlag (buildVerbosity flags)
5562 let distPref = fromFlag (buildDistPref flags)
5663
@@ -74,7 +81,7 @@ generateBuildModule flags pkg lbi = do
7481#endif
7582
7683 -- Lib sources and includes
77− iArgs <- mapM (fmap ("-i"++) . makeAbsolute) $ "test" : libAutogenDir : hsSourceDirs libBI
84+ iArgs <- mapM (fmap ("-i"++) . makeAbsolute) $ libAutogenDir : hsSourceDirs libBI
7885 includeArgs <- mapM (fmap ("-I"++) . makeAbsolute) $ includeDirs libBI
7986
8087 -- CPP includes, i.e. include cabal_macros.h
@@ -82,9 +89,7 @@ generateBuildModule flags pkg lbi = do
8289 [ "-include", libAutogenDir ++ "/cabal_macros.h" ]
8390 ++ cppOptions libBI
8491
85− -- Actually we need to check whether testName suite == "doctests"
86− -- pending https://github.com/haskell/cabal/pull/4229 getting into GHC HEAD tree
87− withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == "doctests") $ do
92+ withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == fromString testsuiteName) $ do
8893
8994 -- get and create autogen dir
9095#if MIN_VERSION_Cabal(1,25,0)
@@ -106,7 +111,7 @@ generateBuildModule flags pkg lbi = do
106111 , "flags = " ++ show (iArgs ++ includeArgs ++ dbFlags ++ cppFlags)
107112 , ""
108113 , "module_sources :: [String]"
109− , "module_sources = " ++ show ("Servant.Utils.LinksSpec" : map display module_sources)
114+ , "module_sources = " ++ show (map display module_sources)
110115 ]
111116 where
112117 -- we do this check in Setup, as then doctests don't need to depend on Cabal
@@ -162,4 +167,16 @@ generateBuildModule flags pkg lbi = do
162167testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]
163168testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys
164169
170+defaultMainWithDoctests :: String -> IO ()
171+defaultMainWithDoctests testSuiteName = defaultMainWithHooks simpleUserHooks
172+ { buildHook = \pkg lbi hooks flags -> do
173+ generateBuildModule testSuiteName flags pkg lbi
174+ buildHook simpleUserHooks pkg lbi hooks flags
175+ }
176+
177+#endif
178+
179+main :: IO ()
180+main = defaultMainWithDoctests "doctests"
181+
165182\end{code}
servant/servant.cabal+2−3
@@ -27,7 +27,7 @@ source-repository head
2727
2828custom-setup
2929 setup-depends:
30− Cabal >=1.14, base, filepath, directory
30+ Cabal >=1.14, base, cabal-doctest
3131
3232library
3333 exposed-modules:
@@ -134,7 +134,7 @@ test-suite spec
134134test-suite doctests
135135 build-depends: base
136136 , servant
137− , doctest
137+ , doctest >=0.11.1 && <0.12
138138 , filemanip
139139 , directory
140140 , filepath
@@ -144,5 +144,4 @@ test-suite doctests
144144 buildable: True
145145 default-language: Haskell2010
146146 ghc-options: -Wall -threaded
147− build-tools: hsc2hs
148147 include-dirs: include
servant/test/doctests.hsnew file+39
@@ -0,0 +1,39 @@
1+-----------------------------------------------------------------------------
2+-- |
3+-- Module : Main (doctests)
4+-- Copyright : (C) 2012-14 Edward Kmett
5+-- License : BSD-style (see the file LICENSE)
6+-- Maintainer : Edward Kmett <ekmett@gmail.com>
7+-- Stability : provisional
8+-- Portability : portable
9+--
10+-- This module provides doctests for a project based on the actual versions
11+-- of the packages it was built with. It requires a corresponding Setup.lhs
12+-- to be added to the project
13+-----------------------------------------------------------------------------
14+module Main where
15+
16+import Build_doctests (flags, pkgs, module_sources)
17+import Data.Foldable (traverse_)
18+import Data.List (isPrefixOf, isSuffixOf)
19+import Test.DocTest
20+
21+main :: IO ()
22+main = do
23+ traverse_ putStrLn args
24+ doctest args
25+ where
26+ args =
27+ "-XOverloadedStrings" :
28+ "-XFlexibleInstances" :
29+ "-XMultiParamTypeClasses" :
30+ flags ++ flags' ++ pkgs ++
31+ "Servant.Utils.LinksSpec" :
32+ module_sources
33+
34+ -- HACK: find -i.../src, and change it ot -i.../test
35+ flags'
36+ = map (\x -> take (length x - 4) x ++ "/test")
37+ . filter (\x -> "-i" `isPrefixOf` x && "/src" `isSuffixOf` x)
38+ $ flags
39+
servant/test/doctests.hscdeleted−59
@@ -1,59 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE ForeignFunctionInterface #-}
3−-----------------------------------------------------------------------------
4−-- |
5−-- Module : Main (doctests)
6−-- Copyright : (C) 2012-14 Edward Kmett
7−-- License : BSD-style (see the file LICENSE)
8−-- Maintainer : Edward Kmett <ekmett@gmail.com>
9−-- Stability : provisional
10−-- Portability : portable
11−--
12−-- This module provides doctests for a project based on the actual versions
13−-- of the packages it was built with. It requires a corresponding Setup.lhs
14−-- to be added to the project
15−-----------------------------------------------------------------------------
16−module Main where
17−
18−import Build_doctests (flags, pkgs, module_sources)
19−import Data.Foldable (traverse_)
20−import Test.DocTest
21−
22−##if defined(mingw32_HOST_OS)
23−##if defined(i386_HOST_ARCH)
24−##define USE_CP
25−import Control.Applicative
26−import Control.Exception
27−import Foreign.C.Types
28−foreign import stdcall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool
29−foreign import stdcall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt
30−##elif defined(x86_64_HOST_ARCH)
31−##define USE_CP
32−import Control.Applicative
33−import Control.Exception
34−import Foreign.C.Types
35−foreign import ccall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool
36−foreign import ccall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt
37−##endif
38−##endif
39−
40−-- | Run in a modified codepage where we can print UTF-8 values on Windows.
41−withUnicode :: IO a -> IO a
42−##ifdef USE_CP
43−withUnicode m = do
44− cp <- c_GetConsoleCP
45− (c_SetConsoleCP 65001 >> m) `finally` c_SetConsoleCP cp
46−##else
47−withUnicode m = m
48−##endif
49−
50−main :: IO ()
51−main = withUnicode $ do
52− traverse_ putStrLn args
53− doctest args
54− where
55− args =
56− "-XOverloadedStrings" :
57− "-XFlexibleInstances" :
58− "-XMultiParamTypeClasses" :
59− flags ++ pkgs ++ module_sources