Skip to content
Myr

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

5 files+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/*2Copyright © 2019 NAME HERE <EMAIL ADDRESS>34Licensed under the Apache License, Version 2.0 (the "License");5you may not use this file except in compliance with the License.6You may obtain a copy of the License at78 http://www.apache.org/licenses/LICENSE-2.0910Unless required by applicable law or agreed to in writing, software11distributed under the License is distributed on an "AS IS" BASIS,12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13See the License for the specific language governing permissions and14limitations under the License.15*/16package cmd1718import (19 "fmt"
32 unmodified lines
modified:cobra/cmd/add_test.go+21 added-66 removed
1package cmd23/*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.3import (4 "fmt"5 "os"6 "testing"7)89func TestGoldenAddCmd(t *testing.T) {10 projectName := "github.com/spf13/testproject"11 project := NewProject(projectName)12 defer os.RemoveAll(project.AbsPath())1014 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 }2121 // Initialize the project first.22 initializeProject(project)2324 // Then add the "test" command.25 cmdName := "test"26 cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")27 createCmdFile(project.License(), cmdPath, cmdName)2829 expectedFiles := []string{".", "root.go", "test.go"}30 gotFiles := []string{}3132 // 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 }3839 // 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")4950 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 }2572 // 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 lines
53 t.Errorf("Expected %q, got %q", testCase.expected, got)54 }55 }56}101*/
modified:cobra/cmd/add.go+4 added-4 removed
51 unmodified lines
52 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 }6162 err = command.Create()63 if err != nil {64 er(err)65 }6667 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 lines
modified:cobra/cmd/project.go+1 added-1 removed
85 unmodified lines
86 return licenseTemplate.Execute(licenseFile, data)87}8889func (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 lines
modified:cobra/tpl/main.go+1 added-1 removed
109 unmodified lines
110111func 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*/116package cmd117118import (
35 unmodified lines