Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: display ansi codes when an assert-line fails #454

Open
webstech opened this issue Jan 29, 2023 · 4 comments
Open

CI: display ansi codes when an assert-line fails #454

webstech opened this issue Jan 29, 2023 · 4 comments

Comments

@webstech
Copy link
Contributor

During test development, the ansi codes may not always be well understood. The current bats default error display does not help with determining the actual sequence of codes since the drivers are eating the codes. The bats framework uses cat to display the failing line in assertion tests.

What would be useful is to display the ansi codes in the error message. This could be done in a couple of ways to make the escape character not an escape character.

  1. change the generic run printf "%s" "$output" in tests to run printf "%s" "${output//\e/E}". Tests would need to check for E[ instead of \e[.
  2. introduce a mock for the cat command that does the escape character change. The test cases would need to set/unset the mock but the asserts would not change.

Any interest in this? There may be other options but these both work (have been tested) and are simple to implement.

@webstech
Copy link
Contributor Author

A side effect of option 1. is that running with --print-output-on-failure will display the ansi codes on all the output. That could be considered a plus.

@scottchiefbaker
Copy link
Contributor

I do something similar here: https://github.com/so-fancy/diff-so-fancy/blob/master/test/git_ansi_color.pl#L45

Basically we omit the \e character and just show the body of the ANSI escape code. That would be sufficient I think, but I have no idea how to hook that in to bats. I would be open to more discussion or PRs on this. It would definitely help testing.

@webstech
Copy link
Contributor Author

It would definitely help testing.

At the very least we could document option 1. It had not really occurred to me until I had spent (way too much) time on option 2. Maybe under testing.

Option 2 would look like this in a test (the names can be changed to protect the guilty):

    mockcat

    assert_line --index 0 --partial  "${escape}[${ansi_bold};${ansi_ul};${fg_green}m"

    unmockcat

where this is declared separately:

mockcat() {
    cat() {
        local line
        while IFS='' read -r line || [[ -n $line ]]; do
            printf '%s\n' "${line//${escape}/E}"
        done
    }
}

unmockcat() {
    unset -f cat
}

I do something similar here: https://github.com/so-fancy/diff-so-fancy/blob/master/test/git_ansi_color.pl#L45

Both options could be hidden by functions that check an environment variable. Not sure that is needed. When would you not want to see the actual codes in the escape sequence when something fails? That makes me prefer option 2 where the base tests are unchanged.

@scottchiefbaker
Copy link
Contributor

This is a very interesting proposal... Let me think about it and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants