test add
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by jharshman · committed by Steve Francia · parent 303a3e5 · Browse files · 5 files·+41 added−85 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+41 added−85 removed
- other:+41 added−85 removed
Provenance
- Commit
11aa612- Tree
97ad1bd- Author
jharshman <joshgreyhat@gmail.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent
303a3e5
5 files changed+41 added−85 removed
modified:cobra/cmd/testdata/test.go.golden+14 added-13 removed
1-// Copyright © 2017 NAME HERE <EMAIL ADDRESS>2-//3-// Licensed under the Apache License, Version 2.0 (the "License");4-// you may not use this file except in compliance with the License.5-// You may obtain a copy of the License at6-//7-// http://www.apache.org/licenses/LICENSE-2.08-//9-// Unless required by applicable law or agreed to in writing, software10-// distributed under the License is distributed on an "AS IS" BASIS,11-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12-// See the License for the specific language governing permissions and13-// limitations under the License.1+/*2+Copyright © 2019 NAME HERE <EMAIL ADDRESS>3 4+Licensed under the Apache License, Version 2.0 (the "License");5+you may not use this file except in compliance with the License.6+You may obtain a copy of the License at7+8+ http://www.apache.org/licenses/LICENSE-2.09+10+Unless required by applicable law or agreed to in writing, software11+distributed under the License is distributed on an "AS IS" BASIS,12+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13+See the License for the specific language governing permissions and14+limitations under the License.15+*/16 package cmd17 18 import (19 "fmt"32 unmodified linesmodified:cobra/cmd/add_test.go+21 added-66 removed
1 package cmd2 3-/*4-// TestGoldenAddCmd initializes the project "github.com/spf13/testproject"5-// in GOPATH, adds "test" command6-// and compares the content of all files in cmd directory of testproject7-// with appropriate golden files.8-// Use -update to update existing golden files.3+import (4+ "fmt"5+ "os"6+ "testing"7+)8+9 func TestGoldenAddCmd(t *testing.T) {10- projectName := "github.com/spf13/testproject"11- project := NewProject(projectName)12- defer os.RemoveAll(project.AbsPath())10 14- viper.Set("author", "NAME HERE <EMAIL ADDRESS>")15- viper.Set("license", "apache")16- viper.Set("year", 2017)17- defer viper.Set("author", nil)18- defer viper.Set("license", nil)19- defer viper.Set("year", nil)11+ wd, _ := os.Getwd()12+ command := &Command{13+ CmdName: "test",14+ CmdParent: parentName,15+ Project: &Project{16+ AbsolutePath: fmt.Sprintf("%s/testproject", wd),17+ Legal: getLicense(),18+ Copyright: copyrightLine(),19+ },20+ }21 21- // Initialize the project first.22- initializeProject(project)23-24- // Then add the "test" command.25- cmdName := "test"26- cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")27- createCmdFile(project.License(), cmdPath, cmdName)28-29- expectedFiles := []string{".", "root.go", "test.go"}30- gotFiles := []string{}31-32- // Check project file hierarchy and compare the content of every single file33- // with appropriate golden file.34- err := filepath.Walk(project.CmdPath(), func(path string, info os.FileInfo, err error) error {35- if err != nil {36- return err37- }38-39- // Make path relative to project.CmdPath().40- // E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"41- // then it returns just "root.go".42- relPath, err := filepath.Rel(project.CmdPath(), path)43- if err != nil {44- return err45- }46- relPath = filepath.ToSlash(relPath)47- gotFiles = append(gotFiles, relPath)48- goldenPath := filepath.Join("testdata", filepath.Base(path)+".golden")49-50- switch relPath {51- // Known directories.52- case ".":53- return nil54- // Known files.55- case "root.go", "test.go":56- if *update {57- got, err := ioutil.ReadFile(path)58- if err != nil {59- return err60- }61- ioutil.WriteFile(goldenPath, got, 0644)62- }63- return compareFiles(path, goldenPath)64- }65- // Unknown file.66- return errors.New("unknown file: " + path)67- })68- if err != nil {22+ if err := command.Create(); err != nil {23 t.Fatal(err)24 }25 72- // Check if some files lack.73- if err := checkLackFiles(expectedFiles, gotFiles); err != nil {26+ generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName)27+ goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName)28+ err := compareFiles(generatedFile, goldenFile)29+ if err != nil {30 t.Fatal(err)31 }32 }33 19 unmodified lines53 t.Errorf("Expected %q, got %q", testCase.expected, got)54 }55 }56 }101-*/modified:cobra/cmd/add.go+4 added-4 removed
51 unmodified lines52 command := &Command{53 CmdName: commandName,54 CmdParent: parentName,55 Project: &Project{56- AbsolutePath: fmt.Sprintf("%s/cmd", wd),57- Legal: getLicense(),58- Copyright: copyrightLine(),56+ AbsolutePath: wd,57+ Legal: getLicense(),58+ Copyright: copyrightLine(),59 },60 }61 62 err = command.Create()63 if err != nil {64 er(err)65 }66 67- fmt.Printf("%s created at %s", command.CmdName, command.Project.AbsolutePath)67+ fmt.Printf("%s created at %s", command.CmdName, command.AbsolutePath)68 },69 }70 )71 58 unmodified linesmodified:cobra/cmd/project.go+1 added-1 removed
85 unmodified lines86 return licenseTemplate.Execute(licenseFile, data)87 }88 89 func (c *Command) Create() error {90- cmdFile, err := os.Create(fmt.Sprintf("%s/%s.go", c.Project.AbsolutePath, c.CmdName))90+ cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName))91 if err != nil {92 return err93 }94 defer cmdFile.Close()8 unmodified linesmodified:cobra/tpl/main.go+1 added-1 removed
109 unmodified lines110 111 func AddCommandTemplate() []byte {112 return []byte(`/*113 {{ .Project.Copyright }}114-{{ if .Project.Legal.Header }}{{ .Project.Legal.Header }}{{ end }}114+{{ if .Legal.Header }}{{ .Legal.Header }}{{ end }}115 */116 package cmd117 118 import (35 unmodified lines