Files
gh-actions-cache/internal/utils_test.go
Tom Wieczorek ed34fad2e5 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.
2023-03-11 22:19:00 +05:30

55 lines
1.3 KiB
Go

package internal
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetRepo_IncorrectRepoString(t *testing.T) {
r := "testOrg/testRepo/123/123"
repo, err := GetRepo(r)
assert.ErrorContains(t, err, fmt.Sprintf("expected the \"[HOST/]OWNER/REPO\" format, got \"%s\"", r))
assert.Nil(t, repo)
}
func TestGetRepo_CorrectRepoString(t *testing.T) {
r := "testOrg/testRepo"
repo, err := GetRepo(r)
assert.NoError(t, err)
if assert.NotNil(t, repo) {
assert.Equal(t, "github.com", repo.Host())
assert.Equal(t, "testOrg", repo.Owner())
assert.Equal(t, "testRepo", repo.Name())
}
}
func TestGetRepo_CorrectRepoStringWithCustomHost(t *testing.T) {
r := "api.testEnterprise.com/testOrg/testRepo"
repo, err := GetRepo(r)
assert.NoError(t, err)
if assert.NotNil(t, repo) {
assert.Equal(t, "api.testEnterprise.com", repo.Host())
assert.Equal(t, "testOrg", repo.Owner())
assert.Equal(t, "testRepo", repo.Name())
}
}
func TestFormatCacheSize_MB(t *testing.T) {
cacheSizeInBytes := 1024 * 1024 * 1.5
cacheSizeDetailString := FormatCacheSize(cacheSizeInBytes)
assert.Equal(t, "1.50 MB", cacheSizeDetailString)
}
func TestFormatCacheSize_GB(t *testing.T) {
cacheSizeInBytes := 1024 * 1024 * 1024 * 1.5
cacheSizeDetailString := FormatCacheSize(cacheSizeInBytes)
assert.Equal(t, "1.50 GB", cacheSizeDetailString)
}