Skip to content

Commit

Permalink
check and fix the use of errors.As
Browse files Browse the repository at this point in the history
  • Loading branch information
nopcoder committed Dec 6, 2020
1 parent b1b3858 commit e9c88a9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
25 changes: 10 additions & 15 deletions catalog/mvcc/cataloger_create_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,40 @@ func TestCataloger_CreateRepo(t *testing.T) {
tests := []struct {
name string
args args
wantErr bool
asErr error
wantErr error
}{
{
name: "basic",
args: args{name: "repo1", storage: "s3://bucket1", branch: "master"},
wantErr: false,
asErr: nil,
wantErr: nil,
},
{
name: "unknown branch",
args: args{name: "repo3", storage: "s3://bucket3", branch: ""},
wantErr: true,
asErr: catalog.ErrInvalidValue,
wantErr: catalog.ErrInvalidValue,
},
{
name: "missing repo",
args: args{name: "", storage: "s3://bucket1", branch: "master"},
wantErr: true,
asErr: catalog.ErrInvalidValue,
wantErr: catalog.ErrInvalidValue,
},
{
name: "missing storage",
args: args{name: "repo1", storage: "", branch: "master"},
wantErr: true,
asErr: catalog.ErrInvalidValue,
wantErr: catalog.ErrInvalidValue,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo, err := c.CreateRepository(ctx, tt.args.name, tt.args.storage, tt.args.branch)
if (err != nil) != tt.wantErr {
if !errors.Is(err, tt.wantErr) {
t.Fatalf("CreateRepository() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && tt.asErr != nil && !errors.As(err, &tt.asErr) {
t.Fatalf("CreateRepository() error = %v, expected as %v", err, tt.asErr)
}
if err != nil {
if tt.wantErr != nil {
if repo != nil {
t.Fatalf("CreateRepository() repo = %v, expected nil on error", repo)
}
return
}
if repo.Name != tt.args.name {
Expand Down
2 changes: 1 addition & 1 deletion catalog/mvcc/cataloger_delete_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestCataloger_DeleteBranch(t *testing.T) {
return
}
_, err = c.GetBranchReference(ctx, tt.args.repository, tt.args.branch)
if !errors.As(err, &db.ErrNotFound) {
if !errors.Is(err, db.ErrNotFound) {
t.Errorf("Branch should not be found after delete, got err=%s", err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions catalog/mvcc/cataloger_delete_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCataloger_DeleteEntry(t *testing.T) {
t.Run("delete file not exists", func(t *testing.T) {
err := c.DeleteEntry(ctx, repository, "master", "/file1")
wantErr := catalog.ErrEntryNotFound
if !errors.As(err, &wantErr) {
if !errors.Is(err, wantErr) {
t.Errorf("DeleteEntry() error = %s, want = %s", err, wantErr)
}
})
Expand Down Expand Up @@ -98,13 +98,13 @@ func TestCataloger_DeleteEntry(t *testing.T) {
func testDeleteEntryExpectNotFound(t *testing.T, ctx context.Context, c catalog.Cataloger, repository, branch string, path string) {
_, err := c.GetEntry(ctx, repository, MakeReference(branch, UncommittedID), path, catalog.GetEntryParams{})
wantErr := db.ErrNotFound
if !errors.As(err, &wantErr) {
if !errors.Is(err, wantErr) {
t.Fatalf("DeleteEntry() get entry err = %s, want = %s", err, wantErr)
}
// expect a second delete to fail on entry not found
err = c.DeleteEntry(ctx, repository, branch, path)
wantErr = catalog.ErrEntryNotFound
if !errors.As(err, &wantErr) {
if !errors.Is(err, wantErr) {
t.Fatalf("DeleteEntry() error = %s, want = %s", err, wantErr)
}
}
Expand All @@ -116,7 +116,7 @@ func testDeleteEntryCommitAndExpectNotFound(t *testing.T, ctx context.Context, c
}
_, err = c.GetEntry(ctx, repository, branch+CommittedSuffix, path, catalog.GetEntryParams{})
wantErr := db.ErrNotFound
if !errors.As(err, &wantErr) {
if !errors.Is(err, wantErr) {
t.Fatalf("DeleteEntry() get entry err = %s, want = %s", err, wantErr)
}
}
2 changes: 1 addition & 1 deletion catalog/mvcc/cataloger_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func insertMergeCommit(tx db.Tx, relation RelationType, leftID int64, rightID in
var parentLastLineage []int64
err = tx.Get(&parentLastLineage, `SELECT DISTINCT ON (branch_id) lineage_commits FROM catalog_commits
WHERE branch_id = $1 AND merge_type = 'from_parent' ORDER BY branch_id,commit_id DESC`, leftID)
if err != nil && !errors.As(err, &db.ErrNotFound) {
if err != nil && !errors.Is(err, db.ErrNotFound) {
return err
}
childNewLineage = append([]int64{int64(leftLastCommitID)}, parentLastLineage...)
Expand Down
6 changes: 3 additions & 3 deletions catalog/mvcc/cataloger_reset_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestCataloger_ResetEntry_NewToNone(t *testing.T) {
}
_, err := c.GetEntry(ctx, repository, MakeReference("master", UncommittedID), "/file1", catalog.GetEntryParams{})
expectedErr := db.ErrNotFound
if !errors.As(err, &expectedErr) {
if !errors.Is(err, expectedErr) {
t.Fatalf("ResetEntry expecting the file to be gone with %s, got = %s", expectedErr, err)
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestCataloger_ResetEntry_Committed(t *testing.T) {
}
err := c.ResetEntry(ctx, repository, "master", "/file1")
expectedErr := db.ErrNotFound
if !errors.As(err, &expectedErr) {
if !errors.Is(err, expectedErr) {
t.Fatal("ResetEntry expected not to find file in case nothing to reset: ", err)
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestCataloger_ResetEntry_CommittedParentBranch(t *testing.T) {
}
err = c.ResetEntry(ctx, repository, "b1", "/file1")
expectedErr := db.ErrNotFound
if !errors.As(err, &expectedErr) {
if !errors.Is(err, expectedErr) {
t.Fatal("ResetEntry expected not to find file in case nothing to reset:", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion catalog/mvcc/cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func testVerifyEntries(t testing.TB, ctx context.Context, c catalog.Cataloger, r
for _, entry := range entries {
ent, err := c.GetEntry(ctx, repository, reference, entry.Path, catalog.GetEntryParams{})
if entry.Deleted {
if !errors.As(err, &db.ErrNotFound) {
if !errors.Is(err, db.ErrNotFound) {
t.Fatalf("Get entry '%s', err = %s, expected not found", entry.Path, err)
}
} else {
Expand Down

0 comments on commit e9c88a9

Please sign in to comment.