From 5d8eaf7e65c404a0d10d3705697dd99369630dda Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 5 Dec 2020 13:13:59 -0800 Subject: Refactor all callbacks (#700) This change is a preparation for another change that makes all callback types return a Go error instead of an error code / an integer. That is going to make make things a lot more idiomatic. The reason this change is split is threefold: a) This change is mostly mechanical and should contain no semantic changes. b) This change is backwards-compatible (in the Go API compatibility sense of the word), and thus can be backported to all other releases. c) It makes the other change a bit smaller and more focused on just one thing. Concretely, this change makes all callbacks populate a Go error when they fail. If the callback is invoked from the same stack as the function to which it was passed (e.g. for `Tree.Walk`), it will preserve the error object directly into a struct that also holds the callback function. Otherwise if the callback is pased to one func and will be invoked when run from another one (e.g. for `Repository.InitRebase`), the error string is saved into the libgit2 thread-local storage and then re-created as a `GitError`. --- diff_test.go | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'diff_test.go') diff --git a/diff_test.go b/diff_test.go index 3a1e65e..1e3cd5c 100644 --- a/diff_test.go +++ b/diff_test.go @@ -173,9 +173,8 @@ func TestDiffTreeToTree(t *testing.T) { }, DiffDetailLines) if err != errTest { - t.Fatal("Expected custom error to be returned") + t.Fatalf("Expected custom error to be returned, got %v, want %v", err, errTest) } - } func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) { @@ -486,13 +485,15 @@ func TestApplyToTree(t *testing.T) { diffAC, err := repo.DiffTreeToTree(treeA, treeC, nil) checkFatal(t, err) + errMessageDropped := errors.New("message dropped") + for _, tc := range []struct { name string tree *Tree diff *Diff applyHunkCallback ApplyHunkCallback applyDeltaCallback ApplyDeltaCallback - error error + err error expectedDiff *Diff }{ { @@ -505,7 +506,7 @@ func TestApplyToTree(t *testing.T) { name: "applying a conflicting patch errors", tree: treeB, diff: diffAC, - error: &GitError{ + err: &GitError{ Message: "hunk at line 1 did not apply", Code: ErrorCodeApplyFail, Class: ErrorClassPatch, @@ -529,12 +530,8 @@ func TestApplyToTree(t *testing.T) { name: "hunk callback erroring fails the call", tree: treeA, diff: diffAB, - applyHunkCallback: func(*DiffHunk) (bool, error) { return true, errors.New("message dropped") }, - error: &GitError{ - Message: "Generic", - Code: ErrorCodeGeneric, - Class: ErrorClassInvalid, - }, + applyHunkCallback: func(*DiffHunk) (bool, error) { return true, errMessageDropped }, + err: errMessageDropped, }, { name: "delta callback returning false does not apply", @@ -546,12 +543,8 @@ func TestApplyToTree(t *testing.T) { name: "delta callback erroring fails the call", tree: treeA, diff: diffAB, - applyDeltaCallback: func(*DiffDelta) (bool, error) { return true, errors.New("message dropped") }, - error: &GitError{ - Message: "Generic", - Code: ErrorCodeGeneric, - Class: ErrorClassInvalid, - }, + applyDeltaCallback: func(*DiffDelta) (bool, error) { return true, errMessageDropped }, + err: errMessageDropped, }, } { t.Run(tc.name, func(t *testing.T) { @@ -562,9 +555,9 @@ func TestApplyToTree(t *testing.T) { opts.ApplyDeltaCallback = tc.applyDeltaCallback index, err := repo.ApplyToTree(tc.diff, tc.tree, opts) - if tc.error != nil { - if !reflect.DeepEqual(err, tc.error) { - t.Fatalf("expected error %q but got %q", tc.error, err) + if tc.err != nil { + if !reflect.DeepEqual(tc.err, err) { + t.Fatalf("expected error %q but got %q", tc.err, err) } return -- cgit v1.2.3