Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,9 @@ func isFunction(arg interface{}) bool {
return reflect.TypeOf(arg).Kind() == reflect.Func
}

var spewConfig = spew.ConfigState{
const stringerEnabledEnvVarName = "TESTIFY_SPEW_STRINGER_ENABLE"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't expose the library name in the environment variable. Something like "TESTIFY_DIFF_STRINGER_ENABLE" would be better, if we use an environment variable at all.


var spewConfigStringerDisabled = spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
Expand All @@ -1659,6 +1661,20 @@ var spewConfigStringerEnabled = spew.ConfigState{
MaxDepth: 10,
}

var spewConfig spew.ConfigState

func setupSpewConfig() {
if os.Getenv(stringerEnabledEnvVarName) == "TRUE" {
spewConfig = spewConfigStringerEnabled
return
}
spewConfig = spewConfigStringerDisabled
}

func init() {
setupSpewConfig()
}

type tHelper interface {
Helper()
}
Expand Down
48 changes: 48 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,54 @@ func TestDiffRace(t *testing.T) {
}
}

func TestDiffEnableStringer(t *testing.T) {
if err := os.Setenv(stringerEnabledEnvVarName, "TRUE"); err != nil {
t.Fatal(fmt.Sprintf("unable to set <%s> env var", stringerEnabledEnvVarName))
}
defer func() {
err := os.Unsetenv(stringerEnabledEnvVarName)
if err != nil {
t.Fatal(fmt.Sprintf("unable to unset <%s> env var", stringerEnabledEnvVarName))
}
}()
setupSpewConfig()

loc, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatal("unable to load location")
}

type structWithTime struct {
someTime time.Time
someString string
}

structE := structWithTime{
someTime: time.Date(2021, 5, 22, 0, 0, 0, 0, loc),
someString: "some val",
}

structA := structWithTime{
someTime: time.Date(2021, 5, 23, 0, 0, 0, 0, time.UTC),
someString: "some val",
}

expected := `

Diff:
--- Expected
+++ Actual
@@ -1,3 +1,3 @@
(assert.structWithTime) {
- someTime: (time.Time) 2021-05-22 00:00:00 -0400 EDT,
+ someTime: (time.Time) 2021-05-23 00:00:00 +0000 UTC,
someString: (string) (len=8) "some val"
`

actual := diff(structE, structA)
Equal(t, expected, actual)
}

type mockTestingT struct {
errorFmt string
args []interface{}
Expand Down