From 59f7abe7495f32af0a80b8dc8f1cb2dedd259a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Wed, 23 Mar 2016 17:40:52 +0100 Subject: [PATCH] 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. --- cmd/skopeo/main.go | 8 +++++++- cmd/skopeo/main_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 cmd/skopeo/main_test.go diff --git a/cmd/skopeo/main.go b/cmd/skopeo/main.go index 0062ad5a..afffd63e 100644 --- a/cmd/skopeo/main.go +++ b/cmd/skopeo/main.go @@ -17,7 +17,8 @@ const ( usage = `interact with registries` ) -func main() { +// createApp returns a cli.App to be run or tested. +func createApp() *cli.App { app := cli.NewApp() app.Name = "skopeo" if gitCommit != "" { @@ -67,6 +68,11 @@ func main() { standaloneSignCmd, standaloneVerifyCmd, } + return app +} + +func main() { + app := createApp() if err := app.Run(os.Args); err != nil { logrus.Fatal(err) } diff --git a/cmd/skopeo/main_test.go b/cmd/skopeo/main_test.go new file mode 100644 index 00000000..42df5069 --- /dev/null +++ b/cmd/skopeo/main_test.go @@ -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 +}