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

Allow --pattern|-p option to repeat #380

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Expand Up @@ -69,6 +69,7 @@ jobs:
core-tests/exit-status-tests.sh
# Prevent Git for Windows from replacing slashes with backslashes in patterns
MSYS_NO_PATHCONV=1 core-tests/failing-pattern-test.sh
core-tests/multiple-pattern-test.sh

- name: Test resource-release-test.sh
if: runner.os != 'Windows'
Expand Down
6 changes: 6 additions & 0 deletions core-tests/core-tests.cabal
Expand Up @@ -50,3 +50,9 @@ executable failing-pattern-test
build-depends: base <= 5, tasty, tasty-hunit, random >= 1.2, mtl
default-extensions: ScopedTypeVariables
ghc-options: -Wall -fno-warn-type-defaults

executable multiple-pattern-test
import: commons
main-is: multiple-pattern-test.hs
build-depends: base < 5, tasty, tasty-hunit
ghc-options: -Wall -fno-warn-type-defaults
14 changes: 14 additions & 0 deletions core-tests/multiple-pattern-test.hs
@@ -0,0 +1,14 @@
import Test.Tasty
import Test.Tasty.HUnit

main :: IO ()
main = defaultMain $ testGroup "all"
[ testGroup "red"
[ testCase "square" $ pure ()
, testCase "circle" $ pure ()
]
, testGroup "green"
[ testCase "square" $ pure ()
, testCase "circle" $ pure ()
]
]
28 changes: 28 additions & 0 deletions core-tests/multiple-pattern-test.sh
@@ -0,0 +1,28 @@
#!/bin/sh

set -eux

if ! command -v multiple-pattern-test
then
echo "multiple-pattern-test executable is not in PATH, aborting"
exit 1
fi

[ "$(multiple-pattern-test -l | wc -l)" -eq 4 ]
[ "$(multiple-pattern-test -l -p red | wc -l)" -eq 2 ]
[ "$(multiple-pattern-test -l -p circle | wc -l)" -eq 2 ]
[ "$(multiple-pattern-test -l -p red -p circle | wc -l)" -eq 1 ]
[ "$(multiple-pattern-test -l -p red -p circle -p green | wc -l)" -eq 0 ]

# Edge case: the empty pattern matches everything
[ "$(multiple-pattern-test -l -p '' | wc -l)" -eq 4 ]
[ "$(multiple-pattern-test -l -p '' -p '' | wc -l)" -eq 4 ]
[ "$(multiple-pattern-test -l -p '' -p red | wc -l)" -eq 2 ]
[ "$(multiple-pattern-test -l -p red -p '' | wc -l)" -eq 2 ]
[ "$(multiple-pattern-test -l -p '' -p red -p '' | wc -l)" -eq 2 ]
[ "$(multiple-pattern-test -l -p red -p '' -p circle | wc -l)" -eq 1 ]

# Environment variable is entirely overridden by any command line options
[ "$(TASTY_PATTERN=red.circle multiple-pattern-test -l | wc -l)" -eq 1 ]
[ "$(TASTY_PATTERN=red.circle multiple-pattern-test -l -p square | wc -l)" -eq 2 ]
[ "$(TASTY_PATTERN=red.circle multiple-pattern-test -l -p square -p green | wc -l)" -eq 1 ]
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ _YYYY-MM-DD_
* `PrintTest` constructor now has an extra field used to report progress.
Supply `const (pure ())` as this extra field value if you want to skip progress reporting.
* Progress reporting is no longer ignored.
* The `-p`/`--pattern` option can be specified multiple times; only tests that match all patterns are run.

Version 1.4.3
---------------
Expand Down
11 changes: 9 additions & 2 deletions core/Test/Tasty/Patterns.hs
@@ -1,6 +1,6 @@
-- | Test patterns

{-# LANGUAGE CPP, DeriveDataTypeable #-}
{-# LANGUAGE CPP, DeriveDataTypeable, TypeApplications #-}

module Test.Tasty.Patterns
( TestPattern(..)
Expand All @@ -18,6 +18,9 @@ import Test.Tasty.Patterns.Parser
import Test.Tasty.Patterns.Eval

import Data.Char
import Data.Coerce (coerce)
import Data.List.NonEmpty (nonEmpty)
import Data.Maybe (catMaybes)
import Data.Typeable
import Options.Applicative hiding (Success)
#if !MIN_VERSION_base(4,11,0)
Expand All @@ -39,12 +42,16 @@ newtype TestPattern =
noPattern :: TestPattern
noPattern = TestPattern Nothing

-- | Since tasty-1.5, this option can be specified multiple times on the
-- command line. Only the tests matching all given patterns will be selected.
instance IsOption TestPattern where
defaultValue = noPattern
parseValue = parseTestPattern
optionName = return "pattern"
optionHelp = return "Select only tests which satisfy a pattern or awk expression"
optionCLParser = mkOptionCLParser (short 'p' <> metavar "PATTERN")
optionCLParser =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhendric could you please attach a comment to the instance IsOption TestPattern saying "Since tasty-1.5 this command-line option can be specified multiple times with accumulating effect bla-bla-bla" or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

fmap (TestPattern . fmap (foldr1 And) . nonEmpty . catMaybes . coerce @[TestPattern]) . some $
mkOptionCLParser (short 'p' <> metavar "PATTERN")

-- | @since 1.2
parseExpr :: String -> Maybe Expr
Expand Down