From d08e06282fba975d7c71a097938425e66bb02b2b Mon Sep 17 00:00:00 2001 From: abdul Date: Tue, 29 Oct 2024 10:43:55 +0700 Subject: [PATCH 1/2] remove the use of depreceated pacakge ioutil Changes to be committed: modified: golden_test.go - remove ioutil pacakge modified: init_test.go - remove ioutil pacakge modified: licenses.go - formating and change fmt.Errorf to errors.New to comply with gopls linter --- cmd/golden_test.go | 6 +++--- cmd/init_test.go | 8 +------- cmd/licenses.go | 10 ++++++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cmd/golden_test.go b/cmd/golden_test.go index 832ea53..d406857 100644 --- a/cmd/golden_test.go +++ b/cmd/golden_test.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "os" "os/exec" ) @@ -26,11 +26,11 @@ func ensureLF(content []byte) []byte { // 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) + contentA, err := os.ReadFile(pathA) if err != nil { return err } - contentB, err := ioutil.ReadFile(pathB) + contentB, err := os.ReadFile(pathB) if err != nil { return err } 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] } From 932c45e5d5c28b98f47725d6f9adb21722711daa Mon Sep 17 00:00:00 2001 From: abdul Date: Tue, 29 Oct 2024 11:55:21 +0700 Subject: [PATCH 2/2] fix compareFiles, fix test Changes to be committed: modified: golden_test.go - fix compareFiles function, replace year in golden test file to current year --- cmd/golden_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/golden_test.go b/cmd/golden_test.go index d406857..5991e56 100644 --- a/cmd/golden_test.go +++ b/cmd/golden_test.go @@ -6,6 +6,7 @@ import ( "fmt" "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 := os.ReadFile(pathA) +func compareFiles(generated, golden string) error { + contentA, err := os.ReadFile(generated) if err != nil { return err } - contentB, err := os.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()) }