Skip to content

Commit

Permalink
prefer testify error funcs over nil checks (influxdata#7857)
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo authored Jul 20, 2020
1 parent bf5befa commit 6580c6a
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 70 deletions.
4 changes: 2 additions & 2 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ func TestCompressWithGzipEarlyClose(t *testing.T) {

func TestVersionAlreadySet(t *testing.T) {
err := SetVersion("foo")
assert.Nil(t, err)
assert.NoError(t, err)

err = SetVersion("bar")

assert.NotNil(t, err)
assert.Error(t, err)
assert.IsType(t, VersionAlreadySetError, err)

assert.Equal(t, "foo", Version())
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestBindJsonStats(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(b.Gather)

assert.Nil(t, err)
assert.NoError(t, err)

// Use subtests for counters, since they are similar structure
type fieldSet struct {
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestBindXmlStatsV2(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(b.Gather)

assert.Nil(t, err)
assert.NoError(t, err)

// Use subtests for counters, since they are similar structure
type fieldSet struct {
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestBindXmlStatsV3(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(b.Gather)

assert.Nil(t, err)
assert.NoError(t, err)

// Use subtests for counters, since they are similar structure
type fieldSet struct {
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestHttpJsonJavaMultiValue(t *testing.T) {
acc.SetDebug(true)
err := acc.GatherError(cassandra.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 2, len(acc.Metrics))

fields := map[string]interface{}{
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestHttpJsonJavaMultiType(t *testing.T) {
acc.SetDebug(true)
err := acc.GatherError(cassandra.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 2, len(acc.Metrics))

fields := map[string]interface{}{
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestHttpJsonCassandraMultiValue(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(cassandra.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 1, len(acc.Metrics))

fields := map[string]interface{}{
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestHttpJsonCassandraNestedMultiValue(t *testing.T) {
acc.SetDebug(true)
err := acc.GatherError(cassandra.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 2, len(acc.Metrics))

fields1 := map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestSelectMetrics(t *testing.T) {
// We've asked for 2 (out of 4) metrics, over all 3 load balancers in all 2
// AZs. We should get 12 metrics.
assert.Equal(t, 12, len(filtered[0].metrics))
assert.Nil(t, err)
assert.NoError(t, err)
}

func TestGenerateStatisticsInputParams(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/filecount/filesystem_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestMTime(t *testing.T) {

fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata/foo")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, mtime, fileInfo.ModTime())
}

Expand All @@ -22,7 +22,7 @@ func TestSize(t *testing.T) {
size := int64(4096)
fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, size, fileInfo.Size())
}

Expand All @@ -31,7 +31,7 @@ func TestIsDir(t *testing.T) {
dir := true
fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, dir, fileInfo.IsDir())
}

Expand All @@ -40,7 +40,7 @@ func TestRealFS(t *testing.T) {
var fs fileSystem = osFS{}
//the following file exists on disk - and not in our fake fs
fileInfo, err := fs.Stat(getTestdataDir() + "/qux")
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, false, fileInfo.IsDir())
require.Equal(t, int64(446), fileInfo.Size())

Expand All @@ -52,7 +52,7 @@ func TestRealFS(t *testing.T) {
require.Equal(t, expectedError, err.Error())
// and verify that what we DO expect to find, we do
fileInfo, err = fs.Stat("/testdata/foo")
require.Nil(t, err)
require.NoError(t, err)
}

func getTestFileSystem() fakeFileSystem {
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func TestNewGithubClient(t *testing.T) {
httpClient := &http.Client{}
g := &GitHub{}
client, err := g.newGithubClient(httpClient)
require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, client.BaseURL.String(), "api.github.com")
g.EnterpriseBaseURL = "api.example.com/"
enterpriseClient, err := g.newGithubClient(httpClient)
require.Nil(t, err)
require.NoError(t, err)
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestSplitRepositoryNameWithNoSlash(t *testing.T) {
t.Run(tt, func(t *testing.T) {
_, _, err := splitRepositoryName(tt)

require.NotNil(t, err)
require.Error(t, err)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/gnmi/gnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func TestParsePath(t *testing.T) {
path := "/foo/bar/bla[shoo=woo][shoop=/woop/]/z"
parsed, err := parsePath("theorigin", path, "thetarget")

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, parsed.Origin, "theorigin")
assert.Equal(t, parsed.Target, "thetarget")
assert.Equal(t, parsed.Element, []string{"foo", "bar", "bla[shoo=woo][shoop=/woop/]", "z"})
assert.Equal(t, parsed.Elem, []*gnmi.PathElem{{Name: "foo"}, {Name: "bar"},
{Name: "bla", Key: map[string]string{"shoo": "woo", "shoop": "/woop/"}}, {Name: "z"}})

parsed, err = parsePath("", "", "")
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, *parsed, gnmi.Path{})

parsed, err = parsePath("", "/foo[[", "")
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/jolokia/jolokia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestHttpJsonMultiValue(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(jolokia.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 1, len(acc.Metrics))

fields := map[string]interface{}{
Expand All @@ -184,7 +184,7 @@ func TestHttpJsonBulkResponse(t *testing.T) {
var acc testutil.Accumulator
err := jolokia.Gather(&acc)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 1, len(acc.Metrics))

fields := map[string]interface{}{
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestHttpJsonThreeLevelMultiValue(t *testing.T) {
var acc testutil.Accumulator
err := acc.GatherError(jolokia.Gather)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 1, len(acc.Metrics))

fields := map[string]interface{}{
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/statsd/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ func TestEventGather(t *testing.T) {
t.Run(tests[i].name, func(t *testing.T) {
err := s.parseEventMessage(tests[i].now, tests[i].message, tests[i].hostname)
if tests[i].err {
require.NotNil(t, err)
require.Error(t, err)
} else {
require.Nil(t, err)
require.NoError(t, err)
}
require.Equal(t, uint64(i+1), acc.NMetrics())

require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, tests[i].expected.title, acc.Metrics[i].Measurement)
require.Equal(t, tests[i].expected.tags, acc.Metrics[i].Tags)
require.Equal(t, tests[i].expected.fields, acc.Metrics[i].Fields)
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestEvents(t *testing.T) {
t.Run(tests[i].name, func(t *testing.T) {
acc.ClearMetrics()
err := s.parseEventMessage(tests[i].args.now, tests[i].args.message, tests[i].args.hostname)
require.Nil(t, err)
require.NoError(t, err)
m := acc.Metrics[0]
require.Equal(t, tests[i].expected.title, m.Measurement)
require.Equal(t, tests[i].expected.text, m.Fields["text"])
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/syslog/rfc5426_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func testRFC5426(t *testing.T, protocol string, address string, bestEffort bool)
// Connect
conn, err := net.Dial(protocol, address)
require.NotNil(t, conn)
require.Nil(t, err)
require.NoError(t, err)

// Write
_, err = conn.Write(tc.data)
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestTimeIncrement_udp(t *testing.T) {
conn, err := net.Dial("udp", address)
require.NotNil(t, conn)
defer conn.Close()
require.Nil(t, err)
require.NoError(t, err)

// Write
_, e := conn.Write([]byte("<1>1 - - - - - -"))
Expand Down
Loading

0 comments on commit 6580c6a

Please sign in to comment.