repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

Eric Bower  ·  2024-07-23

util_test.go

 1package git
 2
 3import (
 4	"fmt"
 5	"io"
 6	"os"
 7	"testing"
 8)
 9
10func TestParsePatchsetWithCover(t *testing.T) {
11	file, err := os.Open("fixtures/with-cover.patch")
12	defer func() {
13		_ = file.Close()
14	}()
15	if err != nil {
16		t.Fatal(err.Error())
17	}
18	actual, err := ParsePatchset(file)
19	if err != nil {
20		t.Fatal(err.Error())
21	}
22	expected := []*Patch{
23		{Title: "Add torch deps"},
24		{Title: "feat: lets build an rnn"},
25		{Title: "chore: add torch to requirements"},
26	}
27	if len(actual) != len(expected) {
28		t.Fatalf("patches not same length (expected:%d, actual:%d)\n", len(expected), len(actual))
29	}
30	for idx, act := range actual {
31		exp := expected[idx]
32		if exp.Title != act.Title {
33			t.Fatalf("title does not match expected (expected:%s, actual:%s)", exp.Title, act.Title)
34		}
35	}
36}
37
38func TestPatchToDiff(t *testing.T) {
39	file, err := os.Open("fixtures/single.patch")
40	defer func() {
41		_ = file.Close()
42	}()
43	if err != nil {
44		t.Fatal(err.Error())
45	}
46
47	fileExp, err := os.Open("fixtures/single.diff")
48	defer func() {
49		_ = file.Close()
50	}()
51	if err != nil {
52		t.Fatal(err.Error())
53	}
54
55	actual, err := patchToDiff(file)
56	if err != nil {
57		t.Fatal(err.Error())
58	}
59
60	by, err := io.ReadAll(fileExp)
61	if err != nil {
62		t.Fatal("cannot read expected file")
63	}
64
65	if actual != string(by) {
66		fmt.Println(actual)
67		t.Fatal("diff does not match expected")
68	}
69}