diff --git a/core/Test/Tasty.hs b/core/Test/Tasty.hs index e34aaa8a..07587365 100644 --- a/core/Test/Tasty.hs +++ b/core/Test/Tasty.hs @@ -36,7 +36,9 @@ module Test.Tasty , sequentialTestGroup -- * Running tests , defaultMain + , defaultMainWithOptions , defaultMainWithIngredients + , defaultMainWithIngredientsAndOptions , defaultIngredients , includingOptions -- * Adjusting and querying options @@ -66,10 +68,10 @@ module Test.Tasty where import Test.Tasty.Core -import Test.Tasty.Runners +import Test.Tasty.Ingredients.Basic import Test.Tasty.Options import Test.Tasty.Options.Core -import Test.Tasty.Ingredients.Basic +import Test.Tasty.Runners -- | List of the default ingredients. This is what 'defaultMain' uses. -- @@ -106,6 +108,36 @@ defaultIngredients = [listingTests, consoleTestReporter] defaultMain :: TestTree -> IO () defaultMain = defaultMainWithIngredients defaultIngredients +-- | Parse the command line arguments and run the tests. +-- +-- When the tests finish, this function calls 'System.Exit.exitWith' with the exit code +-- that indicates whether any tests have failed. Most external systems +-- (stack, cabal, travis-ci, jenkins etc.) rely on the exit code to detect +-- whether the tests pass. If you want to do something else after +-- `defaultMain` returns, you need to catch the exception and then re-throw +-- it. Example: +-- +-- >import Test.Tasty +-- >import Test.Tasty.HUnit +-- >import Test.Tasty.Runners +-- >import System.Exit +-- >import Control.Exception +-- > +-- >test = testCase "Test 1" (2 @?= 3) +-- > +-- >opts = singleOption $ NumThreads 1 +-- > +-- >main = defaultMainWithOptions opts test +-- > `catch` (\e -> do +-- > if e == ExitSuccess +-- > then putStrLn "Yea" +-- > else putStrLn "Nay" +-- > throwIO e) +-- +-- @since 1.5.1 +defaultMainWithOptions :: OptionSet -> TestTree -> IO () +defaultMainWithOptions = defaultMainWithIngredientsAndOptions defaultIngredients + -- | Locally adjust the option value for the given test subtree. -- -- @since 0.1 diff --git a/core/Test/Tasty/CmdLine.hs b/core/Test/Tasty/CmdLine.hs index 55d1da8f..9b7e2d3a 100644 --- a/core/Test/Tasty/CmdLine.hs +++ b/core/Test/Tasty/CmdLine.hs @@ -5,6 +5,7 @@ module Test.Tasty.CmdLine , suiteOptionParser , parseOptions , defaultMainWithIngredients + , defaultMainWithIngredientsAndOptions ) where import Control.Arrow @@ -171,11 +172,24 @@ parseOptions ins tree = do -- -- @since 0.4 defaultMainWithIngredients :: [Ingredient] -> TestTree -> IO () -defaultMainWithIngredients ins testTree = do +defaultMainWithIngredients ins = do + defaultMainWithIngredientsAndOptions ins mempty + +-- | Parse the command line arguments and run the tests using the provided +-- ingredient list and option set. +-- +-- When the tests finish, this function calls 'System.Exit.exitWith' with the exit code +-- that indicates whether any tests have failed. See 'Test.Tasty.defaultMain' for +-- details. +-- +-- @since 1.5.1 +defaultMainWithIngredientsAndOptions :: [Ingredient] -> OptionSet -> TestTree -> IO () +defaultMainWithIngredientsAndOptions ins opts testTree = do installSignalHandlers - opts <- parseOptions ins testTree + parsedOpts <- parseOptions ins testTree + let opts' = opts <> parsedOpts - case tryIngredients ins opts testTree of + case tryIngredients ins opts' testTree of Nothing -> do hPutStrLn stderr "No ingredients agreed to run. Something is wrong either with your ingredient set or the options." diff --git a/core/Test/Tasty/Runners.hs b/core/Test/Tasty/Runners.hs index 87937bf2..5b66fb78 100644 --- a/core/Test/Tasty/Runners.hs +++ b/core/Test/Tasty/Runners.hs @@ -32,6 +32,7 @@ module Test.Tasty.Runners , optionParser , suiteOptionParser , defaultMainWithIngredients + , defaultMainWithIngredientsAndOptions -- * Running tests , Status(..) , Result(..) @@ -54,12 +55,12 @@ module Test.Tasty.Runners ) where +import Test.Tasty.CmdLine import Test.Tasty.Core -import Test.Tasty.Run import Test.Tasty.Ingredients +import Test.Tasty.Ingredients.Basic import Test.Tasty.Options.Core import Test.Tasty.Patterns -import Test.Tasty.CmdLine -import Test.Tasty.Ingredients.Basic +import Test.Tasty.Run import Test.Tasty.Runners.Reducers import Test.Tasty.Runners.Utils