hspec/hspec — pull request #136 by adinapoli (2013-09-01 10:05:35+00:00)
Title: Tentative fix for #135

As promise, here comes the patch.
In first place, sorry if I deliberately went too far in maybe taking some design choices (but after all we are here to discuss them :) )

The idea is pretty simple: I've customised the `Options` record to allow a `[OptDesc (OptResult -> OptResult)]` so that the user can use base's `GetOpt` to inject his own options. This imho has several advantages:

* We reuse a base library, thus we don't rely on third party stuff
* We solve all our problems, because this way the user has full control over his own flags
* I might have some nice real world use.

I've adapted yesterday's code to use the patched code, this is the gist :+1:

``` haskell
options :: [OptDescr (OptResult -> OptResult)]
options = [
Option "e" ["environment"] (ReqArg (const id) "ENVIRONMENT") "Which environment to use"
]

hspec' :: Spec -> IO ()
hspec' = hspecWithUserOptions options

main :: IO ()
main = do
[_, env] <- getArgs
withFreshDB env "news" (hspec' newsSpec)
hspec' $ do
authSpec
cacheSpec
```

What is nice is that we get the description for the flag for free:

```
OPTIONS
--help display this help and exit
-m PATTERN --match=PATTERN only run examples that match given PATTERN
--color colorize the output
--no-color do not colorize the output
-f FORMATTER --format=FORMATTER use a custom formatter; this can be one of:
specdoc
progress
failed-examples
silent
-o FILE --out=FILE write output to a file instead of STDOUT
--depth=N maximum depth of generated test values for
SmallCheck properties
-a N --qc-max-success=N maximum number of successful tests before a
QuickCheck property succeeds
--qc-max-size=N size to use for the biggest test cases
--qc-max-discard=N maximum number of discarded tests per
successful test before giving up
--seed=N used seed for QuickCheck properties
--print-cpu-time include used CPU time in summary
--dry-run pretend that everything passed; don't verify
anything
--fail-fast abort on first failure
-r --rerun rerun all examples that failed in the
previously test run (only works in GHCi)
--maximum-generated-tests=NUMBER how many automated tests something like QuickCheck should try, by default
--html produce HTML output
-v --verbose do not suppress output to stdout when evaluating examples
-e ENVIRONMENT --environment=ENVIRONMENT Which environment to use
```

I've summarised below the most prominent changes, feel free to comment over. Some might be suboptimal, let's crack on them together :)

* Result from `Test.Hspec.Options` has been renamed to `OptResult`. This was because it now needs to be exported, and it was conflicting with `Types.Result`
* The help now shows also the `undocumentedOptions`. Decide whether this is good or not
* Options now needs to be exposed, unless we want to keep it private but put the burden on the user which has to import `System.Console.GetOpt`. I don't mind which of the two we choose.
* Some refactoring, including outsourcing some long string into proper let bindings and some miscellanea refactoring, as suggested by the linter.

Should be all!
Ah!

1 Test is currently failing, but I can't figure out if it's my patch, but it seems marginal anyway:

```
1) Test.Hspec.Runner.hspec, with --help, constrains lines to 80 characters
predicate failed on: ["Usage: spec [OPTION]...","","OPTIONS"," --help display this help and exit","
-m PATTERN --match=PATTERN only run examples that match given PATTERN"," --color
colorize the output"," --no-color do not colorize the output"," -f FORMATTER --format=FORMATTER
use a custom formatter; this can be one of:"," specdoc","
progress"," failed-examples","
silent"," -o FILE --out=FILE write output to a file instead of STDOUT","
--depth=N maximum depth of generated test values for"," SmallC
heck properties"," -a N --qc-max-success=N maximum number of successful tests before a","
QuickCheck property succeeds"," --qc-max-size=N size to use for the biggest test case
s"," --qc-max-discard=N maximum number of discarded tests per","
successful test before giving up"," --seed=N used seed for QuickCheck properties","
--print-cpu-time include used CPU time in summary"," --dry-run pretend that everythi
ng passed; don't verify"," anything"," --fail-fast abor
t on first failure"," -r --rerun rerun all examples that failed in the","
previously test run (only works in GHCi)"," --maximum-generated-tests=NUMBER how many automated tests som
ething like QuickCheck should try, by default"," --html produce HTML output"," -v --ver
bose do not suppress output to stdout when evaluating examples"]

Randomized with seed 4611685480699978188
```