This is an infuriating thing since I have built Hspec-based test suites in which colors all behave normally. But on this project, I cannot get colors to appear when I run all of the test suites at once.
My project.cabal is set up like this:
test-suite unit
type: exitcode-stdio-1.0
main-is: SpecMain.hs
hs-source-dirs: tests/unit
other-modules: WikiSpec
default-language: Haskell2010
ghc-options: -Wall -fno-warn-orphans -threaded
build-depends: base >=4.6
...
test-suite integration
type: exitcode-stdio-1.0
main-is: SpecMain.hs
hs-source-dirs: tests/integration, webapp
other-modules: ApiSpec
default-language: Haskell2010
ghc-options: -Wall -fno-warn-orphans -threaded
build-depends: base >=4.6
...
And then my SpecMain.hs files (identical) contain this:
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
So, when I run stack test
, all of my tests run, but the output is not colorized. If I run stack build --file-watch --test
, the tests run, but if there is any failure at all then all of the output is colored red. Finally, if I run stack test weblog:unit
or stack test weblog:integration
, then the colors end up exactly as they should be. Headers are white, passing tests are green, failing tests are red, and pending tests are yellow.
When I'm doing active development I tend to depend on stack build --file-watch --test
, but I really need the colors to be right.
Have any of you any idea what is going on, how I can fix this, or what additional information I need to provide?
By default, hspec will only use colors when the output is shown on a terminal and when the environment variable TERM
is not "dumb"
(or isn't set). Unless you set an environment variable to "dumb"
, it's likely that there is something going on with the terminal detection.
Either way, stack build
enables you to use arguments for test suites with --test-arguments
, and hspec
interprets several command line arguments, including --color
and --no-color
which overwrite the default behaviour. Therefore, you can force the colors:
stack test --file-watch --test-arguments "--color"
Stack uses the behavior you are seeing when you give it more than one package to test at a time. Typically, this happens because you have more than one location listed in the packages stanza of your stack.yaml
file.
Recent versions of stack mention the following in the auto-generated stack.yaml
file:
# A package marked 'extra-dep: true' will only be built if demanded by a
# non-dependency (i.e. a user package), and its test suites and benchmarks
# will not be run. This is useful for tweaking upstream packages.
If you mark all but one location in the packages stanza as an extra-dep
, stack will revert to its single-package behavior when testing, and show your colorized test results as you expect.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.