-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_test.go
83 lines (63 loc) · 1.95 KB
/
diff_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package git_test
import (
"testing"
git "github.com/purpleclay/gitz"
"github.com/purpleclay/gitz/gittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDiff(t *testing.T) {
gittest.InitRepository(t,
gittest.WithCommittedFiles("main.go"),
gittest.WithFileContent("main.go", `package main
import "fmt"
func print() {
fmt.Println("Hello, World!")
}
func main() {
print()
}`))
overwriteFile(t, "main.go", `package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf("Hello, %s\n" + os.Args[1])
}`)
client, _ := git.NewClient()
diffs, err := client.Diff()
require.NoError(t, err)
require.Len(t, diffs, 1)
assert.Equal(t, "main.go", diffs[0].Path)
require.Len(t, diffs[0].Chunks, 2)
assert.Equal(t, 3, diffs[0].Chunks[0].Added.LineNo)
assert.Equal(t, 4, diffs[0].Chunks[0].Added.Count)
assert.Equal(t, `import (
"fmt"
"os"
)`, diffs[0].Chunks[0].Added.Change)
assert.Equal(t, 3, diffs[0].Chunks[0].Removed.LineNo)
assert.Equal(t, 5, diffs[0].Chunks[0].Removed.Count)
assert.Equal(t, `import "fmt"
func print() {
fmt.Println("Hello, World!")
}`, diffs[0].Chunks[0].Removed.Change)
assert.Equal(t, 9, diffs[0].Chunks[1].Added.LineNo)
assert.Equal(t, 1, diffs[0].Chunks[1].Added.Count)
assert.Equal(t, ` fmt.Printf("Hello, %s\n" + os.Args[1])`, diffs[0].Chunks[1].Added.Change)
assert.Equal(t, 10, diffs[0].Chunks[1].Removed.LineNo)
assert.Equal(t, 1, diffs[0].Chunks[1].Removed.Count)
assert.Equal(t, ` print()`, diffs[0].Chunks[1].Removed.Change)
}
func TestDiffWithDiffPaths(t *testing.T) {
gittest.InitRepository(t,
gittest.WithCommittedFiles("file1.txt", "file2.txt"),
gittest.WithFileContent("file1.txt", "Hello, World!", "file2.txt", "Goodbye, World!"))
overwriteFile(t, "file1.txt", "Goodbye, World!")
overwriteFile(t, "file2.txt", "Hello, World!")
client, _ := git.NewClient()
diffs, err := client.Diff(git.WithDiffPaths("file1.txt"))
require.NoError(t, err)
assert.Len(t, diffs, 1)
}