cardano-ledger icon indicating copy to clipboard operation
cardano-ledger copied to clipboard

Show coloured tree-diff output in ImpTests

Open neilmayhew opened this issue 1 year ago • 2 comments

Description

We use tree-diff to show the difference between actual and expected output in tests, and although tree-diff very helpfully colours the insertions and deletions the output is currently entirely red.

There are two main reasons for this:

  1. hspec forces a red colour on every line when it outputs the text of a failure message.

    This can be overcome by adding --no-color to the command-line test options. Unfortunately, this also drops using colour in the progress messages, but in most circumstances that's an acceptable tradeoff. When it's fairly certain that all tests will pass (eg when doing a final test run before pushing) omitting --no-color can be helpful.

    More investigation is needed to determine whether there's a way to defeat hspec's aggressive colourization of failure messages.

  2. The messages are rendered to String in order to become expectation failures, and this throws away all of the text attributes such as colour.

    This can be overcome by changing the method of rendering the Doc AnsiStyle produced by tree-diff so that the escape codes are embedded in the failure message.

    At the same time, it's also helpful to allow our own ImpTest logging functions to accept Doc AnsiStyle instead of String so that coloured text can be output to the log.

This PR makes the changes necessary for (2), and few other quality-of-life improvements related to the use of colour.

Closes #4471

Checklist

  • [x] Commit sequence broadly makes sense and commits have useful messages
  • [ ] New tests are added if needed and existing tests are updated
  • [ ] When applicable, versions are updated in .cabal and CHANGELOG.md files according to the versioning process.
  • [ ] The version bounds in .cabal files for all affected packages are updated. If you change the bounds in a cabal file, that package itself must have a version increase. (See RELEASING.md)
  • [ ] All visible changes are prepended to the latest section of a CHANGELOG.md for the affected packages. New section is never added with the code changes. (See RELEASING.md)
  • [x] Code is formatted with fourmolu (use scripts/fourmolize.sh)
  • [x] Cabal files are formatted (use scripts/cabal-format.sh)
  • [x] hie.yaml has been updated (use scripts/gen-hie.sh)
  • [x] Self-reviewed the diff

neilmayhew avatar Jul 18 '24 15:07 neilmayhew

This is based on the branch from #4440 because otherwise they would conflict with each other. I'm assuming that #4440 will be merged first.

neilmayhew avatar Jul 18 '24 15:07 neilmayhew

We may want to include changing the CI workflow to use --no-color

neilmayhew avatar Jul 18 '24 15:07 neilmayhew