add support for SPDX license headers

This adds a new "-s" flag that will append an SPDX-License-Identifier
line to license headers.  If "-s=only" is specified, then only the SPDX
identifier will be used.

This also changes the "-l" flag to use SPDX identifiers, with mappings
to support the legacy "apache", "mit", and "mpl" values.  Together with
the "-s" flag, this allows SPDX headers for any arbitrary license type
to be added to files.

Co-authored-by: Bob Callaway <bcallawa@redhat.com>
This commit is contained in:
Will Norris
2021-07-26 20:26:45 -07:00
parent c2fdf83882
commit 97ae522f98
3 changed files with 113 additions and 19 deletions

View File

@@ -31,16 +31,19 @@ func init() {
func TestFetchTemplate(t *testing.T) {
tests := []struct {
description string // test case description
license string // license passed to fetchTemplate
templateFile string // templatefile passed to fetchTemplate
wantTemplate string // expected returned template
wantErr error // expected returned error
description string // test case description
license string // license passed to fetchTemplate
templateFile string // templatefile passed to fetchTemplate
spdx spdxFlag // spdx value passed to fetchTemplate
wantTemplate string // expected returned template
wantErr error // expected returned error
}{
// custom template files
{
"non-existant template file",
"",
"/does/not/exist",
spdxOff,
"",
os.ErrNotExist,
},
@@ -48,27 +51,34 @@ func TestFetchTemplate(t *testing.T) {
"custom template file",
"",
"testdata/custom.tpl",
spdxOff,
"Copyright {{.Year}} {{.Holder}}\n\nCustom License Template\n",
nil,
},
{
"unknown license",
"unknown",
"",
spdxOff,
"",
errors.New(`unknown license: "unknown"`),
errors.New(`unknown license: "unknown". Include the '-s' flag to request SPDX style headers using this license.`),
},
// pre-defined license templates, no SPDX
{
"apache license template",
"apache",
"Apache-2.0",
"",
spdxOff,
tmplApache,
nil,
},
{
"mit license template",
"mit",
"MIT",
"",
spdxOff,
tmplMIT,
nil,
},
@@ -76,21 +86,49 @@ func TestFetchTemplate(t *testing.T) {
"bsd license template",
"bsd",
"",
spdxOff,
tmplBSD,
nil,
},
{
"mpl license template",
"mpl",
"MPL-2.0",
"",
spdxOff,
tmplMPL,
nil,
},
// SPDX variants
{
"apache license template with SPDX added",
"Apache-2.0",
"",
spdxOn,
tmplApache + spdxSuffix,
nil,
},
{
"apache license template with SPDX only",
"Apache-2.0",
"",
spdxOnly,
tmplSPDX,
nil,
},
{
"unknown license with SPDX only",
"unknown",
"",
spdxOnly,
tmplSPDX,
nil,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
tpl, err := fetchTemplate(tt.license, tt.templateFile)
tpl, err := fetchTemplate(tt.license, tt.templateFile, tt.spdx)
if tt.wantErr != nil && (err == nil || (!errors.Is(err, tt.wantErr) && err.Error() != tt.wantErr.Error())) {
t.Fatalf("fetchTemplate(%q, %q) returned error: %#v, want %#v", tt.license, tt.templateFile, err, tt.wantErr)
}