Skip to content

Commit

Permalink
colorize diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed May 16, 2021
1 parent c0faa48 commit bb0a422
Show file tree
Hide file tree
Showing 9 changed files with 1,105 additions and 28 deletions.
9 changes: 1 addition & 8 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type APITest struct {
debugEnabled bool
mockResponseDelayEnabled bool
networkingEnabled bool
colorizeErrors bool
networkingHTTPClient *http.Client
reporter ReportFormatter
verifier Verifier
Expand Down Expand Up @@ -130,12 +129,6 @@ func (a *APITest) Debug() *APITest {
return a
}

// ColorizeErrors configures the error reporter to show messages in red. Disabled on windows
func (a *APITest) ColorizeErrors() *APITest {
a.colorizeErrors = true
return a
}

// Report provides a hook to add custom formatting to the output of the test
func (a *APITest) Report(reporter ReportFormatter) *APITest {
a.reporter = reporter
Expand Down Expand Up @@ -807,7 +800,7 @@ func (r *Response) runTest() *http.Response {
}()

if a.verifier == nil {
a.verifier = DefaultVerifier{ColorizeErrors: a.colorizeErrors}
a.verifier = DefaultVerifier{}
}

a.assertMocks()
Expand Down
2 changes: 1 addition & 1 deletion apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestApiTest_ResponseBody(t *testing.T) {
apitest.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"id": "1234", "name": "Andy"}`))
w.WriteHeader(http.StatusOK)
}).ColorizeErrors().
}).
Get("/user/1234").
Expect(t).
Body(`{"id": "1234", "name": "Andy"}`).
Expand Down
13 changes: 2 additions & 11 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ type Verifier interface {
}

// DefaultVerifier is a verifier that uses some code from https://github.com/stretchr/testify to perform assertions
type DefaultVerifier struct {
ColorizeErrors bool
}
type DefaultVerifier struct{}

var _ Verifier = DefaultVerifier{}

Expand Down Expand Up @@ -79,13 +77,6 @@ func (a DefaultVerifier) Equal(t TestingT, expected, actual interface{}, msgAndA
return true
}

func (a DefaultVerifier) colorize(message string) string {
if runtime.GOOS == "windows" || !a.ColorizeErrors {
return message
}
return "\033[31m" + message + "\033[0m"
}

// Fail reports a failure
func (a DefaultVerifier) Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {
content := []labeledContent{
Expand All @@ -105,7 +96,7 @@ func (a DefaultVerifier) Fail(t TestingT, failureMessage string, msgAndArgs ...i
content = append(content, labeledContent{"Messages", message})
}

t.Errorf("\n%s", ""+a.colorize(labeledOutput(content...)))
t.Errorf("\n%s", ""+labeledOutput(content...))

return false
}
Expand Down
27 changes: 27 additions & 0 deletions difflib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2013, Patrick Mezard
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit bb0a422

Please sign in to comment.