diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-02-03 22:39:46 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-02-03 22:52:19 +0300 |
commit | b7d42ef5004b59d1885dcd332eaab64805d2578c (patch) | |
tree | a7167159ae9808b82bc88c114ffd7afd9773ecf3 /internal/vfs | |
parent | f2fd9a8e0b4778991adf054ef8f3a7244200a60d (diff) |
test: migrate to assertions using modern error checking
Diffstat (limited to 'internal/vfs')
-rw-r--r-- | internal/vfs/local/root_test.go | 55 | ||||
-rw-r--r-- | internal/vfs/local/vfs_test.go | 19 | ||||
-rw-r--r-- | internal/vfs/zip/archive_test.go | 24 | ||||
-rw-r--r-- | internal/vfs/zip/vfs_test.go | 28 |
4 files changed, 53 insertions, 73 deletions
diff --git a/internal/vfs/local/root_test.go b/internal/vfs/local/root_test.go index 094a48c0..9b8cfe23 100644 --- a/internal/vfs/local/root_test.go +++ b/internal/vfs/local/root_test.go @@ -2,11 +2,11 @@ package local import ( "context" - "errors" "io" "io/fs" "os" "path/filepath" + "syscall" "testing" "github.com/stretchr/testify/assert" @@ -70,25 +70,25 @@ func TestReadlink(t *testing.T) { tests := map[string]struct { path string expectedTarget string - expectedErr string + expectedErr error expectedInvalidPath bool - expectedIsNotExist bool }{ "a valid link": { path: "testdata/link", expectedTarget: "file", }, "a file": { - path: "testdata/file", - expectedErr: "invalid argument", + path: "testdata/file", + // TODO: use fs.ErrInvalid once https://github.com/golang/go/issues/30322 is fixed + expectedErr: syscall.EINVAL, }, "a path outside of root directory": { path: "testdata/../../link", expectedInvalidPath: true, }, "a non-existing link": { - path: "non-existing", - expectedIsNotExist: true, + path: "non-existing", + expectedErr: fs.ErrNotExist, }, } @@ -96,19 +96,13 @@ func TestReadlink(t *testing.T) { t.Run(name, func(t *testing.T) { target, err := root.Readlink(ctx, test.path) - if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") - return - } - if test.expectedInvalidPath { require.IsType(t, &invalidPathError{}, err, "InvalidPath") return } - if test.expectedErr != "" { - require.Error(t, err) - require.Contains(t, err.Error(), test.expectedErr, "Readlink") + if test.expectedErr != nil { + require.ErrorIs(t, err, test.expectedErr, "Readlink") return } @@ -176,7 +170,7 @@ func TestLstat(t *testing.T) { modePerm os.FileMode modeType os.FileMode expectedInvalidPath bool - expectedIsNotExist bool + expectedErr error }{ "a directory": { path: "testdata", @@ -198,8 +192,8 @@ func TestLstat(t *testing.T) { expectedInvalidPath: true, }, "a non-existing link": { - path: "non-existing", - expectedIsNotExist: true, + path: "non-existing", + expectedErr: fs.ErrNotExist, }, } @@ -211,8 +205,8 @@ func TestLstat(t *testing.T) { fi, err := root.Lstat(ctx, test.path) - if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") + if test.expectedErr != nil { + require.ErrorIs(t, err, test.expectedErr) return } @@ -238,9 +232,8 @@ func TestOpen(t *testing.T) { tests := map[string]struct { path string expectedInvalidPath bool - expectedIsNotExist bool expectedContent string - expectedErr string + expectedErr error }{ "a file": { path: "testdata/file", @@ -248,19 +241,19 @@ func TestOpen(t *testing.T) { }, "a directory": { path: "testdata", - expectedErr: errNotFile.Error(), + expectedErr: errNotFile, }, "a link": { path: "testdata/link", - expectedErr: "too many levels of symbolic links", + expectedErr: syscall.ELOOP, }, "a path outside of root directory": { path: "testdata/../../link", expectedInvalidPath: true, }, "a non-existing file": { - path: "non-existing", - expectedIsNotExist: true, + path: "non-existing", + expectedErr: fs.ErrNotExist, }, } @@ -271,14 +264,8 @@ func TestOpen(t *testing.T) { defer file.Close() } - if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") - return - } - - if test.expectedErr != "" { - require.Error(t, err, "Open") - require.Contains(t, err.Error(), test.expectedErr, "Open") + if test.expectedErr != nil { + require.ErrorIs(t, err, test.expectedErr, "Open") return } diff --git a/internal/vfs/local/vfs_test.go b/internal/vfs/local/vfs_test.go index fd36dd4c..19840a22 100644 --- a/internal/vfs/local/vfs_test.go +++ b/internal/vfs/local/vfs_test.go @@ -2,7 +2,6 @@ package local import ( "context" - "errors" "io/fs" "os" "path/filepath" @@ -56,10 +55,9 @@ func TestVFSRoot(t *testing.T) { } tests := map[string]struct { - path string - expectedPath string - expectedErr error - expectedIsNotExist bool + path string + expectedPath string + expectedErr error }{ "a valid directory": { path: "dir", @@ -86,8 +84,8 @@ func TestVFSRoot(t *testing.T) { expectedErr: errNotDirectory, }, "a non-existing file": { - path: "not-existing", - expectedIsNotExist: true, + path: "not-existing", + expectedErr: fs.ErrNotExist, }, } @@ -95,13 +93,8 @@ func TestVFSRoot(t *testing.T) { t.Run(name, func(t *testing.T) { rootVFS, err := localVFS.Root(context.Background(), filepath.Join(tmpDir, test.path), "") - if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist)) - return - } - if test.expectedErr != nil { - require.EqualError(t, err, test.expectedErr.Error()) + require.ErrorIs(t, err, test.expectedErr) return } diff --git a/internal/vfs/zip/archive_test.go b/internal/vfs/zip/archive_test.go index 37529e22..782c43b1 100644 --- a/internal/vfs/zip/archive_test.go +++ b/internal/vfs/zip/archive_test.go @@ -7,6 +7,7 @@ import ( "crypto/rand" "fmt" "io" + "io/fs" "net/http" "net/http/httptest" "os" @@ -66,7 +67,7 @@ func testOpen(t *testing.T, zip *zipArchive) { }, "file_does_not_exist": { file: "unknown.html", - expectedErr: os.ErrNotExist, + expectedErr: fs.ErrNotExist, }, } @@ -74,7 +75,7 @@ func testOpen(t *testing.T, zip *zipArchive) { t.Run(name, func(t *testing.T) { f, err := zip.Open(context.Background(), tt.file) if tt.expectedErr != nil { - require.EqualError(t, err, tt.expectedErr.Error()) + require.ErrorIs(t, err, tt.expectedErr) return } @@ -248,7 +249,7 @@ func testLstat(t *testing.T, zip *zipArchive) { }, "file_does_not_exist": { file: "unknown.html", - expectedErr: os.ErrNotExist, + expectedErr: fs.ErrNotExist, }, } @@ -256,7 +257,7 @@ func testLstat(t *testing.T, zip *zipArchive) { t.Run(name, func(t *testing.T) { fi, err := zip.Lstat(context.Background(), tt.file) if tt.expectedErr != nil { - require.EqualError(t, err, tt.expectedErr.Error()) + require.ErrorIs(t, err, tt.expectedErr) return } @@ -309,7 +310,7 @@ func testReadLink(t *testing.T, zip *zipArchive) { }, "file_does_not_exist": { file: "unknown.html", - expectedErr: os.ErrNotExist, + expectedErr: fs.ErrNotExist, }, } @@ -317,7 +318,7 @@ func testReadLink(t *testing.T, zip *zipArchive) { t.Run(name, func(t *testing.T) { link, err := zip.Readlink(context.Background(), tt.file) if tt.expectedErr != nil { - require.EqualError(t, err, tt.expectedErr.Error()) + require.ErrorIs(t, err, tt.expectedErr) return } @@ -357,7 +358,7 @@ func TestArchiveCanBeReadAfterOpenCtxCanceled(t *testing.T) { cancel() err := zip.openArchive(ctx, testServerURL+"/public.zip") - require.EqualError(t, err, context.Canceled.Error()) + require.ErrorIs(t, err, context.Canceled) <-zip.done @@ -374,15 +375,14 @@ func TestReadArchiveFails(t *testing.T) { testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil) defer cleanup() - fs := New(&zipCfg).(*zipVFS) - zip := newArchive(fs, time.Second) + zfs := New(&zipCfg).(*zipVFS) + zip := newArchive(zfs, time.Second) err := zip.openArchive(context.Background(), testServerURL+"/unkown.html") - require.Error(t, err) - require.Contains(t, err.Error(), httprange.ErrNotFound.Error()) + require.ErrorIs(t, err, httprange.ErrNotFound) _, err = zip.Open(context.Background(), "index.html") - require.EqualError(t, err, os.ErrNotExist.Error()) + require.ErrorIs(t, err, fs.ErrNotExist) } func createArchive(t *testing.T, dir string) (map[string][]byte, int64) { diff --git a/internal/vfs/zip/vfs_test.go b/internal/vfs/zip/vfs_test.go index 12e82c1c..af1d263b 100644 --- a/internal/vfs/zip/vfs_test.go +++ b/internal/vfs/zip/vfs_test.go @@ -5,6 +5,7 @@ import ( "errors" "io" "io/fs" + "net/url" "testing" "time" @@ -17,27 +18,27 @@ import ( ) func TestVFSRoot(t *testing.T) { - url, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil) + u, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil) defer cleanup() tests := map[string]struct { - path string - sha256 string - expectedErrMsg string + path string + sha256 string + expectedErr error }{ "zip_file_exists": { path: "/public.zip", sha256: "d6b318b399cfe9a1c8483e49847ee49a2676d8cfd6df57ec64d971ad03640a75", }, "zip_file_does_not_exist": { - path: "/unknown", - sha256: "filedoesnotexist", - expectedErrMsg: fs.ErrNotExist.Error(), + path: "/unknown", + sha256: "filedoesnotexist", + expectedErr: fs.ErrNotExist, }, "invalid_url": { - path: "/%", - sha256: "invalidurl", - expectedErrMsg: "invalid URL", + path: "/%", + sha256: "invalidurl", + expectedErr: url.EscapeError("%"), }, } @@ -45,10 +46,9 @@ func TestVFSRoot(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - root, err := vfs.Root(context.Background(), url+tt.path, tt.sha256) - if tt.expectedErrMsg != "" { - require.Error(t, err) - require.Contains(t, err.Error(), tt.expectedErrMsg) + root, err := vfs.Root(context.Background(), u+tt.path, tt.sha256) + if tt.expectedErr != nil { + require.ErrorIs(t, err, tt.expectedErr) return } |