all repos

mugit @ c543efd

🐮 git server that your cow will love

mugit/internal/git/config_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
run errcheck, 28 days ago
1
package git
2
3
import (
4
	"os"
5
	"path/filepath"
6
	"testing"
7
8
	"olexsmir.xyz/x/is"
9
)
10
11
func TestRepo_IsPrivate(t *testing.T) {
12
	t.Run("default is not private", func(t *testing.T) {
13
		tr := newTestRepo(t)
14
		private, err := tr.open().IsPrivate()
15
		is.Equal(t, private, false)
16
		is.Err(t, err, nil)
17
	})
18
19
	t.Run("set to private", func(t *testing.T) {
20
		r := newTestRepo(t).open()
21
		is.Err(t, r.SetPrivate(true), nil)
22
23
		private, err := r.IsPrivate()
24
		is.Err(t, err, nil)
25
		is.Equal(t, private, true)
26
	})
27
28
	t.Run("can be set back to public", func(t *testing.T) {
29
		r := newTestRepo(t).open()
30
31
		is.Err(t, r.SetPrivate(true), nil)
32
		is.Err(t, r.SetPrivate(false), nil)
33
34
		private, err := r.IsPrivate()
35
		is.Equal(t, private, false)
36
		is.Err(t, err, nil)
37
	})
38
}
39
40
func TestRepo_Description(t *testing.T) {
41
	t.Run("default description is empty description", func(t *testing.T) {
42
		r := newTestRepo(t)
43
		r.commitFile("dummy.txt", "dummy", "Initial commit")
44
45
		descPath := filepath.Join(r.path, ".git", "description")
46
		_ = os.WriteFile(descPath, []byte(defaultDescription), 0o644)
47
48
		desc, err := r.open().Description()
49
		is.Err(t, err, nil)
50
		is.Equal(t, desc, "")
51
	})
52
53
	t.Run("empty description returns empty string", func(t *testing.T) {
54
		r := newTestRepo(t)
55
		r.commitFile("dummy.txt", "dummy", "Initial commit")
56
57
		desc, err := r.open().Description()
58
		is.Err(t, err, nil)
59
		is.Equal(t, desc, "")
60
	})
61
62
	t.Run("default git description is treated as empty", func(t *testing.T) {
63
		r := newTestRepo(t)
64
		r.commitFile("dummy.txt", "dummy", "Initial commit")
65
66
		// Write the default git description to the .git directory
67
		descPath := filepath.Join(r.path, ".git", "description")
68
		_ = os.WriteFile(descPath, []byte(defaultDescription), 0o644)
69
70
		desc, err := r.open().Description()
71
		is.Err(t, err, nil)
72
		is.Equal(t, desc, "")
73
	})
74
75
	t.Run("set and get description", func(t *testing.T) {
76
		r := newTestRepo(t)
77
		r.commitFile("dummy.txt", "dummy", "Initial commit")
78
79
		repo := r.open()
80
		err := repo.SetDescription("My awesome project")
81
		is.Err(t, err, nil)
82
83
		desc, err := repo.Description()
84
		is.Err(t, err, nil)
85
		is.Equal(t, desc, "My awesome project")
86
	})
87
88
	t.Run("description with newlines", func(t *testing.T) {
89
		r := newTestRepo(t)
90
		r.commitFile("dummy.txt", "dummy", "Initial commit")
91
92
		repo := r.open()
93
		multiLine := "My project\n\nWith multiple lines\nof description."
94
		err := repo.SetDescription(multiLine)
95
		is.Err(t, err, nil)
96
97
		desc, err := repo.Description()
98
		is.Err(t, err, nil)
99
		is.Equal(t, desc, multiLine)
100
	})
101
}
102
103
func TestRepo_Mirror(t *testing.T) {
104
	t.Run("repo without origin remote can't be a mirror", func(t *testing.T) {
105
		r := newTestRepo(t)
106
		r.commitFile("dummy.txt", "dummy", "Initial commit")
107
108
		_, err := r.open().IsMirror()
109
		is.Err(t, err, "failed to get remote: ")
110
	})
111
112
	t.Run("set mirror remote", func(t *testing.T) {
113
		r := newTestRepo(t).open()
114
115
		expectedURL := "https://github.com/example/repo.git"
116
		err := r.SetMirrorRemote(expectedURL)
117
		is.Err(t, err, nil)
118
119
		isMirror, err := r.IsMirror()
120
		is.Equal(t, isMirror, true)
121
		is.Err(t, err, nil)
122
123
		url, err := r.RemoteURL()
124
		is.Err(t, err, nil)
125
		is.Equal(t, url, expectedURL)
126
	})
127
}