Add infrastructure for testing cli.Command.Action handlers

Also split creation of cli.App from main(), and add a test helper
function.

This does not change behavior at the moment, but will allow writing
tests of the command handlers.
This commit is contained in:
Miloslav Trmač
2016-03-23 17:40:52 +01:00
parent 6bec0699cb
commit 59f7abe749
2 changed files with 21 additions and 1 deletions

View File

@@ -17,7 +17,8 @@ const (
usage = `interact with registries` usage = `interact with registries`
) )
func main() { // createApp returns a cli.App to be run or tested.
func createApp() *cli.App {
app := cli.NewApp() app := cli.NewApp()
app.Name = "skopeo" app.Name = "skopeo"
if gitCommit != "" { if gitCommit != "" {
@@ -67,6 +68,11 @@ func main() {
standaloneSignCmd, standaloneSignCmd,
standaloneVerifyCmd, standaloneVerifyCmd,
} }
return app
}
func main() {
app := createApp()
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }

14
cmd/skopeo/main_test.go Normal file
View File

@@ -0,0 +1,14 @@
package main
import "bytes"
// runSkopeo creates an app object and runs it with args, with an implied first "skopeo".
// Returns output intended for stdout and the returned error, if any.
func runSkopeo(args ...string) (string, error) {
app := createApp()
stdout := bytes.Buffer{}
app.Writer = &stdout
args = append([]string{"skopeo"}, args...)
err := app.Run(args)
return stdout.String(), err
}