Skip to content

Commit

Permalink
test add
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman authored and spf13 committed Jun 7, 2019
1 parent 303a3e5 commit 11aa612
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 85 deletions.
8 changes: 4 additions & 4 deletions cobra/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}

Expand All @@ -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)
},
}
)
Expand Down
87 changes: 21 additions & 66 deletions cobra/cmd/add_test.go
Original file line number Diff line number Diff line change
@@ -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.
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>")
viper.Set("license", "apache")
viper.Set("year", 2017)
defer viper.Set("author", nil)
defer viper.Set("license", nil)
defer viper.Set("year", nil)
// Initialize the project first.
initializeProject(project)
import (
"fmt"
"os"
"testing"
)

// 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
}
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"
// 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")
wd, _ := os.Getwd()
command := &Command{
CmdName: "test",
CmdParent: parentName,
Project: &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
Legal: getLicense(),
Copyright: copyrightLine(),
},
}

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)
}
}
Expand Down Expand Up @@ -98,4 +54,3 @@ func TestValidateCmdName(t *testing.T) {
}
}
}
*/
2 changes: 1 addition & 1 deletion cobra/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 14 additions & 13 deletions cobra/cmd/testdata/test.go.golden
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright © 2017 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.
/*
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.
*/
package cmd

import (
Expand Down
2 changes: 1 addition & 1 deletion cobra/tpl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 11aa612

Please sign in to comment.