Better testify usage (#54)
* Use assert.NoError instead of assert.Nil
This gives nicer error messages.
* Swap arguments to assert.Equal
The expectation comes first. Otherwise, error messages will be
misleading.
An example:
=== RUN TestDeleteWithIncorrectRepoForDeleteCaches
Error: authentication token not found for host github.com
delete_test.go:56:
Error Trace: /build/source/cmd/delete_test.go:56
Error: Should be true
Test: TestDeleteWithIncorrectRepoForDeleteCaches
Messages: 1 unmatched mocks: https://api.github.com/repos/testOrg/testRepo/actions/caches?key=cacheName
delete_test.go:59:
Error Trace: /build/source/cmd/delete_test.go:59
Error: Not equal:
expected: "authentication token not found for host github.com"
actual : "The given repo does not exist."
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-authentication token not found for host github.com
+The given repo does not exist.
Test: TestDeleteWithIncorrectRepoForDeleteCaches
--- FAIL: TestDeleteWithIncorrectRepoForDeleteCaches (0.00s)
* Use assert.Error instead of assert.NotNil
This gives nicer error messages.
* Use assert.ErrorAs and assert.ErrorContains
This simplifies the assertions and potentially gives better error
messages.
* Use require instead of assert and use assert.NotNil as guard
This is to prevent panics in tests, when things get accessed which
shouldn't be nil.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
@@ -9,6 +8,7 @@ import (
|
||||
"github.com/actions/gh-actions-cache/types"
|
||||
"github.com/cli/go-gh/pkg/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/h2non/gock.v1"
|
||||
)
|
||||
|
||||
@@ -27,14 +27,15 @@ func TestGetCacheUsage_CorrectRepo(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/testRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "list", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
totalCacheSize, err := artifactCache.GetCacheUsage()
|
||||
|
||||
assert.Equal(t, totalCacheSize, float64(291205))
|
||||
assert.Nil(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, float64(291205), totalCacheSize)
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
@@ -50,18 +51,18 @@ func TestGetCacheUsage_IncorrectRepo(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/wrongRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "list", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
totalCacheSize, err := artifactCache.GetCacheUsage()
|
||||
var httpError api.HTTPError
|
||||
errors.As(err, &httpError)
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, httpError.StatusCode, 404)
|
||||
assert.Equal(t, httpError.Message, "Not Found")
|
||||
assert.Equal(t, totalCacheSize, float64(-1))
|
||||
if assert.ErrorAs(t, err, &httpError) {
|
||||
assert.Equal(t, 404, httpError.StatusCode)
|
||||
assert.Equal(t, "Not Found", httpError.Message)
|
||||
}
|
||||
assert.Equal(t, float64(-1), totalCacheSize)
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
@@ -86,21 +87,23 @@ func TestListCaches_Success(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/testRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
f := types.ListOptions{BaseOptions: types.BaseOptions{Repo: "testOrg/testRepo"}, Limit: 30}
|
||||
queryParams := url.Values{}
|
||||
f.GenerateQueryParams(queryParams)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "list", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
listCacheResponse, err := artifactCache.ListCaches(queryParams)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, listCacheResponse)
|
||||
assert.Equal(t, listCacheResponse.TotalCount, 1)
|
||||
assert.Equal(t, len(listCacheResponse.ActionsCaches), 1)
|
||||
assert.Equal(t, listCacheResponse.ActionsCaches[0].Id, 29)
|
||||
assert.NoError(t, err)
|
||||
if assert.NotNil(t, listCacheResponse) {
|
||||
assert.Equal(t, 1, listCacheResponse.TotalCount)
|
||||
assert.Equal(t, 1, len(listCacheResponse.ActionsCaches))
|
||||
assert.Equal(t, 29, listCacheResponse.ActionsCaches[0].Id)
|
||||
}
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
@@ -116,22 +119,22 @@ func TestListCaches_Failure(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/testRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
f := types.ListOptions{BaseOptions: types.BaseOptions{Repo: "testOrg/testRepo"}, Limit: 30}
|
||||
queryParams := url.Values{}
|
||||
f.GenerateQueryParams(queryParams)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "list", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
listCacheResponse, err := artifactCache.ListCaches(queryParams)
|
||||
var httpError api.HTTPError
|
||||
errors.As(err, &httpError)
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, httpError.StatusCode, 404)
|
||||
assert.Equal(t, httpError.Message, "Not Found")
|
||||
assert.Equal(t, listCacheResponse, types.ListApiResponse{})
|
||||
if assert.ErrorAs(t, err, &httpError) {
|
||||
assert.Equal(t, 404, httpError.StatusCode)
|
||||
assert.Equal(t, "Not Found", httpError.Message)
|
||||
}
|
||||
assert.Equal(t, types.ListApiResponse{}, listCacheResponse)
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
@@ -156,18 +159,19 @@ func TestDeleteCaches_Success(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/testRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
f := types.DeleteOptions{BaseOptions: types.BaseOptions{Repo: "testOrg/testRepo"}}
|
||||
queryParams := url.Values{}
|
||||
f.GenerateBaseQueryParams(queryParams)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "delete", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
deletedCache, err := artifactCache.DeleteCaches(queryParams)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, deletedCache, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, deletedCache)
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
@@ -183,17 +187,18 @@ func TestDeleteCaches_Failure(t *testing.T) {
|
||||
}`)
|
||||
|
||||
repo, err := internal.GetRepo("testOrg/testRepo")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
f := types.DeleteOptions{BaseOptions: types.BaseOptions{Repo: "testOrg/testRepo"}}
|
||||
queryParams := url.Values{}
|
||||
f.GenerateBaseQueryParams(queryParams)
|
||||
|
||||
artifactCache, err := NewArtifactCache(repo, "delete", VERSION)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, artifactCache)
|
||||
deletedCache, err := artifactCache.DeleteCaches(queryParams)
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, deletedCache, 0)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 0, deletedCache)
|
||||
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user