1

I run my HSpec tests both locally and in the CI. The default specdoc formatter produces nice, colored stdio output. However, for the CI, I need the results in the XML format so that they can be presented on the web.

I added my XML format to the HSpec config, but that disabled the stdio output altogether. I've tried hacking the formatter so that it ran both formatting commands, but that just resulted in an XML file with mixed text and XML messages (since there's only one configOutputFile option).

A few options at this point are:

  • To run the tests twice, once with each formatter
  • To run the tests with silent formatter and then somehow try to run the fomatters on the results.
  • To hack my formatter output so that e.g. some commands go straight to stdio.

Neither of those sound particularly easy and straightforward. Is there a better way? Being able to use just one Formatter at once sounds like a rather annoying limitation.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Hm, one idea that just sprung to my mind was to run CI formatter in CI, and stdio formatter in stdio. That sounds like a plausible workaround. – Bartek Banachewicz Jul 19 '18 at 09:59

1 Answers1

1

In the end I've decided that it's not worth the effort, but I did make a working PoC of the workaround:

hspecCi :: String -> Spec -> IO ()
hspecCi filename spec = do
    isCiBuild <- (== "true") <$> getEnv "CI" `catch` \(e :: SomeException) -> return ""

    let ciConfig = defaultConfig
                 { configFormatter = Just xmlFormatter
                 , configOutputFile = Right $ testResultsPath ++ filename ++ "/results.xml"
                 }

    hspecWith (if isCiBuild then ciConfig else defaultConfig) spec

This will run stdio output in local builds and XML in CI. Not too hard to write, but maybe it'll help someone. xmlFormatter is something you need to get from somewhere or write yourself.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135