11{-# LANGUAGE CPP #-}
22module Run (
33 doctest
4+
5+ , Config (.. )
6+ , defaultConfig
7+ , doctestWith
8+
9+ , Summary (.. )
10+ , isSuccess
11+ , evaluateSummary
12+ , doctestWithResult
13+
414#ifdef TEST
5- , doctestWithOptions
6- , Summary
715, expandDirs
816#endif
917) where
1018
1119import Prelude ()
1220import Prelude.Compat
1321
14- import Control.Monad ( when , unless )
22+ import Control.Monad
1523import System.Directory (doesFileExist , doesDirectoryExist , getDirectoryContents )
1624import System.Environment (getEnvironment )
1725import System.Exit (exitFailure , exitSuccess )
@@ -32,6 +40,7 @@ import PackageDBs
3240import Parse
3341import Options
3442import Runner
43+ import Location
3544import qualified Interpreter
3645
3746-- | Run doctest with given list of arguments.
@@ -51,7 +60,7 @@ doctest :: [String] -> IO ()
5160doctest args0 = case parseOptions args0 of
5261 ProxyToGhc args -> rawSystem Interpreter. ghc args >>= E. throwIO
5362 Output s -> putStr s
54- Result (Run warnings args_ magicMode fastMode preserveIt verbose ) -> do
63+ Result (Run warnings magicMode config ) -> do
5564 mapM_ (hPutStrLn stderr) warnings
5665 hFlush stderr
5766
@@ -60,22 +69,14 @@ doctest args0 = case parseOptions args0 of
6069 hPutStrLn stderr " WARNING: GHC does not support --interactive, skipping tests"
6170 exitSuccess
6271
63- args <- case magicMode of
64- False -> return args_
72+ opts <- case magicMode of
73+ False -> return (ghcOptions config)
6574 True -> do
66- expandedArgs <- concat <$> mapM expandDirs args_
75+ expandedArgs <- concat <$> mapM expandDirs (ghcOptions config)
6776 packageDBArgs <- getPackageDBArgs
6877 addDistArgs <- getAddDistArgs
6978 return (addDistArgs $ packageDBArgs ++ expandedArgs)
70-
71- r <- doctestWithOptions fastMode preserveIt verbose args `E.catch` \ e -> do
72- case fromException e of
73- Just (UsageError err) -> do
74- hPutStrLn stderr (" doctest: " ++ err)
75- hPutStrLn stderr " Try `doctest --help' for more information."
76- exitFailure
77- _ -> E. throwIO e
78- when (not $ isSuccess r) exitFailure
79+ doctestWith config{ghcOptions = opts}
7980
8081-- | Expand a reference to a directory to all .hs and .lhs files within it.
8182expandDirs :: String -> IO [String ]
@@ -128,14 +129,26 @@ getAddDistArgs = do
128129 else id ) rest
129130 else return id
130131
132+ doctestWith :: Config -> IO ()
133+ doctestWith = doctestWithResult >=> evaluateSummary
134+
131135isSuccess :: Summary -> Bool
132136isSuccess s = sErrors s == 0 && sFailures s == 0
133137
134- doctestWithOptions :: Bool -> Bool -> Bool -> [String ] -> IO Summary
135- doctestWithOptions fastMode preserveIt verbose args = do
136-
137- -- get examples from Haddock comments
138- modules <- getDocTests args
139-
140- Interpreter. withInterpreter args $ \ repl -> withCP65001 $ do
138+ evaluateSummary :: Summary -> IO ()
139+ evaluateSummary r = when (not $ isSuccess r) exitFailure
140+
141+ doctestWithResult :: Config -> IO Summary
142+ doctestWithResult config = do
143+ (getDocTests (ghcOptions config) >>= runDocTests config) `E.catch` \ e -> do
144+ case fromException e of
145+ Just (UsageError err) -> do
146+ hPutStrLn stderr (" doctest: " ++ err)
147+ hPutStrLn stderr " Try `doctest --help' for more information."
148+ exitFailure
149+ _ -> E. throwIO e
150+
151+ runDocTests :: Config -> [Module [Located DocTest ]] -> IO Summary
152+ runDocTests Config {.. } modules = do
153+ Interpreter. withInterpreter ghcOptions $ \ repl -> withCP65001 $ do
141154 runModules fastMode preserveIt verbose repl modules
0 commit comments