-
Notifications
You must be signed in to change notification settings - Fork 17
/
branch_submit_test.go
60 lines (50 loc) · 1.7 KB
/
branch_submit_test.go
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
package main
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/logtest"
)
func TestBranchSubmit_listChangeTemplates(t *testing.T) {
t.Run("NoTimeout", func(t *testing.T) {
log := logtest.New(t)
ctx := context.Background()
tmpl := &forge.ChangeTemplate{}
svc := &spiceTemplateServiceStub{
ListChangeTemplatesF: func(ctx context.Context, _ string, _ forge.Repository) ([]*forge.ChangeTemplate, error) {
_, ok := ctx.Deadline()
require.False(t, ok, "context should not have a deadline")
return []*forge.ChangeTemplate{tmpl}, nil
},
}
got := (&branchSubmitCmd{}).listChangeTemplates(ctx, log, svc, "origin", nil)
if assert.Len(t, got, 1) {
assert.Same(t, tmpl, got[0])
}
})
t.Run("Timeout", func(t *testing.T) {
log := logtest.New(t)
ctx := context.Background()
svc := &spiceTemplateServiceStub{
ListChangeTemplatesF: func(ctx context.Context, _ string, _ forge.Repository) ([]*forge.ChangeTemplate, error) {
_, ok := ctx.Deadline()
require.True(t, ok, "context should have a deadline")
return nil, errors.New("great sadness")
},
}
got := (&branchSubmitCmd{
ListTemplatesTimeout: time.Second,
}).listChangeTemplates(ctx, log, svc, "origin", nil)
assert.Empty(t, got)
})
}
type spiceTemplateServiceStub struct {
ListChangeTemplatesF func(context.Context, string, forge.Repository) ([]*forge.ChangeTemplate, error)
}
func (s *spiceTemplateServiceStub) ListChangeTemplates(ctx context.Context, remoteName string, fr forge.Repository) ([]*forge.ChangeTemplate, error) {
return s.ListChangeTemplatesF(ctx, remoteName, fr)
}