Fix cobra init cmd help documentation (#1108)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Adds tests for other cases where we do not expect projects to be created for absolute paths and relative paths in GOPATH.
authored by Warren Fernandes · committed by GitHub · parent 44d55fb · Browse files · 2 files·+99 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+99 added−41 removed
- other:+99 added−41 removed
Provenance
- Commit
41fd44e- Tree
07731ab- Author
Warren Fernandes <warren.f.fernandes@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
44d55fb
2 files changed+99 added−41 removed
modified:cobra/cmd/init_test.go+66 added-11 removed
1 package cmd2 3 import (4 "fmt"5+ "io/ioutil"6 "os"7 "path/filepath"8 "testing"9+10+ "github.com/spf13/viper"11 )12 13 func getProject() *Project {14 wd, _ := os.Getwd()7 unmodified lines22 }23 }24 25 func TestGoldenInitCmd(t *testing.T) {23- project := getProject()24- defer os.RemoveAll(project.AbsolutePath)26 26- if err := project.Create(); err != nil {27+ dir, err := ioutil.TempDir("", "cobra-init")28+ if err != nil {29 t.Fatal(err)30 }31+ defer os.RemoveAll(dir)32 30- expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}31- for _, f := range expectedFiles {32- generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)33- goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))34- err := compareFiles(generatedFile, goldenFile)35- if err != nil {36- t.Fatal(err)37- }33+ tests := []struct {34+ name string35+ args []string36+ pkgName string37+ expectErr bool38+ }{39+ {40+ name: "successfully creates a project with name",41+ args: []string{"testproject"},42+ pkgName: "github.com/spf13/testproject",43+ expectErr: false,44+ },45+ {46+ name: "returns error when passing an absolute path for project",47+ args: []string{dir},48+ pkgName: "github.com/spf13/testproject",49+ expectErr: true,50+ },51+ {52+ name: "returns error when passing an relative path for project",53+ args: []string{"github.com/spf13/testproject"},54+ pkgName: "github.com/spf13/testproject",55+ expectErr: true,56+ },57 }58+59+ for _, tt := range tests {60+ t.Run(tt.name, func(t *testing.T) {61+62+ initCmd.Flags().Set("pkg-name", tt.pkgName)63+ viper.Set("useViper", true)64+ projectPath, err := initializeProject(tt.args)65+ defer func() {66+ if projectPath != "" {67+ os.RemoveAll(projectPath)68+ }69+ }()70+71+ if !tt.expectErr && err != nil {72+ t.Fatalf("did not expect an error, got %s", err)73+ }74+ if tt.expectErr {75+ if err == nil {76+ t.Fatal("expected an error but got none")77+ } else {78+ // got an expected error nothing more to do79+ return80+ }81+ }82+83+ expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}84+ for _, f := range expectedFiles {85+ generatedFile := fmt.Sprintf("%s/%s", projectPath, f)86+ goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))87+ err := compareFiles(generatedFile, goldenFile)88+ if err != nil {89+ t.Fatal(err)90+ }91+ }92+ })93+ }94 }modified:cobra/cmd/init.go+33 added-30 removed
30 unmodified lines31 Short: "Initialize a Cobra Application",32 Long: `Initialize (cobra init) will create a new application, with a license33 and the appropriate structure for a Cobra-based CLI application.34 35- * If a name is provided, it will be created in the current directory;35+ * If a name is provided, a directory with that name will be created in the current directory;36 * If no name is provided, the current directory will be assumed;37- * If a relative path is provided, it will be created inside $GOPATH38- (e.g. github.com/spf13/hugo);39- * If an absolute path is provided, it will be created;40- * If the directory already exists but is empty, it will be used.37+`,38 42-Init will not use an existing directory with contents.`,39+ Run: func(_ *cobra.Command, args []string) {40 44- Run: func(cmd *cobra.Command, args []string) {45-46- wd, err := os.Getwd()41+ projectPath, err := initializeProject(args)42 if err != nil {43 er(err)44 }50-51- if len(args) > 0 {52- if args[0] != "." {53- wd = fmt.Sprintf("%s/%s", wd, args[0])54- }55- }56-57- project := &Project{58- AbsolutePath: wd,59- PkgName: pkgName,60- Legal: getLicense(),61- Copyright: copyrightLine(),62- Viper: viper.GetBool("useViper"),63- AppName: path.Base(pkgName),64- }65-66- if err := project.Create(); err != nil {67- er(err)68- }69-70- fmt.Printf("Your Cobra application is ready at\n%s\n", project.AbsolutePath)45+ fmt.Printf("Your Cobra application is ready at\n%s\n", projectPath)46 },47 }48 )49 50 func init() {51 initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")52 initCmd.MarkFlagRequired("pkg-name")53 }54+55+func initializeProject(args []string) (string, error) {56+ wd, err := os.Getwd()57+ if err != nil {58+ return "", err59+ }60+61+ if len(args) > 0 {62+ if args[0] != "." {63+ wd = fmt.Sprintf("%s/%s", wd, args[0])64+ }65+ }66+67+ project := &Project{68+ AbsolutePath: wd,69+ PkgName: pkgName,70+ Legal: getLicense(),71+ Copyright: copyrightLine(),72+ Viper: viper.GetBool("useViper"),73+ AppName: path.Base(pkgName),74+ }75+76+ if err := project.Create(); err != nil {77+ return "", err78+ }79+80+ return project.AbsolutePath, nil81+}