Skip to content

Commit 686c2b7

Browse files
committed
Adding tests
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent eb33f76 commit 686c2b7

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

cli/cliui/provisionerjob_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cliui_test
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"os"
8+
"regexp"
79
"runtime"
810
"sync"
911
"testing"
@@ -79,6 +81,70 @@ func TestProvisionerJob(t *testing.T) {
7981
test.PTY.ExpectMatch("Something")
8082
})
8183

84+
t.Run("Queue Position", func(t *testing.T) {
85+
t.Parallel()
86+
87+
stage := cliui.ProvisioningStateQueued
88+
89+
tests := []struct {
90+
name string
91+
queuePos int
92+
expected string
93+
}{
94+
{
95+
name: "first",
96+
queuePos: 0,
97+
expected: fmt.Sprintf("%s$", stage),
98+
},
99+
{
100+
name: "next",
101+
queuePos: 1,
102+
expected: fmt.Sprintf(`%s %s$`, stage, regexp.QuoteMeta("(next)")),
103+
},
104+
{
105+
name: "other",
106+
queuePos: 4,
107+
expected: fmt.Sprintf(`%s %s$`, stage, regexp.QuoteMeta("(position: 4)")),
108+
},
109+
}
110+
111+
for _, tc := range tests {
112+
tc := tc
113+
114+
t.Run(tc.name, func(t *testing.T) {
115+
t.Parallel()
116+
117+
test := newProvisionerJob(t)
118+
test.JobMutex.Lock()
119+
test.Job.QueuePosition = tc.queuePos
120+
test.Job.QueueSize = tc.queuePos
121+
test.JobMutex.Unlock()
122+
123+
go func() {
124+
<-test.Next
125+
test.JobMutex.Lock()
126+
test.Job.Status = codersdk.ProvisionerJobRunning
127+
now := dbtime.Now()
128+
test.Job.StartedAt = &now
129+
test.JobMutex.Unlock()
130+
<-test.Next
131+
test.JobMutex.Lock()
132+
test.Job.Status = codersdk.ProvisionerJobSucceeded
133+
now = dbtime.Now()
134+
test.Job.CompletedAt = &now
135+
close(test.Logs)
136+
test.JobMutex.Unlock()
137+
}()
138+
test.PTY.ExpectRegexMatch(tc.expected)
139+
test.Next <- struct{}{}
140+
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued) // step completed
141+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
142+
test.Next <- struct{}{}
143+
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
144+
})
145+
}
146+
})
147+
82148
// This cannot be ran in parallel because it uses a signal.
83149
// nolint:paralleltest
84150
t.Run("Cancel", func(t *testing.T) {

pty/ptytest/ptytest.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"io"
9+
"regexp"
910
"runtime"
1011
"strings"
1112
"sync"
@@ -145,16 +146,36 @@ type outExpecter struct {
145146
}
146147

147148
func (e *outExpecter) ExpectMatch(str string) string {
149+
return e.expectMatchContext(str, e.ExpectMatchContext)
150+
}
151+
152+
func (e *outExpecter) ExpectRegexMatch(str string) string {
153+
return e.expectMatchContext(str, e.ExpectRegexMatchContext)
154+
}
155+
156+
func (e *outExpecter) expectMatchContext(str string, impl func(ctx context.Context, str string) string) string {
148157
e.t.Helper()
149158

150159
timeout, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
151160
defer cancel()
152161

153-
return e.ExpectMatchContext(timeout, str)
162+
return impl(timeout, str)
154163
}
155164

156165
// TODO(mafredri): Rename this to ExpectMatch when refactoring.
157166
func (e *outExpecter) ExpectMatchContext(ctx context.Context, str string) string {
167+
return e.expectMatcher(ctx, str, func(src, pattern string) bool {
168+
return strings.Contains(src, pattern)
169+
})
170+
}
171+
172+
func (e *outExpecter) ExpectRegexMatchContext(ctx context.Context, str string) string {
173+
return e.expectMatcher(ctx, str, func(src, pattern string) bool {
174+
return regexp.MustCompile(pattern).MatchString(src)
175+
})
176+
}
177+
178+
func (e *outExpecter) expectMatcher(ctx context.Context, str string, matchFn func(src, pattern string) bool) string {
158179
e.t.Helper()
159180

160181
var buffer bytes.Buffer
@@ -168,7 +189,7 @@ func (e *outExpecter) ExpectMatchContext(ctx context.Context, str string) string
168189
if err != nil {
169190
return err
170191
}
171-
if strings.Contains(buffer.String(), str) {
192+
if matchFn(buffer.String(), str) {
172193
return nil
173194
}
174195
}

0 commit comments

Comments
 (0)