Skip to content

Commit

Permalink
DefaultContextConfig.MaxErrors defaults to 10 (was 1)
Browse files Browse the repository at this point in the history
Following the `go build` errors report pattern, a "Too many errors"
error is now chained to the last error encountered when the maximum
number of errors has been reached (if > 1).

Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Jun 11, 2018
1 parent 495f5cb commit 7b03015
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
40 changes: 26 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,40 @@ type ContextConfig struct {
// MaxErrors is the maximal number of errors to dump in case of Cmp*
// failure.
//
// It defaults to 1 except if the environment variable
// It defaults to 10 except if the environment variable
// TESTDEEP_MAX_ERRORS is set. In this latter case, the
// TESTDEEP_MAX_ERRORS value is converted to an int and used as is.
//
// Setting it to 0 has the same effect as 1.
// Setting it to 0 has the same effect as 1: only the first error
// will be dumped without the "Too many errors" error.
//
// Setting it to a negative number means no limit: all errors
// will be dumped.
MaxErrors int
}

const contextDefaultRootName = "DATA"
const (
contextDefaultRootName = "DATA"
envMaxErrors = "TESTDEEP_MAX_ERRORS"
)

func getMaxErrorsFromEnv() int {
env := os.Getenv(envMaxErrors)
if env != "" {
n, err := strconv.Atoi(env)
if err == nil {
return n
}
}
return 10
}

// DefaultContextConfig is the default configuration used to render
// tests failures. If overridden, new settings will impact all Cmp*
// functions and *T methods (if not specifically configured.)
var DefaultContextConfig = ContextConfig{
RootName: contextDefaultRootName,
MaxErrors: func() (n int) {
n, err := strconv.Atoi(os.Getenv("TESTDEEP_MAX_ERRORS"))
if err != nil || n == 0 {
n = 1
}
return
}(),
RootName: contextDefaultRootName,
MaxErrors: getMaxErrorsFromEnv(),
}

func (c *ContextConfig) sanitize() {
Expand Down Expand Up @@ -79,9 +88,11 @@ type Context struct {
// checked. Can be used to avoid filling Error{} with expensive
// computations.
booleanError bool
// 0 ≤ maxErrors ≤ 1 stops when first error encoutered, else accumulate
// maxErrors > 1 stops when maxErrors'th error encoutered
// < 0 do not stop until comparison ends
// 0 ≤ maxErrors ≤ 1 stops when first error encoutered (without the
// "Too many errors" error);
// maxErrors > 1 stops when maxErrors'th error encoutered (with a
// last "Too many errors" error);
// < 0 do not stop until comparison ends.
maxErrors int
errors *[]*Error
}
Expand Down Expand Up @@ -150,6 +161,7 @@ func (c Context) CollectError(err *Error) *Error {
// Else, accumulate...
*c.errors = append(*c.errors, err)
if c.maxErrors >= 0 && len(*c.errors) >= c.maxErrors {
*c.errors = append(*c.errors, ErrTooManyErrors)
return c.mergeErrors()
}
return nil
Expand Down
27 changes: 27 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package testdeep

import (
"os"
"testing"
)

Expand All @@ -22,6 +23,18 @@ func equalStr(t *testing.T, got, expected string) bool {
return false
}

func equalInt(t *testing.T, got, expected int) bool {
if got == expected {
return true
}

t.Helper()
t.Errorf(`Failed test
got: %d
expected: %d`, got, expected)
return false
}

func TestContext(t *testing.T) {
equalStr(t, NewContext().path, "DATA")
equalStr(t, NewBooleanContext().path, "")
Expand Down Expand Up @@ -83,3 +96,17 @@ func TestContext(t *testing.T) {
t.Errorf("Sanitized empty ContextConfig should be = to DefaultContextConfig")
}
}

func TestGetMaxErrorsFromEnv(t *testing.T) {
oldEnv := os.Getenv(envMaxErrors)
defer func() { os.Setenv(envMaxErrors, oldEnv) }()

os.Setenv(envMaxErrors, "")
equalInt(t, getMaxErrorsFromEnv(), 10)

os.Setenv(envMaxErrors, "aaa")
equalInt(t, getMaxErrorsFromEnv(), 10)

os.Setenv(envMaxErrors, "-8")
equalInt(t, getMaxErrorsFromEnv(), -8)
}
9 changes: 7 additions & 2 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ func TestEqualArray(t *testing.T) {
}, false) {
return
}
if err.Next != nil {
t.Errorf("Only 2 Errors should have occurred")
if err.Next != ErrTooManyErrors {
if err.Next == nil {
t.Error("ErrTooManyErrors should follow the 2 errors")
} else {
t.Errorf("Only 2 Errors should have occurred. Found 3rd: %s",
err.Next)
}
return
}
})
Expand Down
15 changes: 14 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ type Error struct {
Next *Error
}

var booleanError = &Error{}
var (
booleanError = &Error{}

// ErrTooManyErrors is chained to the last error encountered when
// the maximum number of errors has been reached.
ErrTooManyErrors = &Error{
Message: "Too many errors (use TESTDEEP_MAX_ERRORS=-1 to see all)",
}
)

// Error implements error interface.
func (e *Error) Error() string {
Expand Down Expand Up @@ -66,6 +74,11 @@ func (e *Error) Append(buf *bytes.Buffer, prefix string) {
}
}

if e == ErrTooManyErrors {
buf.WriteString(e.Message)
return
}

if pos := strings.Index(e.Message, "%%"); pos >= 0 {
buf.WriteString(e.Message[:pos])
buf.WriteString(e.Context.path)
Expand Down
5 changes: 5 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,9 @@ Originates from following error:
DATA[13].Field: Error message
(int) 888
[under TestDeep operator Operator at file.go:24]`)

//
// ErrTooManyErrors
equalStr(t, ErrTooManyErrors.Error(),
`Too many errors (use TESTDEEP_MAX_ERRORS=-1 to see all)`)
}
4 changes: 2 additions & 2 deletions t_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ type T struct {
//
// If MaxErrors is not set (or set to 0), it is set to
// DefaultContextConfig.MaxErrors which is potentially dependent from
// the TESTDEEP_MAX_ERRORS environment variable. See ContextConfig
// documentation for details.
// the TESTDEEP_MAX_ERRORS environment variable (else defaults to 10.)
// See ContextConfig documentation for details.
func NewT(t *testing.T, config ...ContextConfig) *T {
switch len(config) {
case 0:
Expand Down

0 comments on commit 7b03015

Please sign in to comment.