From 750ba8ac9363e9b5e841c47fbc1d70caa6f52702 Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Sun, 30 Jul 2017 00:05:07 -0600 Subject: [PATCH] Fix reproducibility in TestGolden{Add,Init}Cmd These tests were time-dependent and would start failing in 2018 when the golden files still have the copyright year of 2017. Fix by hard-coding the year 2017 for the purpose of these two tests to ensure reproducible builds. Fixes #503 --- cobra/cmd/add_test.go | 5 +++++ cobra/cmd/init_test.go | 5 +++++ cobra/cmd/licenses.go | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cobra/cmd/add_test.go b/cobra/cmd/add_test.go index 63c1866..1aa5f53 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/cmd/add_test.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/spf13/viper" ) // TestGoldenAddCmd initializes the project "github.com/spf13/testproject" @@ -20,6 +22,9 @@ func TestGoldenAddCmd(t *testing.T) { // Initialize the project at first. initializeProject(project) defer os.RemoveAll(project.AbsPath()) + defer viper.Set("year", nil) + + viper.Set("year", 2017) // For reproducible builds // Then add the "test" command. cmdName := "test" diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go index d4ea6ed..1ba9a54 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/cmd/init_test.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/spf13/viper" ) // TestGoldenInitCmd initializes the project "github.com/spf13/testproject" @@ -16,6 +18,9 @@ func TestGoldenInitCmd(t *testing.T) { projectName := "github.com/spf13/testproject" project := NewProject(projectName) defer os.RemoveAll(project.AbsPath()) + defer viper.Set("year", nil) + + viper.Set("year", 2017) // For reproducible builds os.Args = []string{"cobra", "init", projectName} if err := rootCmd.Execute(); err != nil { diff --git a/cobra/cmd/licenses.go b/cobra/cmd/licenses.go index d73e6fb..61444f6 100644 --- a/cobra/cmd/licenses.go +++ b/cobra/cmd/licenses.go @@ -77,7 +77,11 @@ func getLicense() License { func copyrightLine() string { author := viper.GetString("author") - year := time.Now().Format("2006") + + year := viper.GetString("year") // For reproducible builds + if year == "" { + year = time.Now().Format("2006") + } return "Copyright © " + year + " " + author }