Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bisakh Mondal <bisakhmondal00@gmail.com>
- Loading branch information
1 parent
093fe35
commit 6fae087
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package errors | ||
|
||
import ( | ||
stderrors "errors" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/thanos-io/thanos/pkg/testutil" | ||
) | ||
|
||
const msg = "test_error_message" | ||
const wrapper = "test_wrapper" | ||
|
||
func TestNew(t *testing.T) { | ||
err := New(msg) | ||
testutil.Equals(t, err.Error(), msg, "the root error message must match") | ||
|
||
reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNew .*\/thanos\/pkg\/errors\/errors_test\.go:17`) | ||
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match") | ||
} | ||
|
||
func TestNewFormatted(t *testing.T) { | ||
fmtMsg := msg + " key=%v" | ||
expectedMsg := msg + " key=value" | ||
|
||
err := New(fmtMsg, "value") | ||
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") | ||
reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewFormatted .*\/thanos\/pkg\/errors\/errors_test\.go:28`) | ||
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match") | ||
} | ||
|
||
func TestWrap(t *testing.T) { | ||
err := New(msg) | ||
err = Wrap(err, wrapper) | ||
|
||
expectedMsg := wrapper + ": " + msg | ||
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") | ||
|
||
reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:36 | ||
[[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:35`) | ||
|
||
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match") | ||
} | ||
|
||
func TestUnwrap(t *testing.T) { | ||
// test with base error | ||
err := New(msg) | ||
|
||
for i, tc := range []struct { | ||
err error | ||
expected string | ||
isNil bool | ||
}{ | ||
{ | ||
// no wrapping | ||
err: err, | ||
isNil: true, | ||
}, | ||
{ | ||
err: Wrap(err, wrapper), | ||
expected: "test_error_message", | ||
}, | ||
{ | ||
err: Wrap(Wrap(err, wrapper), wrapper), | ||
expected: "test_wrapper: test_error_message", | ||
}, | ||
// check primitives errors | ||
{ | ||
err: stderrors.New("std-error"), | ||
isNil: true, | ||
}, | ||
{ | ||
err: Wrap(stderrors.New("std-error"), wrapper), | ||
expected: "std-error", | ||
}, | ||
{ | ||
err: nil, | ||
isNil: true, | ||
}, | ||
} { | ||
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) { | ||
unwrapped := Unwrap(tc.err) | ||
if tc.isNil { | ||
testutil.Equals(t, unwrapped, nil) | ||
return | ||
} | ||
testutil.Equals(t, unwrapped.Error(), tc.expected, "Unwrap must match expected output") | ||
}) | ||
} | ||
} | ||
|
||
func TestCause(t *testing.T) { | ||
// test with base error that implements interface containing Unwrap method | ||
err := New(msg) | ||
|
||
for i, tc := range []struct { | ||
err error | ||
expected string | ||
isNil bool | ||
}{ | ||
{ | ||
// no wrapping | ||
err: err, | ||
isNil: true, | ||
}, | ||
{ | ||
err: Wrap(err, wrapper), | ||
isNil: true, | ||
}, | ||
{ | ||
err: Wrap(Wrap(err, wrapper), wrapper), | ||
isNil: true, | ||
}, | ||
// check primitives errors | ||
{ | ||
err: stderrors.New("std-error"), | ||
expected: "std-error", | ||
}, | ||
{ | ||
err: Wrap(stderrors.New("std-error"), wrapper), | ||
expected: "std-error", | ||
}, | ||
{ | ||
err: nil, | ||
isNil: true, | ||
}, | ||
} { | ||
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) { | ||
cause := Cause(tc.err) | ||
if tc.isNil { | ||
testutil.Equals(t, cause, nil) | ||
return | ||
} | ||
testutil.Equals(t, cause.Error(), tc.expected, "Unwrap must match expected output") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package errors | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func caller() stacktrace { | ||
return newStackTrace() | ||
} | ||
|
||
func TestStacktraceOutput(t *testing.T) { | ||
st := caller() | ||
expectedPhrase := "thanos/pkg/errors/stacktrace_test.go:13" | ||
if !strings.Contains(st.String(), expectedPhrase) { | ||
t.Fatalf("expected %v phrase into the stacktrace, received stacktrace: \n%v", expectedPhrase, st.String()) | ||
} | ||
} | ||
|
||
func TestStacktraceProgramCounterLen(t *testing.T) { | ||
st := caller() | ||
output := st.String() | ||
lines := len(strings.Split(strings.TrimSuffix(output, "\n"), "\n")) | ||
if len(st) != lines { | ||
t.Fatalf("output lines vs program counter size mismatch: program counter size %v, output lines %v", len(st), lines) | ||
} | ||
} |