6

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?

Savanni D'Gerinel
  • 2,379
  • 17
  • 27
  • 2
    Hm, cannot reproduce with a variant that doesn't use hspec-discover and Stack 1.0.4. That being said, which shell do you use? Also, have you tried to colorize the output manually with `--test-arguments "--color"`? – Zeta Mar 21 '16 at 07:06
  • That's interesting. `stack test --test-arguments "--color"` works. This will help, but it leaves me mystified about how my current project is different from others. – Savanni D'Gerinel Mar 21 '16 at 15:48

2 Answers2

5

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"
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Well, that's not it. The terminal is set to `xterm-256color`. If I go to another project in the same terminal, `stack test` displays colors exactly as I expect. The difference is that the working project runs stack resolver 3.15 and the broken project runs stack resolver 5.3 – Savanni D'Gerinel Mar 22 '16 at 03:39
  • @SavanniD'Gerinel again, I cannot reproduce that behavior. Can you provide a [mcve]? – Zeta Mar 22 '16 at 05:41
  • This produces colored output indeed, but i whoud like it to be the default when i call stack test. Do you know a way to (maybe put it somehow in the stack.yaml) to archive this? I tried `build: test-arguments: ["--color"]` but it did not work – user3637541 Jan 03 '17 at 18:43
1

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.

Jason Whittle
  • 751
  • 4
  • 23