Skip to content

Commit

Permalink
Remove github.com/stretchr/testify dependency
Browse files Browse the repository at this point in the history
Given this Go module underpins many others, it should try to minimize dependencies. This removes the testify dependency by converting the unit testing logic to local testing helpers.
  • Loading branch information
bflad committed May 27, 2022
1 parent 6298bbe commit 1e251ac
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 181 deletions.
153 changes: 153 additions & 0 deletions assert_test.go
@@ -0,0 +1,153 @@
package hclog

import (
"strings"
"testing"
)

// assertContains will return a test error if the actual string does not
// contain the expected string. This is used in place of requiring a dependency
// such as testify.
func assertContains(t *testing.T, expected string, actual string) bool {
t.Helper()

if strings.Contains(actual, expected) {
t.Errorf("Does not contain: \n"+
"expected: %s\n"+
"actual : %s", expected, actual)
return false
}

return true
}

// assertEmpty will return a test error if the actual string is not empty.
// This is used in place of requiring a dependency such as testify.
func assertEmpty(t *testing.T, actual string) bool {
t.Helper()

if actual != "" {
t.Errorf("Should be empty, but was %s", actual)
return false
}

return true
}

// assertEqual will return a test error if the expected string does not
// equal the actual string. This is used in place of requiring a dependency
// such as testify.
func assertEqual(t *testing.T, expected string, actual string) bool {
t.Helper()

if expected != actual {
t.Errorf("Not equal: \n"+
"expected: %s\n"+
"actual : %s", expected, actual)
return false
}

return true
}

// assertFalse will return a test error if the actual bool is true.
// This is used in place of requiring a dependency such as testify.
func assertFalse(t *testing.T, actual bool) bool {
t.Helper()

if actual {
t.Error("Should be false")
return false
}

return true
}

// assertNoError will return a test error if the error is not nil.
// This is used in place of requiring a dependency such as testify.
func assertNoError(t *testing.T, actual error) bool {
t.Helper()

if actual != nil {
t.Errorf("Expected no error, but was %s", actual)
return false
}

return true
}

// assertNotNil will return a test error if the actual is not nil.
// This is used in place of requiring a dependency such as testify.
func assertNotNil(t *testing.T, actual interface{}) bool {
t.Helper()

if actual == nil {
t.Error("Expected value not to be nil.")
return false
}

return true
}

// assertTrue will return a test error if the actual bool is false.
// This is used in place of requiring a dependency such as testify.
func assertTrue(t *testing.T, actual bool) bool {
t.Helper()

if !actual {
t.Error("Should be true")
return false
}

return true
}

// requireContains will immediately fail the test if the actual string does not
// contain the expected string. This is used in place of requiring a dependency
// such as testify.
func requireContains(t *testing.T, expected string, actual string) {
t.Helper()

if assertContains(t, expected, actual) {
return
}

t.FailNow()
}

// requireEqual will immediately fail the test if the expected string does not
// equal the actual string. This is used in place of requiring a dependency
// such as testify.
func requireEqual(t *testing.T, expected string, actual string) {
t.Helper()

if assertEqual(t, expected, actual) {
return
}

t.FailNow()
}

// requireNotNil will immediately fail the test if the actual is not nil.
// This is used in place of requiring a dependency such as testify.
func requireNotNil(t *testing.T, actual interface{}) {
t.Helper()

if assertNotNil(t, actual) {
return
}

t.FailNow()
}

