Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to supply options programmatically #412

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions core/Test/Tasty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ module Test.Tasty
, sequentialTestGroup
-- * Running tests
, defaultMain
, defaultMainWithOptions
, defaultMainWithIngredients
, defaultMainWithIngredientsAndOptions
, defaultIngredients
, includingOptions
-- * Adjusting and querying options
Expand Down Expand Up @@ -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.
--
Expand Down Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions core/Test/Tasty/CmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Test.Tasty.CmdLine
, suiteOptionParser
, parseOptions
, defaultMainWithIngredients
, defaultMainWithIngredientsAndOptions
) where

import Control.Arrow
Expand Down Expand Up @@ -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."
Expand Down
7 changes: 4 additions & 3 deletions core/Test/Tasty/Runners.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Test.Tasty.Runners
, optionParser
, suiteOptionParser
, defaultMainWithIngredients
, defaultMainWithIngredientsAndOptions
-- * Running tests
, Status(..)
, Result(..)
Expand All @@ -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
Loading