launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/testbase/cmd.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testbase
     5  
     6  import (
     7  	"os/exec"
     8  )
     9  
    10  // HookCommandOutput intercepts CommandOutput to a function that passes the
    11  // actual command and it's output back via a channel, and returns the error
    12  // passed into this function.  It also returns a cleanup function so you can
    13  // restore the original function
    14  func HookCommandOutput(
    15  	outputFunc *func(cmd *exec.Cmd) ([]byte, error), output []byte, err error) (<-chan *exec.Cmd, func()) {
    16  
    17  	cmdChan := make(chan *exec.Cmd, 1)
    18  	origCommandOutput := *outputFunc
    19  	cleanup := func() {
    20  		*outputFunc = origCommandOutput
    21  	}
    22  	*outputFunc = func(cmd *exec.Cmd) ([]byte, error) {
    23  		cmdChan <- cmd
    24  		return output, err
    25  	}
    26  	return cmdChan, cleanup
    27  }