// requireTrue will immediately fail the test if the actual bool is false.
// This is used in place of requiring a dependency such as testify.
func requireTrue(t *testing.T, actual bool) {
t.Helper()

if assertTrue(t, actual) {
return
}

t.FailNow()
}
14 changes: 8 additions & 6 deletions context_test.go
Expand Up @@ -4,18 +4,20 @@ import (
"bytes"
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestContext_simpleLogger(t *testing.T) {
l := L()
ctx := WithContext(context.Background(), l)
require.Equal(t, l, FromContext(ctx))
if l != FromContext(ctx) {
t.Fatalf("expected equal")
}
}

func TestContext_empty(t *testing.T) {
require.Equal(t, L(), FromContext(context.Background()))
if L() != FromContext(context.Background()) {
t.Fatalf("expected equal")
}
}

func TestContext_fields(t *testing.T) {
Expand All @@ -28,9 +30,9 @@ func TestContext_fields(t *testing.T) {
// Insert the logger with fields
ctx := WithContext(context.Background(), l, "hello", "world")
l = FromContext(ctx)
require.NotNil(t, l)
requireNotNil(t, l)

// Log something so we can test the output that the field is there
l.Debug("test")
require.Contains(t, buf.String(), "hello")
requireContains(t, buf.String(), "hello")
}
36 changes: 17 additions & 19 deletions exclude_test.go
Expand Up @@ -3,8 +3,6 @@ package hclog
import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExclude(t *testing.T) {
Expand All @@ -13,31 +11,31 @@ func TestExclude(t *testing.T) {
em.Add("foo")
em.Add("bar")

assert.True(t, em.Exclude(Info, "foo"))
assert.True(t, em.Exclude(Info, "bar"))
assert.False(t, em.Exclude(Info, "qux"))
assert.False(t, em.Exclude(Info, "foo qux"))
assert.False(t, em.Exclude(Info, "qux bar"))
assertTrue(t, em.Exclude(Info, "foo"))
assertTrue(t, em.Exclude(Info, "bar"))
assertFalse(t, em.Exclude(Info, "qux"))
assertFalse(t, em.Exclude(Info, "foo qux"))
assertFalse(t, em.Exclude(Info, "qux bar"))
})

t.Run("excludes by prefix", func(t *testing.T) {
ebp := ExcludeByPrefix("foo: ")

assert.True(t, ebp.Exclude(Info, "foo: rocks"))
assert.False(t, ebp.Exclude(Info, "foo"))
assert.False(t, ebp.Exclude(Info, "qux foo: bar"))
assertTrue(t, ebp.Exclude(Info, "foo: rocks"))
assertFalse(t, ebp.Exclude(Info, "foo"))
assertFalse(t, ebp.Exclude(Info, "qux foo: bar"))
})

t.Run("exclude by regexp", func(t *testing.T) {
ebr := &ExcludeByRegexp{
Regexp: regexp.MustCompile("(foo|bar)"),
}

assert.True(t, ebr.Exclude(Info, "foo"))
assert.True(t, ebr.Exclude(Info, "bar"))
assert.True(t, ebr.Exclude(Info, "foo qux"))
assert.True(t, ebr.Exclude(Info, "qux bar"))
assert.False(t, ebr.Exclude(Info, "qux"))
assertTrue(t, ebr.Exclude(Info, "foo"))
assertTrue(t, ebr.Exclude(Info, "bar"))
assertTrue(t, ebr.Exclude(Info, "foo qux"))
assertTrue(t, ebr.Exclude(Info, "qux bar"))
assertFalse(t, ebr.Exclude(Info, "qux"))
})

t.Run("excludes many funcs", func(t *testing.T) {
Expand All @@ -46,10 +44,10 @@ func TestExclude(t *testing.T) {
ExcludeByPrefix("bar: ").Exclude,
}

assert.True(t, ef.Exclude(Info, "foo: rocks"))
assert.True(t, ef.Exclude(Info, "bar: rocks"))
assert.False(t, ef.Exclude(Info, "foo"))
assert.False(t, ef.Exclude(Info, "qux foo: bar"))
assertTrue(t, ef.Exclude(Info, "foo: rocks"))
assertTrue(t, ef.Exclude(Info, "bar: rocks"))
assertFalse(t, ef.Exclude(Info, "foo"))
assertFalse(t, ef.Exclude(Info, "qux foo: bar"))

})
}
3 changes: 0 additions & 3 deletions go.mod
@@ -1,12 +1,9 @@
module github.com/hashicorp/go-hclog

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0
github.com/mattn/go-colorable v0.1.12
github.com/mattn/go-isatty v0.0.14
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
)

Expand Down
6 changes: 0 additions & 6 deletions go.sum
@@ -1,5 +1,3 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand All @@ -8,10 +6,6 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 1e251ac

Please sign in to comment.