diff options
author | Alessio Caiazza <376774-nolith@users.noreply.gitlab.com> | 2021-09-30 11:09:01 +0300 |
---|---|---|
committer | Alessio Caiazza <376774-nolith@users.noreply.gitlab.com> | 2021-09-30 11:09:01 +0300 |
commit | dee99f0074625c3b1dfe07aa477e159344f89ac2 (patch) | |
tree | 7d2632aa28e9b7a6ca87a369f9a0815fc22347fb | |
parent | 8bf69d0b47e766424634a52a27428cb26a0376c4 (diff) | |
parent | 5ba8177bf2acbd44676065185797ca69a6ef56bb (diff) |
Merge branch 'fix/os-errors' into 'master'
refactor: avoid os.Is.. in favor of errors.Is
See merge request gitlab-org/gitlab-pages!567
-rw-r--r-- | internal/serving/disk/symlink/path_test.go | 10 | ||||
-rw-r--r-- | internal/vfs/local/root_test.go | 8 | ||||
-rw-r--r-- | internal/vfs/local/vfs.go | 11 | ||||
-rw-r--r-- | test/acceptance/acceptance_test.go | 4 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 4 |
5 files changed, 23 insertions, 14 deletions
diff --git a/internal/serving/disk/symlink/path_test.go b/internal/serving/disk/symlink/path_test.go index 400432c2..21880efa 100644 --- a/internal/serving/disk/symlink/path_test.go +++ b/internal/serving/disk/symlink/path_test.go @@ -6,6 +6,8 @@ package symlink_test import ( "context" + "errors" + "io/fs" "os" "path/filepath" "runtime" @@ -20,7 +22,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/vfs/local" ) -var fs = vfs.Instrumented(&local.VFS{}) +var localFs = vfs.Instrumented(&local.VFS{}) type EvalSymlinksTest struct { // If dest is empty, the path is created; otherwise the dest is symlinked to the path. @@ -70,7 +72,7 @@ func simpleJoin(path ...string) string { } func testEvalSymlinks(t *testing.T, wd, path, want string) { - root, err := fs.Root(context.Background(), wd) + root, err := localFs.Root(context.Background(), wd) require.NoError(t, err) have, err := symlink.EvalSymlinks(context.Background(), root, path) @@ -125,7 +127,7 @@ func TestEvalSymlinksIsNotExist(t *testing.T) { root, _ := testhelpers.TmpDir(t, "symlink_tests") _, err := symlink.EvalSymlinks(context.Background(), root, "notexist") - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { t.Errorf("expected the file is not found, got %v\n", err) } @@ -136,7 +138,7 @@ func TestEvalSymlinksIsNotExist(t *testing.T) { defer os.Remove("link") _, err = symlink.EvalSymlinks(context.Background(), root, "link") - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { t.Errorf("expected the file is not found, got %v\n", err) } } diff --git a/internal/vfs/local/root_test.go b/internal/vfs/local/root_test.go index 1cf505bf..a835bae3 100644 --- a/internal/vfs/local/root_test.go +++ b/internal/vfs/local/root_test.go @@ -2,7 +2,9 @@ package local import ( "context" + "errors" "io" + "io/fs" "os" "path/filepath" "testing" @@ -95,7 +97,7 @@ func TestReadlink(t *testing.T) { target, err := root.Readlink(ctx, test.path) if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, os.IsNotExist(err), "IsNotExist") + require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") return } @@ -211,7 +213,7 @@ func TestLstat(t *testing.T) { fi, err := root.Lstat(ctx, test.path) if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, os.IsNotExist(err), "IsNotExist") + require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") return } @@ -271,7 +273,7 @@ func TestOpen(t *testing.T) { } if test.expectedIsNotExist { - require.Equal(t, test.expectedIsNotExist, os.IsNotExist(err), "IsNotExist") + require.Equal(t, test.expectedIsNotExist, errors.Is(err, fs.ErrNotExist), "IsNotExist") return } diff --git a/internal/vfs/local/vfs.go b/internal/vfs/local/vfs.go index ea54e8e8..5cca94fd 100644 --- a/internal/vfs/local/vfs.go +++ b/internal/vfs/local/vfs.go @@ -3,6 +3,7 @@ package local import ( "context" "errors" + "io/fs" "os" "path/filepath" @@ -14,21 +15,21 @@ var errNotDirectory = errors.New("path needs to be a directory") type VFS struct{} -func (fs VFS) Root(ctx context.Context, path string) (vfs.Root, error) { +func (localFs VFS) Root(ctx context.Context, path string) (vfs.Root, error) { rootPath, err := filepath.Abs(path) if err != nil { return nil, err } rootPath, err = filepath.EvalSymlinks(rootPath) - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { return nil, &vfs.ErrNotExist{Inner: err} } else if err != nil { return nil, err } fi, err := os.Lstat(rootPath) - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { return nil, &vfs.ErrNotExist{Inner: err} } else if err != nil { return nil, err @@ -41,11 +42,11 @@ func (fs VFS) Root(ctx context.Context, path string) (vfs.Root, error) { return &Root{rootPath: rootPath}, nil } -func (fs *VFS) Name() string { +func (localFs *VFS) Name() string { return "local" } -func (fs *VFS) Reconfigure(*config.Config) error { +func (localFs *VFS) Reconfigure(*config.Config) error { // noop return nil } diff --git a/test/acceptance/acceptance_test.go b/test/acceptance/acceptance_test.go index 9d0c0cd2..b36bd63d 100644 --- a/test/acceptance/acceptance_test.go +++ b/test/acceptance/acceptance_test.go @@ -1,8 +1,10 @@ package acceptance_test import ( + "errors" "flag" "fmt" + "io/fs" "log" "os" "testing" @@ -58,7 +60,7 @@ func TestMain(m *testing.M) { os.Exit(0) } - if _, err := os.Stat(*pagesBinary); os.IsNotExist(err) { + if _, err := os.Stat(*pagesBinary); errors.Is(err, fs.ErrNotExist) { log.Fatalf("Couldn't find gitlab-pages binary at %s\n", *pagesBinary) } diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 33a19127..d9f2f14c 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -6,8 +6,10 @@ import ( "crypto/tls" "crypto/x509" "encoding/json" + "errors" "fmt" "io" + "io/fs" "net" "net/http" "net/http/httptest" @@ -554,7 +556,7 @@ func (o *stubOpts) getAPICalled() bool { func lookupFromFile(t *testing.T, domain string, w http.ResponseWriter) { fixture, err := os.Open("../../shared/lookups/" + domain + ".json") - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { w.WriteHeader(http.StatusNoContent) t.Logf("GitLab domain %s source stub served 204", domain) |