-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
203 lines (164 loc) · 4.62 KB
/
main_test.go
File metadata and controls
203 lines (164 loc) · 4.62 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bytes"
"io"
"log"
"os"
"strings"
"testing"
"time"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/teatest"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/muesli/termenv"
)
func init() {
// This is required for CI to pass. See https://charm.sh/blog/teatest/
lipgloss.SetColorProfile(termenv.Ascii)
}
func TestRepo(t *testing.T) {
// Bless. This seems to work really well! The next steps here will be to try
// load the model with these branch names
repo, err := git.PlainOpen("./testdata/TestRepo/")
if err != nil {
log.Print("Error: ", err)
}
var branches []string
branchIter, err := repo.Branches()
if err != nil {
log.Print("Error: ", err)
}
myFunc := func(ref *plumbing.Reference) error {
trim := strings.TrimPrefix(ref.Name().String(), "refs/heads/")
branches = append(branches, trim)
return nil
}
err = branchIter.ForEach(myFunc)
if err != nil {
log.Print(err)
}
model := initModel(repo, branches)
tm := teatest.NewTestModel(t, model, teatest.WithInitialTermSize(300, 100))
// Assert that the program, at some point, has the following byte string ... make a helper function?
teatest.WaitFor(t, tm.Output(),
func(bts []byte) bool {
return bytes.Contains(
bts,
[]byte("1. branch-1"),
)
},
)
moveNDownAndSelectBranch(tm, 1)
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
out, err := io.ReadAll(tm.FinalOutput(t))
if err != nil {
t.Error("Error reading from FinalOutput", err)
}
teatest.RequireEqualOutput(t, out)
}
func TestOutput(t *testing.T) {
// Tempdir to clone the repository
dir, err := os.MkdirTemp("", "test-directory")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
repo := initTmpGitRepository(dir)
branchNames := []string{
"JOB-62131/JOB-76475/add-location-timers-to-fms",
"JOB-62131/JOB-76477/store-feature-enablement",
"JOB-62131/JOB-77400/show-modal-dialogue-on-disablement",
}
createBranches(repo, branchNames)
model := initialModel(repo)
t.Run("Moving down once and selecting a branch", func(t *testing.T) {
tm := teatest.NewTestModel(t, model, teatest.WithInitialTermSize(300, 100))
// Assert that the program, at some point, has the following byte string ... make a helper function?
teatest.WaitFor(t, tm.Output(),
func(bts []byte) bool {
return bytes.Contains(
bts,
[]byte("1. JOB-62131/JOB-76475/add-location-timers-to-fms"),
)
},
)
moveNDownAndSelectBranch(tm, 1)
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
out, err := io.ReadAll(tm.FinalOutput(t))
if err != nil {
t.Error(err)
}
teatest.RequireEqualOutput(t, out)
})
t.Run("Moving down twice and selecting a branch", func(t *testing.T) {
tm := teatest.NewTestModel(t, model, teatest.WithInitialTermSize(300, 100))
teatest.WaitFor(t, tm.Output(),
func(bts []byte) bool {
return bytes.Contains(
bts,
[]byte("1. JOB-62131/JOB-76475/add-location-timers-to-fms"),
)
},
)
moveNDownAndSelectBranch(tm, 2)
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
out, err := io.ReadAll(tm.FinalOutput(t))
if err != nil {
t.Error(err)
}
teatest.RequireEqualOutput(t, out)
})
}
func initModel(repo *git.Repository, branches []string) Model {
var items []list.Item
for _, branch := range branches {
items = append(items, Item(branch))
}
l := list.New(items, ItemDelegate{}, DefaultWidth, ListHeight)
return Model{list: l, repo: repo}
}
func initialModel(repo *git.Repository) Model {
branches := []list.Item{
Item("JOB-62131/JOB-76475/add-location-timers-to-fms"),
Item("JOB-62131/JOB-76477/store-feature-enablement"),
Item("JOB-62131/JOB-77400/show-modal-dialogue-on-disablement"),
}
l := list.New(branches, ItemDelegate{}, DefaultWidth, ListHeight)
return Model{list: l, repo: repo}
}
func moveNDownAndSelectBranch(tm *teatest.TestModel, down int) {
for i := 0; i < down; i++ {
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("j"),
})
}
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
}
func createBranches(repo *git.Repository, branchNames []string) {
for _, b := range branchNames {
opts := &config.Branch{Name: b}
err := repo.CreateBranch(opts)
if err != nil {
log.Fatal(err)
}
}
}
func initTmpGitRepository(dir string) *git.Repository {
_, err := git.PlainInit(dir, false)
if err != nil {
log.Fatal(err)
}
repo, err := git.PlainOpen(dir)
if err != nil {
log.Fatal(err)
}
return repo
}