summaryrefslogtreecommitdiff
path: root/diff_test.go
diff options
context:
space:
mode:
authorlhchavez <[email protected]>2020-12-05 13:13:59 -0800
committerGitHub <[email protected]>2020-12-05 13:13:59 -0800
commit5d8eaf7e65c404a0d10d3705697dd99369630dda (patch)
tree85e2f17a8c3ee1fe3ec6a6e680237907ec8dc638 /diff_test.go
parent137c05e802d5e11a5ab54809bc8be8f61ccece21 (diff)
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`.
Diffstat (limited to 'diff_test.go')
-rw-r--r--diff_test.go31
1 files changed, 12 insertions, 19 deletions
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