Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional -license.year flag #46

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package license

import (
"fmt"
"go/ast"
"regexp"
"strconv"
"strings"
"time"

"golang.org/x/tools/go/analysis"
)
Expand All @@ -25,11 +28,12 @@ var EEAnalyzer = &analysis.Analyzer{

var (
ignoreFilesPattern string
licenseYear string
sourceAvailablePackageRe = regexp.MustCompile("/enterprise")
)

const (
defaultLicenseLine1 = "// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved."
licenseLine1Format = "// Copyright (c) %d-present Mattermost, Inc. All Rights Reserved."
defaultLicenseLine2 = "// See LICENSE.txt for license information."
enterpriseLicenseLine2 = "// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information."
sourceAvailableLicenseLine2 = "// See LICENSE.enterprise for license information."
Expand All @@ -51,7 +55,9 @@ var generatedHeaders = []string{

func init() {
Analyzer.Flags.StringVar(&ignoreFilesPattern, "ignore", "", "Comma separated list of files to ignore")
Analyzer.Flags.StringVar(&licenseYear, "year", "2015", "Year to use in license header (2015-present)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the year to be parsed as string vs integer?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only ever really used as an integer, so could we parse it as one rather than a string that is later converted? I think this would also mean error checking happens automatically

EEAnalyzer.Flags.StringVar(&ignoreFilesPattern, "ignore", "", "Comma separated list of files to ignore")
EEAnalyzer.Flags.StringVar(&licenseYear, "year", "2015", "Year to use in license header (2015-present)")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}

func run(pass *analysis.Pass) (interface{}, error) {
Expand All @@ -60,7 +66,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
ignoreFiles = strings.Split(ignoreFilesPattern, ",")
}

expectedLine1 := defaultLicenseLine1
// Validate license year
year := 2015
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could make this a package-level constant.

if licenseYear != "" {
if y, err := strconv.Atoi(licenseYear); err != nil {
return nil, fmt.Errorf("invalid license year: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd generally use %w for errors, but not a big deal.

} else {
currentYear := time.Now().Year()
if y < 2015 || y > currentYear {
return nil, fmt.Errorf("license year must be between 2015 and %d", currentYear)
}
year = y
}
}

expectedLine1 := fmt.Sprintf(licenseLine1Format, year)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the above change, this would simplify down to

	// Validate license year
	currentYear := time.Now().Year()
	if licenseYear < 2015 || licenseYear > currentYear {
		return nil, fmt.Errorf("license year must be between 2015 and %d", currentYear)
	}

	expectedLine1 := fmt.Sprintf(licenseLine1Format, licenseYear)

Perhaps a good idea to use a constant for the MinYear of 2015? Not likely to change I guess

expectedLine2 := defaultLicenseLine2

if pass.Analyzer.Name == "enterpriseLicense" {
Expand Down
80 changes: 80 additions & 0 deletions license/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package license_test

import (
"fmt"
"path/filepath"
"testing"

Expand All @@ -14,6 +15,14 @@ import (
"golang.org/x/tools/go/analysis/analysistest"
)

type MockT struct {
calls []string
}

func (mt *MockT) Errorf(format string, args ...interface{}) {
mt.calls = append(mt.calls, fmt.Sprintf(format, args...))
}

func TestLicense(t *testing.T) {
testCases := []struct {
Description string
Expand Down Expand Up @@ -91,6 +100,77 @@ func TestLicense(t *testing.T) {
analysistest.Run(t, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "invalid_reference"))
})

t.Run("parameterized year", func(t *testing.T) {
t.Run("invalid year", func(t *testing.T) {
mt := &MockT{}
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "invalid"))
t.Cleanup(func() {
require.NoError(t, testCase.Analyzer.Flags.Set("year", ""))
})
analysistest.Run(mt, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "valid"))

require.Len(t, mt.calls, 1)
for _, call := range mt.calls {
require.Contains(t, call, "invalid license year:")
}
})

t.Run("before 2015", func(t *testing.T) {
mt := &MockT{}
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "2014"))
t.Cleanup(func() {
require.NoError(t, testCase.Analyzer.Flags.Set("year", ""))
})
analysistest.Run(mt, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "valid"))

require.Len(t, mt.calls, 1)
for _, call := range mt.calls {
require.Contains(t, call, "license year must be between 2015 and")
}
})

t.Run("2015", func(t *testing.T) {
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "2015"))
analysistest.Run(t, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "parameterized_year/2015"))
})

t.Run("2024", func(t *testing.T) {
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "2024"))
t.Cleanup(func() {
require.NoError(t, testCase.Analyzer.Flags.Set("year", ""))
})
analysistest.Run(t, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "parameterized_year/2024"))
})

t.Run("current year", func(t *testing.T) {
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "2025"))
t.Cleanup(func() {
require.NoError(t, testCase.Analyzer.Flags.Set("year", ""))
})
analysistest.Run(t, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "parameterized_year/current"))
})

t.Run("future year", func(t *testing.T) {
mt := &MockT{}
testdata := analysistest.TestData()
require.NoError(t, testCase.Analyzer.Flags.Set("year", "2026"))
t.Cleanup(func() {
require.NoError(t, testCase.Analyzer.Flags.Set("year", ""))
})
analysistest.Run(mt, testdata, testCase.Analyzer, filepath.Join(testCase.Path, "valid"))

require.Len(t, mt.calls, 1)
for _, call := range mt.calls {
require.Contains(t, call, "license year must be between 2015 and")
}
})
})

t.Run("build directives", func(t *testing.T) {
t.Run("go:generate with valid license", func(t *testing.T) {
testdata := analysistest.TestData()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information.

package year2015

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved.
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information.

package year2024

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved.
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information.

package yearcurrent

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.enterprise for license information.

package year2015

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.enterprise for license information.

package year2024

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.enterprise for license information.

package yearcurrent

func Test() {}
6 changes: 6 additions & 0 deletions license/testdata/src/standard/parameterized_year/2015/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package year2015

func Test() {}
6 changes: 6 additions & 0 deletions license/testdata/src/standard/parameterized_year/2024/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package year2024

func Test() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package yearcurrent

func Test() {}
Loading