44package config
55
66import (
7+ "fmt"
78 "path/filepath"
9+ "strconv"
810 "strings"
911 "testing"
12+ "time"
1013
1114 "github.com/knadh/koanf"
1215 "github.com/spf13/pflag"
@@ -26,8 +29,8 @@ func Test_New(t *testing.T) {
2629
2730 // Validate the default value(s)
2831 assert .Equal (t , 1 , actualOutput .SchemaVersion , "Schema Version defaults to 1" )
29- assert .Equal (t , "HashiCorp, Inc ." , actualOutput .Project .CopyrightHolder , "Copyright Holder defaults to 'HashiCorp, Inc .'" )
30- assert .Equal (t , "project.copyright_holder -> HashiCorp, Inc .\n schema_version -> 1\n " , actualOutput .Sprint (), "Koanf object gets updated appropriately with defaults" )
32+ assert .Equal (t , "IBM Corp ." , actualOutput .Project .CopyrightHolder , "Copyright Holder defaults to 'IBM Corp .'" )
33+ assert .Equal (t , "project.copyright_holder -> IBM Corp .\n schema_version -> 1\n " , actualOutput .Sprint (), "Koanf object gets updated appropriately with defaults" )
3134 })
3235}
3336
@@ -48,7 +51,7 @@ func Test_LoadConfMap(t *testing.T) {
4851 globalKoanf : koanf .New (delim ),
4952 SchemaVersion : 12 ,
5053 Project : Project {
51- CopyrightHolder : "HashiCorp, Inc ." ,
54+ CopyrightHolder : "IBM Corp ." ,
5255 CopyrightYear : 9001 ,
5356 License : "MPL-2.0" ,
5457 },
@@ -73,10 +76,11 @@ func Test_LoadConfMap(t *testing.T) {
7376func Test_LoadCommandFlags (t * testing.T ) {
7477 // Map command flags to config keys
7578 mapping := map [string ]string {
76- `schemaVersion` : `schema_version` ,
77- `spdx` : `project.license` ,
78- `year` : `project.copyright_year` ,
79- `ignoredRepos` : `dispatch.ignored_repos` ,
79+ `schemaVersion` : `schema_version` ,
80+ `spdx` : `project.license` ,
81+ `year` : `project.copyright_year` ,
82+ `copyrightHolder` : `project.copyright_holder` ,
83+ `ignoredRepos` : `dispatch.ignored_repos` ,
8084 }
8185
8286 tests := []struct {
@@ -92,7 +96,7 @@ func Test_LoadCommandFlags(t *testing.T) {
9296 expectedOutput : & Config {
9397 SchemaVersion : 1 ,
9498 Project : Project {
95- CopyrightHolder : "HashiCorp, Inc ." ,
99+ CopyrightHolder : "IBM Corp ." ,
96100 CopyrightYear : 9001 ,
97101 License : "MPL-2.0" ,
98102 },
@@ -108,7 +112,7 @@ func Test_LoadCommandFlags(t *testing.T) {
108112 expectedOutput : & Config {
109113 SchemaVersion : 12 ,
110114 Project : Project {
111- CopyrightHolder : "HashiCorp, Inc ." ,
115+ CopyrightHolder : "IBM Corp ." ,
112116 CopyrightYear : 9001 ,
113117 License : "MPL-2.0" ,
114118 },
@@ -124,7 +128,7 @@ func Test_LoadCommandFlags(t *testing.T) {
124128 expectedOutput : & Config {
125129 SchemaVersion : 33 ,
126130 Project : Project {
127- CopyrightHolder : "HashiCorp, Inc ." ,
131+ CopyrightHolder : "IBM Corp ." ,
128132 CopyrightYear : 9001 ,
129133 License : "MPL-2.0" ,
130134 },
@@ -140,7 +144,7 @@ func Test_LoadCommandFlags(t *testing.T) {
140144 expectedOutput : & Config {
141145 SchemaVersion : 33 ,
142146 Project : Project {
143- CopyrightHolder : "HashiCorp, Inc ." ,
147+ CopyrightHolder : "IBM Corp ." ,
144148 CopyrightYear : 9001 ,
145149 License : "MPL-2.0" ,
146150 },
@@ -157,6 +161,7 @@ func Test_LoadCommandFlags(t *testing.T) {
157161 flags .Int ("schemaVersion" , 12 , "Config Schema Version" )
158162 flags .String ("spdx" , "MPL-2.0" , "SPDX License Identifier" )
159163 flags .Int ("year" , 9001 , "Year of copyright" )
164+ flags .String ("copyrightHolder" , "IBM Corp." , "Copyright Holder" )
160165 flags .StringArray ("ignoredRepos" , []string {"foo" , "bar" }, "repos to ignore" )
161166 err := flags .Parse (tt .args )
162167 assert .Nil (t , err , "If this broke, the test is wrong, not the function under test" )
@@ -367,3 +372,45 @@ func Test_GetConfigPath(t *testing.T) {
367372 abs , _ := filepath .Abs (cfgPath )
368373 assert .Equal (t , abs , actualOutput .GetConfigPath (), "Loaded config should return abs file path" )
369374}
375+
376+ func Test_FormatCopyrightYears (t * testing.T ) {
377+ currentYear := time .Now ().Year ()
378+
379+ tests := []struct {
380+ description string
381+ copyrightYear int
382+ expectedOutput string
383+ }{
384+ {
385+ description : "No copyright year set (0) should return current year only" ,
386+ copyrightYear : 0 ,
387+ expectedOutput : strconv .Itoa (currentYear ),
388+ },
389+ {
390+ description : "Copyright year equals current year should return single year" ,
391+ copyrightYear : currentYear ,
392+ expectedOutput : strconv .Itoa (currentYear ),
393+ },
394+ {
395+ description : "Copyright year before current year should return year range" ,
396+ copyrightYear : 2023 ,
397+ expectedOutput : fmt .Sprintf ("2023, %d" , currentYear ),
398+ },
399+ {
400+ description : "Old copyright year should return year range" ,
401+ copyrightYear : 2018 ,
402+ expectedOutput : fmt .Sprintf ("2018, %d" , currentYear ),
403+ },
404+ }
405+
406+ for _ , tt := range tests {
407+ t .Run (tt .description , func (t * testing.T ) {
408+ c := MustNew ()
409+ c .Project .CopyrightYear = tt .copyrightYear
410+
411+ actualOutput := c .FormatCopyrightYears ()
412+
413+ assert .Equal (t , tt .expectedOutput , actualOutput , tt .description )
414+ })
415+ }
416+ }
0 commit comments