From 11aa612384e0e18662f319528bde7b68ffca43ee Mon Sep 17 00:00:00 2001 From: jharshman Date: Wed, 30 Jan 2019 19:47:38 -0800 Subject: [PATCH] test add --- cobra/cmd/add.go | 8 +-- cobra/cmd/add_test.go | 87 ++++++++----------------------- cobra/cmd/project.go | 2 +- cobra/cmd/testdata/test.go.golden | 27 +++++----- cobra/tpl/main.go | 2 +- 5 files changed, 41 insertions(+), 85 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 5b0d9e11..2d240b8a 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 94497084..d9ae0f6d 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 852623f6..167c55ff 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 ed644275..fb8e0fa9 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 71f1f450..5e5a0fae 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