1package git
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "testing"
9
10 "github.com/charmbracelet/soft-serve/git"
11)
12
13func TestPktline(t *testing.T) {
14 cases := []struct {
15 name string
16 in []byte
17 err error
18 out []byte
19 }{
20 {
21 name: "empty",
22 in: []byte{},
23 out: []byte("0005\n0000"),
24 },
25 {
26 name: "simple",
27 in: []byte("hello"),
28 out: []byte("000ahello\n0000"),
29 },
30 {
31 name: "newline",
32 in: []byte("hello\n"),
33 out: []byte("000bhello\n\n0000"),
34 },
35 {
36 name: "error",
37 err: fmt.Errorf("foobar"),
38 out: []byte("000fERR foobar\n0000"),
39 },
40 }
41
42 for _, c := range cases {
43 t.Run(c.name, func(t *testing.T) {
44 var out bytes.Buffer
45 if c.err == nil {
46 if err := WritePktline(&out, string(c.in)); err != nil {
47 t.Fatal(err)
48 }
49 } else {
50 if err := WritePktlineErr(&out, c.err); err != nil {
51 t.Fatal(err)
52 }
53 }
54
55 if !bytes.Equal(out.Bytes(), c.out) {
56 t.Errorf("expected %q, got %q", c.out, out.Bytes())
57 }
58 })
59 }
60}
61
62func TestEnsureWithinBad(t *testing.T) {
63 tmp := t.TempDir()
64 for _, f := range []string{
65 "..",
66 "../../../",
67 } {
68 if err := EnsureWithin(tmp, f); err == nil {
69 t.Errorf("EnsureWithin(%q, %q) => nil, want non-nil error", tmp, f)
70 }
71 }
72}
73
74func TestEnsureWithinGood(t *testing.T) {
75 tmp := t.TempDir()
76 for _, f := range []string{
77 tmp,
78 tmp + "/foo",
79 tmp + "/foo/bar",
80 } {
81 if err := EnsureWithin(tmp, f); err != nil {
82 t.Errorf("EnsureWithin(%q, %q) => %v, want nil error", tmp, f, err)
83 }
84 }
85}
86
87func TestEnsureDefaultBranchEmpty(t *testing.T) {
88 tmp := t.TempDir()
89 r, err := git.Init(tmp, false)
90 if err != nil {
91 t.Fatal(err)
92 }
93
94 if err := EnsureDefaultBranch(context.TODO(), r.Path); !errors.Is(err, ErrNoBranches) {
95 t.Errorf("EnsureDefaultBranch(%q) => %v, want ErrNoBranches", tmp, err)
96 }
97}