Tests: replace assert.Nil(t, err) with assert.NoError(t, err)

This is a better way to check errors, both semantically (no usually nil is
bad, but nil error is good, so this avoids a mental negation) and for
the reporting by the assert package.
This commit is contained in:
Sybren A. Stüvel 2022-03-26 14:05:53 +01:00
parent 91a6d66de5
commit 3905135465
7 changed files with 28 additions and 28 deletions

@ -53,7 +53,7 @@ func TestCheckout(t *testing.T) {
func assertLinksTo(t *testing.T, linkPath, expectedTarget string) {
actualTarget, err := os.Readlink(linkPath)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, expectedTarget, actualTarget)
}

@ -48,29 +48,29 @@ func TestSymlinkToCheckout(t *testing.T) {
// Fake an older file.
blobPath := path.Join(manager.checkoutBasePath, "jemoeder.blob")
err := ioutil.WriteFile(blobPath, []byte("op je hoofd"), 0600)
assert.Nil(t, err)
assert.NoError(t, err)
wayBackWhen := time.Now().Add(-time.Hour * 24 * 100)
err = os.Chtimes(blobPath, wayBackWhen, wayBackWhen)
assert.Nil(t, err)
assert.NoError(t, err)
symlinkRelativePath := "path/to/jemoeder.txt"
err = manager.SymlinkToCheckout(blobPath, manager.checkoutBasePath, symlinkRelativePath)
assert.Nil(t, err)
assert.NoError(t, err)
// Wait for touch() calls to be done.
manager.wg.Wait()
// The blob should have been touched to indicate it was referenced just now.
stat, err := os.Stat(blobPath)
assert.Nil(t, err)
assert.NoError(t, err)
assert.True(t,
stat.ModTime().After(wayBackWhen),
"File must be touched (%v must be later than %v)", stat.ModTime(), wayBackWhen)
symlinkPath := path.Join(manager.checkoutBasePath, symlinkRelativePath)
stat, err = os.Lstat(symlinkPath)
assert.Nil(t, err)
assert.NoError(t, err)
assert.True(t, stat.Mode()&os.ModeType == os.ModeSymlink,
"%v should be a symlink", symlinkPath)
}

@ -76,7 +76,7 @@ func TestGCFindOldFiles(t *testing.T) {
// Since all the links have just been created, nothing should be considered old.
ageThreshold := server.gcAgeThreshold()
old, err := server.gcFindOldFiles(ageThreshold, log.With().Str("test", "test").Logger())
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, mtimeMap{}, old)
// Make some files old, they should show up in a scan.
@ -86,7 +86,7 @@ func TestGCFindOldFiles(t *testing.T) {
makeOld(server, expectOld, "stored/dc/89f15de821ad1df3e78f8ef455e653a2d1862f2eb3f5ee78aa4ca68eb6fb35/781.blob")
old, err = server.gcFindOldFiles(ageThreshold, log.With().Str("package", "shaman/test").Logger())
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, expectOld, old)
}
@ -124,18 +124,18 @@ func TestGCComponents(t *testing.T) {
// No symlinks created yet, so this should report all the files in oldFiles.
oldFiles := copymap(expectOld)
err := server.gcFilterLinkedFiles(server.config.CheckoutPath(), oldFiles, log.With().Str("package", "shaman/test").Logger(), nil)
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, expectOld, oldFiles)
// Create some symlinks
checkoutInfo, err := server.checkoutMan.PrepareCheckout("checkoutID")
assert.Nil(t, err)
assert.NoError(t, err)
err = server.checkoutMan.SymlinkToCheckout(absPaths["3367.blob"], server.config.CheckoutPath(),
path.Join(checkoutInfo.RelativePath, "use-of-3367.blob"))
assert.Nil(t, err)
assert.NoError(t, err)
err = server.checkoutMan.SymlinkToCheckout(absPaths["781.blob"], extraCheckoutDir,
path.Join(checkoutInfo.RelativePath, "use-of-781.blob"))
assert.Nil(t, err)
assert.NoError(t, err)
// There should only be two old file reported now.
expectRemovable := mtimeMap{
@ -146,17 +146,17 @@ func TestGCComponents(t *testing.T) {
stats := GCStats{}
err = server.gcFilterLinkedFiles(server.config.CheckoutPath(), oldFiles, log.With().Str("package", "shaman/test").Logger(), &stats)
assert.Equal(t, 1, stats.numSymlinksChecked) // 1 is in checkoutPath, the other in extraCheckoutDir
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, len(expectRemovable)+1, len(oldFiles)) // one file is linked from the extra checkout dir
err = server.gcFilterLinkedFiles(extraCheckoutDir, oldFiles, log.With().Str("package", "shaman/test").Logger(), &stats)
assert.Equal(t, 2, stats.numSymlinksChecked) // 1 is in checkoutPath, the other in extraCheckoutDir
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, expectRemovable, oldFiles)
// Touching a file before requesting deletion should not delete it.
now := time.Now()
err = os.Chtimes(absPaths["6001.blob"], now, now)
assert.Nil(t, err)
assert.NoError(t, err)
// Running the garbage collector should only remove that one unused and untouched file.
assert.FileExists(t, absPaths["6001.blob"], "file should exist before GC")
@ -199,13 +199,13 @@ func TestGarbageCollect(t *testing.T) {
// Create some symlinks
checkoutInfo, err := server.checkoutMan.PrepareCheckout("checkoutID")
assert.Nil(t, err)
assert.NoError(t, err)
err = server.checkoutMan.SymlinkToCheckout(absPaths["3367.blob"], server.config.CheckoutPath(),
path.Join(checkoutInfo.RelativePath, "use-of-3367.blob"))
assert.Nil(t, err)
assert.NoError(t, err)
err = server.checkoutMan.SymlinkToCheckout(absPaths["781.blob"], extraCheckoutDir,
path.Join(checkoutInfo.RelativePath, "use-of-781.blob"))
assert.Nil(t, err)
assert.NoError(t, err)
// Running the garbage collector should only remove those two unused files.
assert.FileExists(t, absPaths["6001.blob"], "file should exist before GC")

@ -86,7 +86,7 @@ func TestStoreFile(t *testing.T) {
assert.FileExists(t, path)
savedContent, err := ioutil.ReadFile(path)
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, payload, savedContent, "The file should be saved uncompressed")
}

