mgechev / revive

🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
https://revive.run
MIT License
4.69k stars 273 forks source link

Error generated when try to compare strings in test which contains color codes #925

Closed mfederowicz closed 8 months ago

mfederowicz commented 8 months ago

Describe the bug When we try to compare strings which contains color codes in output, there are some errors

To Reproduce Steps to reproduce the behavior:

go test
--- FAIL: TestReviveFormat (0.00s)
    core_test.go:63: Expected formatted failures
        '../testdata/if-return.go
          (15, 2)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.  
          (88, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.  
          (95, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.  
          (91, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement                           
          (98, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement                           

         ✖ 5 problems (0 errors) (5 warnings)'
        to contain
        '(91, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement', but it didn't.
FAIL
exit status 1
FAIL    github.com/mgechev/revive/revivelib     0.004s
chavacava commented 8 months ago

Hi @mfederowicz, thanks for reporting the issue. Is not clear to me in which cases tests try to compare strings with color codes.

mfederowicz commented 8 months ago

in TestReviveFormat test there is:

errorMsgs := []string{
        "(91, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement",
        "(98, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement",
        "(15, 2)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
        "(88, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
        "(95, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
    }
    for _, errorMsg := range errorMsgs {
        if !strings.Contains(failures, errorMsg) {
            t.Fatalf("Expected formatted failures\n'%s'\nto contain\n'%s', but it didn't.", failures, errorMsg)
        }
    }

when we try if !strings.Contains(failures, errorMsg) variable failures have output from formatter with color marks and compare

(91, 3) https://revive.run/r#unreachable-code unreachable code after this statement <- against the same string but wrapped with ascii color markers (under console you see colors: green, red, yellow) results with false :(

of course when you run go test ./... in main directory there is no error, error is when you run go test in /revivelib directory

chavacava commented 8 months ago

Closed by #926