Skip to content

Commit 08f84b6

Browse files
authoredApr 20, 2023
Merge branch 'coder:main' into main
2 parents 05b8034 + 38a6d54 commit 08f84b6

File tree

68 files changed

+2618
-1275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2618
-1275
lines changed
 

‎cli/clibase/option.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ func (s *OptionSet) Add(opts ...Option) {
8080
*s = append(*s, opts...)
8181
}
8282

83+
// Filter will only return options that match the given filter. (return true)
84+
func (s OptionSet) Filter(filter func(opt Option) bool) OptionSet {
85+
cpy := make(OptionSet, 0)
86+
for _, opt := range s {
87+
if filter(opt) {
88+
cpy = append(cpy, opt)
89+
}
90+
}
91+
return cpy
92+
}
93+
8394
// FlagSet returns a pflag.FlagSet for the OptionSet.
8495
func (s *OptionSet) FlagSet() *pflag.FlagSet {
8596
if s == nil {

‎cli/clitest/clitest.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func extractTar(t *testing.T, data []byte, directory string) {
127127
}
128128
}
129129

130-
// Start runs the command in a goroutine and cleans it up when
131-
// the test completed.
130+
// Start runs the command in a goroutine and cleans it up when the test
131+
// completed.
132132
func Start(t *testing.T, inv *clibase.Invocation) {
133133
t.Helper()
134134

@@ -170,7 +170,7 @@ func (w *ErrorWaiter) Wait() error {
170170
var ok bool
171171
w.cachedError, ok = <-w.c
172172
if !ok {
173-
panic("unexpoected channel close")
173+
panic("unexpected channel close")
174174
}
175175
})
176176
return w.cachedError
@@ -196,18 +196,18 @@ func (w *ErrorWaiter) RequireAs(want interface{}) {
196196
require.ErrorAs(w.t, w.Wait(), want)
197197
}
198198

199-
// StartWithWaiter runs the command in a goroutine but returns the error
200-
// instead of asserting it. This is useful for testing error cases.
199+
// StartWithWaiter runs the command in a goroutine but returns the error instead
200+
// of asserting it. This is useful for testing error cases.
201201
func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
202202
t.Helper()
203203

204-
errCh := make(chan error, 1)
205-
206-
var cleaningUp atomic.Bool
207-
208204
var (
209205
ctx = inv.Context()
210206
cancel func()
207+
208+
cleaningUp atomic.Bool
209+
errCh = make(chan error, 1)
210+
doneCh = make(chan struct{})
211211
)
212212
if _, ok := ctx.Deadline(); !ok {
213213
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(testutil.WaitMedium))
@@ -218,12 +218,13 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
218218
inv = inv.WithContext(ctx)
219219

220220
go func() {
221+
defer close(doneCh)
221222
defer close(errCh)
222223
err := inv.Run()
223224
if cleaningUp.Load() && errors.Is(err, context.DeadlineExceeded) {
224-
// If we're cleaning up, this error is likely related to the
225-
// CLI teardown process. E.g., the server could be slow to shut
226-
// down Postgres.
225+
// If we're cleaning up, this error is likely related to the CLI
226+
// teardown process. E.g., the server could be slow to shut down
227+
// Postgres.
227228
t.Logf("command %q timed out during test cleanup", inv.Command.FullName())
228229
}
229230
// Whether or not this fails the test is left to the caller.
@@ -235,7 +236,7 @@ func StartWithWaiter(t *testing.T, inv *clibase.Invocation) *ErrorWaiter {
235236
t.Cleanup(func() {
236237
cancel()
237238
cleaningUp.Store(true)
238-
<-errCh
239+
<-doneCh
239240
})
240241
return &ErrorWaiter{c: errCh, t: t}
241242
}

0 commit comments

Comments
 (0)