@ -105,7 +105,7 @@ func TestOpenForUpload(t *testing.T) {
fileSize := int64(len(contents))
file, err := store.OpenForUpload("abcdefxxx", fileSize)
assert.Nil(t, err)
assert.NoError(t, err)
file.Write(contents)
file.Close()
@ -114,7 +114,7 @@ func TestOpenForUpload(t *testing.T) {
assert.Equal(t, StatusUploading, status)
readContents, err := ioutil.ReadFile(foundPath)
assert.Nil(t, err)
assert.NoError(t, err)
assert.EqualValues(t, contents, readContents)
}
@ -126,16 +126,16 @@ func TestMoveToStored(t *testing.T) {
fileSize := int64(len(contents))
err := store.MoveToStored("abcdefxxx", fileSize, "/just/some/path")
assert.NotNil(t, err)
assert.Error(t, err)
file, err := store.OpenForUpload("abcdefxxx", fileSize)
assert.Nil(t, err)
assert.NoError(t, err)
file.Write(contents)
file.Close()
tempLocation := file.Name()
err = store.MoveToStored("abcdefxxx", fileSize, file.Name())
assert.Nil(t, err)
assert.NoError(t, err)
foundPath, status := store.ResolveFile("abcdefxxx", fileSize, ResolveEverything)
assert.NotEqual(t, file.Name(), foundPath)

@ -55,7 +55,7 @@ func TestContains(t *testing.T) {
func TestFilePermissions(t *testing.T) {
dirname, err := os.MkdirTemp("", "file-permission-test")
assert.Nil(t, err)
assert.NoError(t, err)
defer os.RemoveAll(dirname)
bin := storageBin{
@ -65,11 +65,11 @@ func TestFilePermissions(t *testing.T) {
}
file, err := bin.openForWriting("testfilename.blend")
assert.Nil(t, err)
assert.NoError(t, err)
defer file.Close()
filestat, err := file.Stat()
assert.Nil(t, err)
assert.NoError(t, err)
// The exact permissions depend on the current (unittest) process umask. This
// umask is not easy to get, which is why we have a copy of `tempfile.go` in

@ -46,7 +46,7 @@ func TestTouch(t *testing.T) {
assert.Nil(t, Touch(testPath))
stat, err := os.Stat(testPath)
assert.Nil(t, err)
assert.NoError(t, err)
threshold := time.Now().Add(-5 * time.Second)
assert.True(t, stat.ModTime().After(threshold),