Skip to content

Commit

Permalink
Show how to rerun individual spec items on test failures (close #205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jul 2, 2018
1 parent a7c7961 commit 83dd0b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.markdown
@@ -1,3 +1,6 @@
## Changes in 2.5.4
- Show how to rerun individual spec items on test failures (see #205)

## Changes in 2.5.3
- Treat character escapes like `\NUL` as single tokens on `--diff` (see #351)
- Allow a `/` at the beginning and at the end of an absolute path that is
Expand Down
3 changes: 3 additions & 0 deletions hspec-core/src/Test/Hspec/Core/Formatters.hs
Expand Up @@ -230,6 +230,9 @@ defaultFailedFormatter = do
(xs, "") -> output xs
(xs, _ : ys) -> output (xs ++ "\n") >> write (indentation ++ " ") >> indented output ys
Error _ e -> withFailColor . indent $ (("uncaught exception: " ++) . formatException) e

writeLine ""
writeLine (" To rerun use: --match " ++ show (joinPath path))
where
indentation = " "
indent message = do
Expand Down
11 changes: 9 additions & 2 deletions hspec-core/src/Test/Hspec/Core/Util.hs
Expand Up @@ -7,6 +7,7 @@ module Test.Hspec.Core.Util (

-- * Working with paths
, Path
, joinPath
, formatRequirement
, filterPredicate

Expand Down Expand Up @@ -67,6 +68,12 @@ lineBreaksAt n input = case words input of
-- It consists of a list of group descriptions and a requirement description.
type Path = ([String], String)

-- |
-- Join a `Path` with slashes. The result will have a leading and a trailing
-- slash.
joinPath :: Path -> String
joinPath (groups, requirement) = "/" ++ intercalate "/" (groups ++ [requirement]) ++ "/"

-- |
-- Try to create a proper English sentence from a path by applying some
-- heuristics.
Expand All @@ -83,11 +90,11 @@ formatRequirement (groups, requirement) = groups_ ++ requirement

-- | A predicate that can be used to filter a spec tree.
filterPredicate :: String -> Path -> Bool
filterPredicate pattern path@(groups, requirement) =
filterPredicate pattern path =
pattern `isInfixOf` plain
|| pattern `isInfixOf` formatted
where
plain = "/" ++ intercalate "/" (groups ++ [requirement]) ++ "/"
plain = joinPath path
formatted = formatRequirement path

-- | The function `formatException` converts an exception to a string.
Expand Down
2 changes: 2 additions & 0 deletions hspec-core/test/Test/Hspec/Core/FormattersSpec.hs
Expand Up @@ -217,6 +217,8 @@ spec = do
, " two"
, " third"
, ""
, " To rerun use: --match \"//\""
, ""
#if __GLASGOW_HASKELL__ == 800
, "WARNING:"
, " Your version of GHC is affected by https://ghc.haskell.org/trac/ghc/ticket/13285."
Expand Down
6 changes: 6 additions & 0 deletions hspec-core/test/Test/Hspec/Core/RunnerSpec.hs
Expand Up @@ -199,6 +199,8 @@ spec = do
, ""
, " 1) foo"
, ""
, " To rerun use: --match \"/foo/\""
, ""
#if __GLASGOW_HASKELL__ == 800
, "WARNING:"
, " Your version of GHC is affected by https://ghc.haskell.org/trac/ghc/ticket/13285."
Expand Down Expand Up @@ -268,6 +270,8 @@ spec = do
, ""
, " 1) bar"
, ""
, " To rerun use: --match \"/bar/\""
, ""
#if __GLASGOW_HASKELL__ == 800
, "WARNING:"
, " Your version of GHC is affected by https://ghc.haskell.org/trac/ghc/ticket/13285."
Expand Down Expand Up @@ -314,6 +318,8 @@ spec = do
, ""
, " 1) foo bar"
, ""
, " To rerun use: --match \"/foo/bar/\""
, ""
#if __GLASGOW_HASKELL__ == 800
, "WARNING:"
, " Your version of GHC is affected by https://ghc.haskell.org/trac/ghc/ticket/13285."
Expand Down

0 comments on commit 83dd0b6

Please sign in to comment.