From 4b2965f3d6de583fc67075b22b328ea5e3f54b71 Mon Sep 17 00:00:00 2001 From: Brian Meyers Date: Fri, 22 Feb 2019 13:38:36 -0500 Subject: [PATCH] Fixed tests and added dep --- .gitignore | 1 + Gopkg.lock | 173 ++++++++++++++++++++++++++++++ Gopkg.toml | 54 ++++++++++ cobra/cmd/add.go | 3 +- cobra/cmd/add_test.go | 6 +- cobra/cmd/flag_helpers.go | 6 +- cobra/cmd/init.go | 3 +- cobra/cmd/init_test.go | 6 +- cobra/cmd/testdata/main.go.golden | 2 +- cobra/cmd/testdata/root.go.golden | 54 +++------- cobra/cmd/testdata/test.go.golden | 33 ++---- 11 files changed, 263 insertions(+), 78 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml diff --git a/.gitignore b/.gitignore index 24ec322f..9421fef0 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ tags cobra.test .idea +vendor diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 00000000..0115e1a4 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,173 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:7cb4fdca4c251b3ef8027c90ea35f70c7b661a593b9eeae34753c65499098bb1" + name = "github.com/cpuguy83/go-md2man" + packages = ["md2man"] + pruneopts = "UT" + revision = "20f5889cbdc3c73dbd2862796665e7c465ade7d1" + version = "v1.0.8" + +[[projects]] + digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd" + name = "github.com/fsnotify/fsnotify" + packages = ["."] + pruneopts = "UT" + revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9" + version = "v1.4.7" + +[[projects]] + digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10" + name = "github.com/hashicorp/hcl" + packages = [ + ".", + "hcl/ast", + "hcl/parser", + "hcl/printer", + "hcl/scanner", + "hcl/strconv", + "hcl/token", + "json/parser", + "json/scanner", + "json/token", + ] + pruneopts = "UT" + revision = "8cb6e5b959231cc1119e43259c4a608f9c51a241" + version = "v1.0.0" + +[[projects]] + digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be" + name = "github.com/inconshreveable/mousetrap" + packages = ["."] + pruneopts = "UT" + revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" + version = "v1.0" + +[[projects]] + digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7" + name = "github.com/magiconair/properties" + packages = ["."] + pruneopts = "UT" + revision = "c2353362d570a7bfa228149c62842019201cfb71" + version = "v1.8.0" + +[[projects]] + digest = "1:5d231480e1c64a726869bc4142d270184c419749d34f167646baa21008eb0a79" + name = "github.com/mitchellh/go-homedir" + packages = ["."] + pruneopts = "UT" + revision = "af06845cf3004701891bf4fdb884bfe4920b3727" + version = "v1.1.0" + +[[projects]] + digest = "1:53bc4cd4914cd7cd52139990d5170d6dc99067ae31c56530621b18b35fc30318" + name = "github.com/mitchellh/mapstructure" + packages = ["."] + pruneopts = "UT" + revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe" + version = "v1.1.2" + +[[projects]] + digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e" + name = "github.com/pelletier/go-toml" + packages = ["."] + pruneopts = "UT" + revision = "c01d1270ff3e442a8a57cddc1c92dc1138598194" + version = "v1.2.0" + +[[projects]] + digest = "1:b36a0ede02c4c2aef7df7f91cbbb7bb88a98b5d253509d4f997dda526e50c88c" + name = "github.com/russross/blackfriday" + packages = ["."] + pruneopts = "UT" + revision = "05f3235734ad95d0016f6a23902f06461fcf567a" + version = "v1.5.2" + +[[projects]] + digest = "1:3e39bafd6c2f4bf3c76c3bfd16a2e09e016510ad5db90dc02b88e2f565d6d595" + name = "github.com/spf13/afero" + packages = [ + ".", + "mem", + ] + pruneopts = "UT" + revision = "f4711e4db9e9a1d3887343acb72b2bbfc2f686f5" + version = "v1.2.1" + +[[projects]] + digest = "1:08d65904057412fc0270fc4812a1c90c594186819243160dc779a402d4b6d0bc" + name = "github.com/spf13/cast" + packages = ["."] + pruneopts = "UT" + revision = "8c9545af88b134710ab1cd196795e7f2388358d7" + version = "v1.3.0" + +[[projects]] + digest = "1:68ea4e23713989dc20b1bded5d9da2c5f9be14ff9885beef481848edd18c26cb" + name = "github.com/spf13/jwalterweatherman" + packages = ["."] + pruneopts = "UT" + revision = "4a4406e478ca629068e7768fc33f3f044173c0a6" + version = "v1.0.0" + +[[projects]] + digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2" + name = "github.com/spf13/pflag" + packages = ["."] + pruneopts = "UT" + revision = "298182f68c66c05229eb03ac171abe6e309ee79a" + version = "v1.0.3" + +[[projects]] + digest = "1:de37e343c64582d7026bf8ab6ac5b22a72eac54f3a57020db31524affed9f423" + name = "github.com/spf13/viper" + packages = ["."] + pruneopts = "UT" + revision = "6d33b5a963d922d182c91e8a1c88d81fd150cfd4" + version = "v1.3.1" + +[[projects]] + branch = "master" + digest = "1:d0e9a312c4610a508569ab25e54d34600b1a96d19b1866ece104ffdf1c1b9d2c" + name = "golang.org/x/sys" + packages = ["unix"] + pruneopts = "UT" + revision = "cd391775e71e684db52b63df9affd58269495083" + +[[projects]] + digest = "1:8029e9743749d4be5bc9f7d42ea1659471767860f0cdc34d37c3111bd308a295" + name = "golang.org/x/text" + packages = [ + "internal/gen", + "internal/triegen", + "internal/ucd", + "transform", + "unicode/cldr", + "unicode/norm", + ] + pruneopts = "UT" + revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0" + version = "v0.3.0" + +[[projects]] + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = [ + "github.com/cpuguy83/go-md2man/md2man", + "github.com/inconshreveable/mousetrap", + "github.com/mitchellh/go-homedir", + "github.com/spf13/pflag", + "github.com/spf13/viper", + "gopkg.in/yaml.v2", + ] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 00000000..e273f6f6 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,54 @@ +# Gopkg.toml example +# +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + name = "github.com/cpuguy83/go-md2man" + version = "1.0.8" + +[[constraint]] + name = "github.com/inconshreveable/mousetrap" + version = "1.0.0" + +[[constraint]] + name = "github.com/mitchellh/go-homedir" + version = "1.1.0" + +[[constraint]] + name = "github.com/spf13/pflag" + version = "1.0.3" + +[[constraint]] + name = "github.com/spf13/viper" + version = "1.3.1" + +[[constraint]] + name = "gopkg.in/yaml.v2" + version = "2.2.2" + +[prune] + go-tests = true + unused-packages = true diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 8620465a..7e986069 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -136,8 +136,7 @@ import ( {{ printFlagVars .flags }} func init() { - {{ printFlagCreates .flags false }} - {{.parentName}}.AddCommand({{.cmdName}}Cmd) + {{.parentName}}.AddCommand({{.cmdName}}Cmd){{ printFlagCreates .flags false }} } // {{.cmdName}}Cmd represents the {{.cmdName}} command diff --git a/cobra/cmd/add_test.go b/cobra/cmd/add_test.go index b920e2b9..330a4579 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/cmd/add_test.go @@ -10,13 +10,13 @@ import ( "github.com/spf13/viper" ) -// TestGoldenAddCmd initializes the project "github.com/spf13/testproject" +// TestGoldenAddCmd initializes the project "github.com/OneCloudInc/testproject" // in GOPATH, adds "test" command // and compares the content of all files in cmd directory of testproject // with appropriate golden files. // Use -update to update existing golden files. func TestGoldenAddCmd(t *testing.T) { - projectName := "github.com/spf13/testproject" + projectName := "github.com/OneCloudInc/testproject" project := NewProject(projectName) defer os.RemoveAll(project.AbsPath()) @@ -46,7 +46,7 @@ func TestGoldenAddCmd(t *testing.T) { } // Make path relative to project.CmdPath(). - // E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go" + // E.g. path = "/home/user/go/src/github.com/OneCloudInc/testproject/cmd/root.go" // then it returns just "root.go". relPath, err := filepath.Rel(project.CmdPath(), path) if err != nil { diff --git a/cobra/cmd/flag_helpers.go b/cobra/cmd/flag_helpers.go index 9120be94..6e19b468 100644 --- a/cobra/cmd/flag_helpers.go +++ b/cobra/cmd/flag_helpers.go @@ -32,7 +32,7 @@ func printFlagVars(iFlags interface{}) string { flags := toFlagArray(iFlags) varDefs := "" for _, f := range flags { - varDefs += fmt.Sprintf("var %s %s\n", f.VarName, f.FlagType) + varDefs += fmt.Sprintf("var %s %s\r\n", f.VarName, f.FlagType) } return varDefs } @@ -45,8 +45,8 @@ func printFlagCreates(iFlags interface{}, persistent bool) string { flagsFn = "PersistentFlags" } for _, f := range flags { - createStrs += fmt.Sprintf( `%sCmd.%s().%s(&%s, "%s",%s "%s") - `, + createStrs += fmt.Sprintf( ` +%sCmd.%s().%s(&%s, "%s",%s "%s")`, f.CmdName, flagsFn, f.CreateFn, diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 27baf31e..1f3c3ba2 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -168,8 +168,7 @@ func init() { {{- if .viper}} // Cobra also supports local flags, which will only run // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") - {{ printFlagCreates .flags true }} + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle"){{ printFlagCreates .flags true }} } // rootCmd represents the base command when called without any subcommands diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go index 40eb4038..01963fad 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/cmd/init_test.go @@ -10,12 +10,12 @@ import ( "github.com/spf13/viper" ) -// TestGoldenInitCmd initializes the project "github.com/spf13/testproject" +// TestGoldenInitCmd initializes the project "github.com/OneCloudInc/testproject" // in GOPATH and compares the content of files in initialized project with // appropriate golden files ("testdata/*.golden"). // Use -update to update existing golden files. func TestGoldenInitCmd(t *testing.T) { - projectName := "github.com/spf13/testproject" + projectName := "github.com/OneCloudInc/testproject" project := NewProject(projectName) defer os.RemoveAll(project.AbsPath()) @@ -42,7 +42,7 @@ func TestGoldenInitCmd(t *testing.T) { } // Make path relative to project.AbsPath(). - // E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go" + // E.g. path = "/home/user/go/src/github.com/OneCloudInc/testproject/cmd/root.go" // then it returns just "cmd/root.go". relPath, err := filepath.Rel(project.AbsPath(), path) if err != nil { diff --git a/cobra/cmd/testdata/main.go.golden b/cobra/cmd/testdata/main.go.golden index cdbe38d7..a8aa62cd 100644 --- a/cobra/cmd/testdata/main.go.golden +++ b/cobra/cmd/testdata/main.go.golden @@ -14,7 +14,7 @@ package main -import "github.com/spf13/testproject/cmd" +import "github.com/OneCloudInc/testproject/cmd" func main() { cmd.Execute() diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/cmd/testdata/root.go.golden index 8c101684..2a878fee 100644 --- a/cobra/cmd/testdata/root.go.golden +++ b/cobra/cmd/testdata/root.go.golden @@ -18,12 +18,22 @@ import ( "fmt" "os" - homedir "github.com/mitchellh/go-homedir" "github.com/OneCloudInc/cobra" - "github.com/spf13/viper" ) -var cfgFile string + + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -49,41 +59,3 @@ func Execute() { } } -func init() { - cobra.OnInitialize(initConfig) - - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} - -// initConfig reads in config file and ENV variables if set. -func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - // Find home directory. - home, err := homedir.Dir() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - // Search config in home directory with name ".testproject" (without extension). - viper.AddConfigPath(home) - viper.SetConfigName(".testproject") - } - - viper.AutomaticEnv() // read in environment variables that match - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Println("Using config file:", viper.ConfigFileUsed()) - } -} diff --git a/cobra/cmd/testdata/test.go.golden b/cobra/cmd/testdata/test.go.golden index 3f56c0a4..915f356c 100644 --- a/cobra/cmd/testdata/test.go.golden +++ b/cobra/cmd/testdata/test.go.golden @@ -20,31 +20,18 @@ import ( "github.com/OneCloudInc/cobra" ) -// testCmd represents the test command -var testCmd = &cobra.Command{ - Use: "test", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("test called") - }, -} func init() { rootCmd.AddCommand(testCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // testCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + +// testCmd represents the test command +var testCmd = &cobra.Command{ + Use: "test", + Short: "test", + Long: `Description`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("test called, place the command logic here") + }, }