forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
40 lines (34 loc) · 985 Bytes
/
testing.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cmdtest
import (
"github.com/juju/cmd/v3"
"github.com/juju/cmd/v3/cmdtesting"
"github.com/juju/juju/provider/dummy"
)
// RunCommandWithDummyProvider runs the command and returns channels holding the
// command's operations and errors.
func RunCommandWithDummyProvider(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
if ctx == nil {
panic("ctx == nil")
}
errc = make(chan error, 1)
opc = make(chan dummy.Operation, 200)
dummy.Listen(opc)
go func() {
defer func() {
// signal that we're done with this ops channel.
dummy.Listen(nil)
// now that dummy is no longer going to send ops on
// this channel, close it to signal to test cases
// that we are done.
close(opc)
}()
if err := cmdtesting.InitCommand(com, args); err != nil {
errc <- err
return
}
errc <- com.Run(ctx)
}()
return
}