mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
test add
This commit is contained in:
parent
303a3e5160
commit
11aa612384
5 changed files with 41 additions and 85 deletions
|
@ -53,9 +53,9 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
|
||||||
CmdName: commandName,
|
CmdName: commandName,
|
||||||
CmdParent: parentName,
|
CmdParent: parentName,
|
||||||
Project: &Project{
|
Project: &Project{
|
||||||
AbsolutePath: fmt.Sprintf("%s/cmd", wd),
|
AbsolutePath: wd,
|
||||||
Legal: getLicense(),
|
Legal: getLicense(),
|
||||||
Copyright: copyrightLine(),
|
Copyright: copyrightLine(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s created at %s", command.CmdName, command.Project.AbsolutePath)
|
fmt.Printf("%s created at %s", command.CmdName, command.AbsolutePath)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,76 +1,32 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
/*
|
import (
|
||||||
// TestGoldenAddCmd initializes the project "github.com/spf13/testproject"
|
"fmt"
|
||||||
// in GOPATH, adds "test" command
|
"os"
|
||||||
// and compares the content of all files in cmd directory of testproject
|
"testing"
|
||||||
// with appropriate golden files.
|
)
|
||||||
// Use -update to update existing golden files.
|
|
||||||
func TestGoldenAddCmd(t *testing.T) {
|
func TestGoldenAddCmd(t *testing.T) {
|
||||||
projectName := "github.com/spf13/testproject"
|
|
||||||
project := NewProject(projectName)
|
|
||||||
defer os.RemoveAll(project.AbsPath())
|
|
||||||
|
|
||||||
viper.Set("author", "NAME HERE <EMAIL ADDRESS>")
|
wd, _ := os.Getwd()
|
||||||
viper.Set("license", "apache")
|
command := &Command{
|
||||||
viper.Set("year", 2017)
|
CmdName: "test",
|
||||||
defer viper.Set("author", nil)
|
CmdParent: parentName,
|
||||||
defer viper.Set("license", nil)
|
Project: &Project{
|
||||||
defer viper.Set("year", nil)
|
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
|
||||||
|
Legal: getLicense(),
|
||||||
|
Copyright: copyrightLine(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the project first.
|
if err := command.Create(); err != nil {
|
||||||
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 {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if some files lack.
|
generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName)
|
||||||
if err := checkLackFiles(expectedFiles, gotFiles); err != nil {
|
goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName)
|
||||||
|
err := compareFiles(generatedFile, goldenFile)
|
||||||
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,4 +54,3 @@ func TestValidateCmdName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ func (p *Project) createLicenseFile() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) Create() 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
27
cobra/cmd/testdata/test.go.golden
vendored
27
cobra/cmd/testdata/test.go.golden
vendored
|
@ -1,17 +1,18 @@
|
||||||
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
|
/*
|
||||||
//
|
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
|
||||||
// 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.
|
|
||||||
|
|
||||||
|
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
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -111,7 +111,7 @@ func initConfig() {
|
||||||
func AddCommandTemplate() []byte {
|
func AddCommandTemplate() []byte {
|
||||||
return []byte(`/*
|
return []byte(`/*
|
||||||
{{ .Project.Copyright }}
|
{{ .Project.Copyright }}
|
||||||
{{ if .Project.Legal.Header }}{{ .Project.Legal.Header }}{{ end }}
|
{{ if .Legal.Header }}{{ .Legal.Header }}{{ end }}
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue