summaryrefslogtreecommitdiffstats
path: root/cmd/platform/cmdtestlib.go
diff options
context:
space:
mode:
authorHarrison Healey <harrisonmhealey@gmail.com>2017-04-15 13:45:22 -0400
committerChristopher Speller <crspeller@gmail.com>2017-04-15 13:45:22 -0400
commit461a0b3b7c14cd59cb53eb66f419c965ab3bdd24 (patch)
tree88eea69d8963214fa43e8bf1a9989f2ab3d1c521 /cmd/platform/cmdtestlib.go
parent24667e3e5423dc939770d0b4bf06ed2f42b4a445 (diff)
downloadchat-461a0b3b7c14cd59cb53eb66f419c965ab3bdd24.tar.gz
chat-461a0b3b7c14cd59cb53eb66f419c965ab3bdd24.tar.bz2
chat-461a0b3b7c14cd59cb53eb66f419c965ab3bdd24.zip
PLT-6113 Added initial unit tests for cmd package (#6086)
* Fixed app.CreateUser not using the provided locale * Added initial unit tests for cmd package * Disabled unit tests while we move to 'go build'
Diffstat (limited to 'cmd/platform/cmdtestlib.go')
-rw-r--r--cmd/platform/cmdtestlib.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/cmd/platform/cmdtestlib.go b/cmd/platform/cmdtestlib.go
new file mode 100644
index 000000000..16bfc2b21
--- /dev/null
+++ b/cmd/platform/cmdtestlib.go
@@ -0,0 +1,45 @@
+// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package main
+
+import (
+ "bytes"
+ "strings"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/pflag"
+)
+
+func runCommand(argString string) error {
+ err, _ := runCommandWithOutput(argString)
+
+ return err
+}
+
+func runCommandWithOutput(argString string) (error, string) {
+ // Set arguments on the root command
+ rootCmd.SetArgs(strings.Split(argString, " "))
+ defer rootCmd.SetArgs([]string{})
+
+ output := new(bytes.Buffer)
+ rootCmd.SetOutput(output)
+ defer rootCmd.SetOutput(nil)
+
+ // Executing the root command will call the necessary subcommand
+ cmd, err := rootCmd.ExecuteC()
+
+ // And clear the arguments on the subcommand that was actually called since they'd otherwise
+ // be used on the next call to this command
+ clearArgs(cmd)
+
+ return err, output.String()
+}
+
+func clearArgs(command *cobra.Command) {
+ command.Flags().VisitAll(clearFlag)
+}
+
+func clearFlag(flag *pflag.Flag) {
+ flag.Value.Set(flag.DefValue)
+}