2014-03-20 23:56:41 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2014-03-21 00:54:18 -05:00
|
|
|
"errors"
|
2014-03-22 00:16:26 -05:00
|
|
|
"os"
|
2014-03-20 23:56:41 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiffTreeToTree(t *testing.T) {
|
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer repo.Free()
|
2014-03-22 00:16:26 -05:00
|
|
|
defer os.RemoveAll(repo.Workdir())
|
2014-03-20 23:56:41 -05:00
|
|
|
|
|
|
|
_, originalTreeId := seedTestRepo(t, repo)
|
|
|
|
originalTree, err := repo.LookupTree(originalTreeId)
|
|
|
|
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
_, newTreeId := updateReadme(t, repo, "file changed\n")
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
newTree, err := repo.LookupTree(newTreeId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2014-03-22 00:16:26 -05:00
|
|
|
callbackInvoked := false
|
|
|
|
opts := DiffOptions{
|
|
|
|
NotifyCallback: func(diffSoFar *Diff, delta DiffDelta, matchedPathSpec string) error {
|
|
|
|
callbackInvoked = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := repo.DiffTreeToTree(originalTree, newTree, &opts)
|
2014-03-20 23:56:41 -05:00
|
|
|
checkFatal(t, err)
|
2014-03-22 00:16:26 -05:00
|
|
|
if !callbackInvoked {
|
|
|
|
t.Fatal("callback not invoked")
|
|
|
|
}
|
2014-03-20 23:56:41 -05:00
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("no diff returned")
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
files := make([]string, 0)
|
2014-03-22 00:16:26 -05:00
|
|
|
hunks := make([]DiffHunk, 0)
|
|
|
|
lines := make([]DiffLine, 0)
|
|
|
|
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
2014-03-21 00:54:18 -05:00
|
|
|
files = append(files, file.OldFile.Path)
|
2014-03-22 00:16:26 -05:00
|
|
|
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
|
2014-03-21 19:20:48 -05:00
|
|
|
hunks = append(hunks, hunk)
|
2014-03-22 00:16:26 -05:00
|
|
|
return func(line DiffLine) error {
|
2014-03-21 19:20:48 -05:00
|
|
|
lines = append(lines, line)
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}, nil
|
2014-03-22 00:16:26 -05:00
|
|
|
}, DiffDetailLines)
|
2014-03-20 23:56:41 -05:00
|
|
|
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
if len(files) != 1 {
|
2014-03-20 23:56:41 -05:00
|
|
|
t.Fatal("Incorrect number of files in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if files[0] != "README" {
|
|
|
|
t.Fatal("File in diff was expected to be README")
|
|
|
|
}
|
2014-03-21 00:54:18 -05:00
|
|
|
|
2014-03-21 19:20:48 -05:00
|
|
|
if len(hunks) != 1 {
|
|
|
|
t.Fatal("Incorrect number of hunks in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hunks[0].OldStart != 1 || hunks[0].NewStart != 1 {
|
|
|
|
t.Fatal("Incorrect hunk")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines) != 2 {
|
|
|
|
t.Fatal("Incorrect number of lines in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines[0].Content != "foo\n" {
|
|
|
|
t.Fatal("Incorrect lines in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines[1].Content != "file changed\n" {
|
|
|
|
t.Fatal("Incorrect lines in diff")
|
|
|
|
}
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
errTest := errors.New("test error")
|
|
|
|
|
2014-03-22 00:16:26 -05:00
|
|
|
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
2014-03-21 19:20:48 -05:00
|
|
|
return nil, errTest
|
2014-03-22 00:16:26 -05:00
|
|
|
}, DiffDetailLines)
|
2014-03-21 00:54:18 -05:00
|
|
|
|
|
|
|
if err != errTest {
|
|
|
|
t.Fatal("Expected custom error to be returned")
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
}
|