From 9c6f23d7ccf7ad0104702394cee66754ccd90e2c Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 8 Jul 2019 17:50:31 +0200 Subject: [PATCH] typo in timeout tests + adding tests for standarderror --- build/tests/docker-entrypoint.sh | 1 + internal/pkg/standarderror/standarderror.go | 4 +- .../pkg/standarderror/standarderror_test.go | 70 +++++++++++++++++++ internal/pkg/timeout/timeout_test.go | 4 +- test/testfunc.go | 2 +- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 internal/pkg/standarderror/standarderror_test.go diff --git a/build/tests/docker-entrypoint.sh b/build/tests/docker-entrypoint.sh index 91cc7d79..b4b0ea1d 100755 --- a/build/tests/docker-entrypoint.sh +++ b/build/tests/docker-entrypoint.sh @@ -9,6 +9,7 @@ go test github.com/thecodingmachine/gotenberg/internal/pkg/pm2 -run TestChromeSt go test github.com/thecodingmachine/gotenberg/internal/pkg/pm2 -run TestUnoconvStart # Running others tests. +go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg/internal/pkg go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg/internal/app/api # Finally testing processes shutdown. diff --git a/internal/pkg/standarderror/standarderror.go b/internal/pkg/standarderror/standarderror.go index 4391374e..f5ad9ec8 100644 --- a/internal/pkg/standarderror/standarderror.go +++ b/internal/pkg/standarderror/standarderror.go @@ -64,6 +64,8 @@ func Code(err error) string { return Internal } +const defaultMessage = "an internal error has occurred: please contact technical support" + // Message returns the human-readable message of the error, if available. // Otherwise returns a generic error message. func Message(err error) string { @@ -77,7 +79,7 @@ func Message(err error) string { if ok && e.Err != nil { return Message(e.Err) } - return "An internal error has occurred. Please contact technical support." + return defaultMessage } // Op returns the logical operation of the error, if available. diff --git a/internal/pkg/standarderror/standarderror_test.go b/internal/pkg/standarderror/standarderror_test.go new file mode 100644 index 00000000..eecd5631 --- /dev/null +++ b/internal/pkg/standarderror/standarderror_test.go @@ -0,0 +1,70 @@ +package standarderror + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func scenario1() error { + rootErr := errors.New("root error") + nestedErr := &Error{ + Code: Invalid, + Op: "bar", + Message: "nested error", + Err: rootErr, + } + err := &Error{ + Op: "foo", + Err: nestedErr, + } + return err +} + +func scenario2() error { + nestedErr := &Error{ + Code: Invalid, + Op: "bar", + Message: "nested error", + } + err := &Error{ + Code: Internal, + Op: "foo", + Err: nestedErr, + } + return err +} + +func TestError(t *testing.T) { + err := scenario1() + assert.Equal(t, "root error", err.Error()) + err = scenario2() + assert.Equal(t, " nested error", err.Error()) +} + +func TestCode(t *testing.T) { + assert.Equal(t, "", Code(nil)) + err := scenario1() + assert.Equal(t, Invalid, Code(err)) + err = scenario2() + assert.Equal(t, Internal, Code(err)) + err = errors.New("some error") + assert.Equal(t, Internal, Code(err)) +} + +func TestMessage(t *testing.T) { + assert.Equal(t, "", Message(nil)) + err := scenario1() + assert.Equal(t, "nested error", Message(err)) + err = errors.New("some error") + assert.Equal(t, defaultMessage, Message(err)) +} + +func TestOp(t *testing.T) { + assert.Equal(t, "", Op(nil)) + err := scenario1() + assert.Equal(t, "foo: bar", Op(err)) + err = errors.New("some error") + assert.Equal(t, "", Op(err)) +} diff --git a/internal/pkg/timeout/timeout_test.go b/internal/pkg/timeout/timeout_test.go index 23d4c06a..9ab855e9 100644 --- a/internal/pkg/timeout/timeout_test.go +++ b/internal/pkg/timeout/timeout_test.go @@ -27,12 +27,12 @@ func TestErr(t *testing.T) { err := Err(ctx) assert.NotNil(t, err) standardized := test.RequireStandardError(t, err) - assert.Equal(t, standardized.Code, standarderror.Timeout) + assert.Equal(t, standarderror.Timeout, standardized.Code) // should failed. ctx, cancel = Context(5) cancel() err = Err(ctx) assert.NotNil(t, err) standardized = test.RequireStandardError(t, err) - assert.Equal(t, standarderror.Code(err), standarderror.Internal) + assert.Equal(t, standarderror.Internal, standarderror.Code(err)) } diff --git a/test/testfunc.go b/test/testfunc.go index bf1103b2..5fb43bb9 100644 --- a/test/testfunc.go +++ b/test/testfunc.go @@ -45,7 +45,7 @@ func AssertConcurrent(t *testing.T, fn func() error, amount int) { // If so, returns the instance of standarderror.Error. func RequireStandardError(t *testing.T, err error) *standarderror.Error { standardized, ok := err.(*standarderror.Error) - require.Equal(t, ok, true) + require.Equal(t, true, ok) return standardized }