diff --git a/cmd/golden_test.go b/cmd/golden_test.go index 832ea53..5991e56 100644 --- a/cmd/golden_test.go +++ b/cmd/golden_test.go @@ -4,8 +4,9 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "os" "os/exec" + "time" ) func init() { @@ -25,29 +26,33 @@ func ensureLF(content []byte) []byte { // If contents are equal, it returns nil. // If not, it returns which files are not equal // and diff (if system has diff command) between these files. -func compareFiles(pathA, pathB string) error { - contentA, err := ioutil.ReadFile(pathA) +func compareFiles(generated, golden string) error { + contentA, err := os.ReadFile(generated) if err != nil { return err } - contentB, err := ioutil.ReadFile(pathB) + contentB, err := os.ReadFile(golden) if err != nil { return err } + + // change year to current year + contentB = bytes.ReplaceAll(contentB, []byte("2022"), []byte(time.Now().Format("2006"))) + if !bytes.Equal(ensureLF(contentA), ensureLF(contentB)) { output := new(bytes.Buffer) - output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", pathA, pathB)) + output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", generated, golden)) diffPath, err := exec.LookPath("diff") if err != nil { // Don't execute diff if it can't be found. return nil } - diffCmd := exec.Command(diffPath, "-u", "--strip-trailing-cr", pathA, pathB) + diffCmd := exec.Command(diffPath, "-u", "--strip-trailing-cr", generated, golden) diffCmd.Stdout = output diffCmd.Stderr = output - output.WriteString("$ diff -u " + pathA + " " + pathB + "\n") + output.WriteString("$ diff -u " + generated + " " + golden + "\n") if err := diffCmd.Run(); err != nil { output.WriteString("\n" + err.Error()) } diff --git a/cmd/init_test.go b/cmd/init_test.go index 0461552..9478567 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -23,11 +22,7 @@ func getProject() *Project { } func TestGoldenInitCmd(t *testing.T) { - - dir, err := ioutil.TempDir("", "cobra-init") - if err != nil { - t.Fatal(err) - } + dir := filepath.Join(os.TempDir(), "cobra-init") defer os.RemoveAll(dir) tests := []struct { @@ -46,7 +41,6 @@ func TestGoldenInitCmd(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - viper.Set("useViper", true) viper.Set("license", "apache") projectPath, err := initializeProject(tt.args) diff --git a/cmd/licenses.go b/cmd/licenses.go index 30c7b24..eee5726 100644 --- a/cmd/licenses.go +++ b/cmd/licenses.go @@ -16,7 +16,7 @@ package cmd import ( - "fmt" + "errors" "strings" "time" @@ -64,8 +64,10 @@ func getLicense() License { // If user wants to have custom license, use that. if viper.IsSet("license.header") || viper.IsSet("license.text") { - return License{Header: viper.GetString("license.header"), - Text: viper.GetString("license.text")} + return License{ + Header: viper.GetString("license.header"), + Text: viper.GetString("license.text"), + } } // If user wants to have built-in license, use that. @@ -94,7 +96,7 @@ func copyrightLine() string { func findLicense(name string) License { found := matchLicense(name) if found == "" { - cobra.CheckErr(fmt.Errorf("unknown license: " + name)) + cobra.CheckErr(errors.New("unknown license: " + name)) } return Licenses[found] }