Skip to content

Commit 35d61a0

Browse files
committed
Add basic tests for patch formatting
For now, assert that we produce exactly the same output as the original parsed input. I think these tests would ideally assert that the re-parsed value is the same, but it's kind of annoying to compare File objects without adding an external dependency.
1 parent fe252ed commit 35d61a0

9 files changed

+125
-0
lines changed

gitdiff/gitdiff_string_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package gitdiff
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestFile_String(t *testing.T) {
9+
sources := []string{
10+
"testdata/string/delete.patch",
11+
"testdata/string/mode.patch",
12+
"testdata/string/modify.patch",
13+
"testdata/string/new.patch",
14+
"testdata/string/new_empty.patch",
15+
"testdata/string/new_mode.patch",
16+
"testdata/string/rename.patch",
17+
"testdata/string/rename_modify.patch",
18+
}
19+
20+
for _, src := range sources {
21+
b, err := os.ReadFile(src)
22+
if err != nil {
23+
t.Fatalf("failed to read %s: %v", src, err)
24+
}
25+
26+
original := assertParseSingleFile(t, src, b)
27+
str := original.String()
28+
29+
if string(b) != str {
30+
t.Errorf("%s: incorrect patch\nexpected: %q\n actual: %q\n", src, string(b), str)
31+
}
32+
}
33+
}

gitdiff/testdata/string/delete.patch

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/file.txt b/file.txt
2+
deleted file mode 100644
3+
index c9e9e05..0000000
4+
--- a/file.txt
5+
+++ /dev/null
6+
@@ -1,10 +0,0 @@
7+
-one
8+
-two
9+
-three
10+
-four
11+
-five
12+
-six
13+
-seven
14+
-eight
15+
-nine
16+
-ten

gitdiff/testdata/string/mode.patch

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
diff --git a/file.txt b/file.txt
2+
old mode 100644
3+
new mode 100755

gitdiff/testdata/string/modify.patch

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/file.txt b/file.txt
2+
index c9e9e05..7d5fdc6 100644
3+
--- a/file.txt
4+
+++ b/file.txt
5+
@@ -3,8 +3,10 @@ two
6+
three
7+
four
8+
five
9+
-six
10+
+six six six six six six
11+
seven
12+
eight
13+
nine
14+
ten
15+
+eleven
16+
+twelve

gitdiff/testdata/string/new.patch

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/file.txt b/file.txt
2+
new file mode 100644
3+
index 0000000..c9e9e05
4+
--- /dev/null
5+
+++ b/file.txt
6+
@@ -0,0 +1,10 @@
7+
+one
8+
+two
9+
+three
10+
+four
11+
+five
12+
+six
13+
+seven
14+
+eight
15+
+nine
16+
+ten
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
diff --git a/file.txt b/file.txt
2+
new file mode 100644
3+
index 0000000..e69de29
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/file.sh b/file.sh
2+
new file mode 100755
3+
index 0000000..c9e9e05
4+
--- /dev/null
5+
+++ b/file.sh
6+
@@ -0,0 +1,10 @@
7+
+one
8+
+two
9+
+three
10+
+four
11+
+five
12+
+six
13+
+seven
14+
+eight
15+
+nine
16+
+ten

gitdiff/testdata/string/rename.patch

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
diff --git a/file.txt b/numbers.txt
2+
similarity index 100%
3+
rename from file.txt
4+
rename to numbers.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/file.txt b/numbers.txt
2+
similarity index 77%
3+
rename from file.txt
4+
rename to numbers.txt
5+
index c9e9e05..a6b31d6 100644
6+
--- a/file.txt
7+
+++ b/numbers.txt
8+
@@ -3,8 +3,9 @@ two
9+
three
10+
four
11+
five
12+
-six
13+
+ six
14+
seven
15+
eight
16+
nine
17+
ten
18+
+eleven

0 commit comments

Comments
 (0)