mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Fix cobra init cmd help documentation (#1108)
Adds tests for other cases where we do not expect projects to be created for absolute paths and relative paths in GOPATH.
This commit is contained in:
parent
44d55fb4d3
commit
41fd44e1df
2 changed files with 99 additions and 41 deletions
|
@ -32,21 +32,31 @@ var (
|
||||||
Long: `Initialize (cobra init) will create a new application, with a license
|
Long: `Initialize (cobra init) will create a new application, with a license
|
||||||
and the appropriate structure for a Cobra-based CLI application.
|
and the appropriate structure for a Cobra-based CLI application.
|
||||||
|
|
||||||
* If a name is provided, it will be created in the current directory;
|
* If a name is provided, a directory with that name will be created in the current directory;
|
||||||
* If no name is provided, the current directory will be assumed;
|
* If no name is provided, the current directory will be assumed;
|
||||||
* If a relative path is provided, it will be created inside $GOPATH
|
`,
|
||||||
(e.g. github.com/spf13/hugo);
|
|
||||||
* If an absolute path is provided, it will be created;
|
|
||||||
* If the directory already exists but is empty, it will be used.
|
|
||||||
|
|
||||||
Init will not use an existing directory with contents.`,
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
projectPath, err := initializeProject(args)
|
||||||
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Your Cobra application is ready at\n%s\n", projectPath)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
|
||||||
|
initCmd.MarkFlagRequired("pkg-name")
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializeProject(args []string) (string, error) {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
if args[0] != "." {
|
if args[0] != "." {
|
||||||
|
@ -64,15 +74,8 @@ Init will not use an existing directory with contents.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := project.Create(); err != nil {
|
if err := project.Create(); err != nil {
|
||||||
er(err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Your Cobra application is ready at\n%s\n", project.AbsolutePath)
|
return project.AbsolutePath, nil
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
|
|
||||||
initCmd.MarkFlagRequired("pkg-name")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,12 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getProject() *Project {
|
func getProject() *Project {
|
||||||
|
@ -20,20 +23,72 @@ func getProject() *Project {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGoldenInitCmd(t *testing.T) {
|
func TestGoldenInitCmd(t *testing.T) {
|
||||||
project := getProject()
|
|
||||||
defer os.RemoveAll(project.AbsolutePath)
|
|
||||||
|
|
||||||
if err := project.Create(); err != nil {
|
dir, err := ioutil.TempDir("", "cobra-init")
|
||||||
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
pkgName string
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "successfully creates a project with name",
|
||||||
|
args: []string{"testproject"},
|
||||||
|
pkgName: "github.com/spf13/testproject",
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "returns error when passing an absolute path for project",
|
||||||
|
args: []string{dir},
|
||||||
|
pkgName: "github.com/spf13/testproject",
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "returns error when passing an relative path for project",
|
||||||
|
args: []string{"github.com/spf13/testproject"},
|
||||||
|
pkgName: "github.com/spf13/testproject",
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
initCmd.Flags().Set("pkg-name", tt.pkgName)
|
||||||
|
viper.Set("useViper", true)
|
||||||
|
projectPath, err := initializeProject(tt.args)
|
||||||
|
defer func() {
|
||||||
|
if projectPath != "" {
|
||||||
|
os.RemoveAll(projectPath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if !tt.expectErr && err != nil {
|
||||||
|
t.Fatalf("did not expect an error, got %s", err)
|
||||||
|
}
|
||||||
|
if tt.expectErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected an error but got none")
|
||||||
|
} else {
|
||||||
|
// got an expected error nothing more to do
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
|
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
|
||||||
for _, f := range expectedFiles {
|
for _, f := range expectedFiles {
|
||||||
generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
|
generatedFile := fmt.Sprintf("%s/%s", projectPath, f)
|
||||||
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
|
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
|
||||||
err := compareFiles(generatedFile, goldenFile)
|
err := compareFiles(generatedFile, goldenFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue