github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/cmdtest/testing.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmdtest
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  
    10  	"github.com/juju/juju/provider/dummy"
    11  )
    12  
    13  // RunCommandWithDummyProvider runs the command and returns channels holding the
    14  // command's operations and errors.
    15  func RunCommandWithDummyProvider(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
    16  	if ctx == nil {
    17  		panic("ctx == nil")
    18  	}
    19  	errc = make(chan error, 1)
    20  	opc = make(chan dummy.Operation, 200)
    21  	dummy.Listen(opc)
    22  	go func() {
    23  		defer func() {
    24  			// signal that we're done with this ops channel.
    25  			dummy.Listen(nil)
    26  			// now that dummy is no longer going to send ops on
    27  			// this channel, close it to signal to test cases
    28  			// that we are done.
    29  			close(opc)
    30  		}()
    31  
    32  		if err := cmdtesting.InitCommand(com, args); err != nil {
    33  			errc <- err
    34  			return
    35  		}
    36  
    37  		errc <- com.Run(ctx)
    38  	}()
    39  	return
    40  }