diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 5b0d9e1..2d240b8 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -53,9 +53,9 @@ Example: cobra add server -> resulting in a new cmd/server.go`, CmdName: commandName, CmdParent: parentName, Project: &Project{ - AbsolutePath: fmt.Sprintf("%s/cmd", wd), - Legal: getLicense(), - Copyright: copyrightLine(), + AbsolutePath: wd, + Legal: getLicense(), + Copyright: copyrightLine(), }, } @@ -64,7 +64,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`, er(err) } - fmt.Printf("%s created at %s", command.CmdName, command.Project.AbsolutePath) + fmt.Printf("%s created at %s", command.CmdName, command.AbsolutePath) }, } ) diff --git a/cobra/cmd/add_test.go b/cobra/cmd/add_test.go index 9449708..d9ae0f6 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/cmd/add_test.go @@ -1,76 +1,32 @@ package cmd -/* -// TestGoldenAddCmd initializes the project "github.com/spf13/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. +import ( + "fmt" + "os" + "testing" +) + func TestGoldenAddCmd(t *testing.T) { - projectName := "github.com/spf13/testproject" - project := NewProject(projectName) - defer os.RemoveAll(project.AbsPath()) - viper.Set("author", "NAME HERE ") - viper.Set("license", "apache") - viper.Set("year", 2017) - defer viper.Set("author", nil) - defer viper.Set("license", nil) - defer viper.Set("year", nil) + wd, _ := os.Getwd() + command := &Command{ + CmdName: "test", + CmdParent: parentName, + Project: &Project{ + AbsolutePath: fmt.Sprintf("%s/testproject", wd), + Legal: getLicense(), + Copyright: copyrightLine(), + }, + } - // Initialize the project first. - initializeProject(project) - - // Then add the "test" command. - cmdName := "test" - cmdPath := filepath.Join(project.CmdPath(), cmdName+".go") - createCmdFile(project.License(), cmdPath, cmdName) - - expectedFiles := []string{".", "root.go", "test.go"} - gotFiles := []string{} - - // Check project file hierarchy and compare the content of every single file - // with appropriate golden file. - err := filepath.Walk(project.CmdPath(), func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - // Make path relative to project.CmdPath(). - // E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go" - // then it returns just "root.go". - relPath, err := filepath.Rel(project.CmdPath(), path) - if err != nil { - return err - } - relPath = filepath.ToSlash(relPath) - gotFiles = append(gotFiles, relPath) - goldenPath := filepath.Join("testdata", filepath.Base(path)+".golden") - - switch relPath { - // Known directories. - case ".": - return nil - // Known files. - case "root.go", "test.go": - if *update { - got, err := ioutil.ReadFile(path) - if err != nil { - return err - } - ioutil.WriteFile(goldenPath, got, 0644) - } - return compareFiles(path, goldenPath) - } - // Unknown file. - return errors.New("unknown file: " + path) - }) - if err != nil { + if err := command.Create(); err != nil { t.Fatal(err) } - // Check if some files lack. - if err := checkLackFiles(expectedFiles, gotFiles); err != nil { + generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName) + goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName) + err := compareFiles(generatedFile, goldenFile) + if err != nil { t.Fatal(err) } } @@ -98,4 +54,3 @@ func TestValidateCmdName(t *testing.T) { } } } -*/ diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 852623f..167c55f 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -87,7 +87,7 @@ func (p *Project) createLicenseFile() error { } func (c *Command) Create() error { - cmdFile, err := os.Create(fmt.Sprintf("%s/%s.go", c.Project.AbsolutePath, c.CmdName)) + cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName)) if err != nil { return err } diff --git a/cobra/cmd/testdata/test.go.golden b/cobra/cmd/testdata/test.go.golden index ed64427..fb8e0fa 100644 --- a/cobra/cmd/testdata/test.go.golden +++ b/cobra/cmd/testdata/test.go.golden @@ -1,17 +1,18 @@ -// Copyright © 2017 NAME HERE -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright © 2019 NAME HERE +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package cmd import ( diff --git a/cobra/tpl/main.go b/cobra/tpl/main.go index 71f1f45..5e5a0fa 100644 --- a/cobra/tpl/main.go +++ b/cobra/tpl/main.go @@ -111,7 +111,7 @@ func initConfig() { func AddCommandTemplate() []byte { return []byte(`/* {{ .Project.Copyright }} -{{ if .Project.Legal.Header }}{{ .Project.Legal.Header }}{{ end }} +{{ if .Legal.Header }}{{ .Legal.Header }}{{ end }} */ package cmd