diff --git a/README.md b/README.md index 2a7ae81..f4f1842 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ addlicense requires go 1.16 or later. -f custom license file (no default) -l license type: apache, bsd, mit, mpl (defaults to "apache") -y year (defaults to current year) + -x custom license check, can be used with custom license to properly check presence of header (defaults to "(c)") -check check only mode: verify presence of license headers and exit with non-zero code if missing -ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/** diff --git a/main.go b/main.go index bf53861..227ee02 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ var ( license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl") licensef = flag.String("f", "", "license file") year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)") + copyright = flag.String("x", "(c)", "custom copyright string used to determine license header presence") verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified") checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing") ) @@ -371,5 +372,6 @@ func hasLicense(b []byte) bool { } return bytes.Contains(bytes.ToLower(b[:n]), []byte("copyright")) || bytes.Contains(bytes.ToLower(b[:n]), []byte("mozilla public")) || - bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) + bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) || + bytes.Contains(bytes.ToLower(b[:n]), []byte(*copyright)) } diff --git a/main_test.go b/main_test.go index 0c60953..d1de772 100644 --- a/main_test.go +++ b/main_test.go @@ -89,6 +89,31 @@ func TestMultiyear(t *testing.T) { run(t, "diff", samplefile, sampleLicensed) } +func TestCustomCopyright(t *testing.T) { + if os.Getenv("RUNME") != "" { + main() + return + } + + tmp := tempDir(t) + t.Logf("tmp dir: %s", tmp) + samplefile := filepath.Join(tmp, "file.c") + const sampleLicensed = "testdata/custom_copyright_file.c" + const customLicense = "testdata/custom_copyright.tpl" + + run(t, "cp", "testdata/initial/file.c", samplefile) + cmd := exec.Command(os.Args[0], + "-test.run=TestCustomCopyright", + "-f", customLicense, "-c", "Google LLC", "-y", "2022", + "-x", "©", samplefile, + ) + cmd.Env = []string{"RUNME=1"} + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("%v\n%s", err, out) + } + run(t, "diff", samplefile, sampleLicensed) +} + func TestWriteErrors(t *testing.T) { if os.Getenv("RUNME") != "" { main() @@ -248,6 +273,7 @@ func TestAddLicense(t *testing.T) { // ensure files with existing license or generated files are // skipped. No need to test all permutations of these, since // there are specific tests below. + {"// (c) 2022 Acme\ncontent", "// (c) 2022 Acme\ncontent", false}, {"// Copyright 2000 Acme\ncontent", "// Copyright 2000 Acme\ncontent", false}, {"// Code generated by go generate; DO NOT EDIT.\ncontent", "// Code generated by go generate; DO NOT EDIT.\ncontent", false}, } @@ -395,6 +421,7 @@ func TestHasLicense(t *testing.T) { {"Copyright 2000", true}, {"CoPyRiGhT 2000", true}, + {"(c) 2022", true}, {"Subject to the terms of the Mozilla Public License", true}, {"SPDX-License-Identifier: MIT", true}, {"spdx-license-identifier: MIT", true}, diff --git a/testdata/custom_copyright.tpl b/testdata/custom_copyright.tpl new file mode 100644 index 0000000..963e5fa --- /dev/null +++ b/testdata/custom_copyright.tpl @@ -0,0 +1,3 @@ +© {{.Year}} {{.Holder}} + +Custom License Template diff --git a/testdata/custom_copyright_file.c b/testdata/custom_copyright_file.c new file mode 100644 index 0000000..b66490e --- /dev/null +++ b/testdata/custom_copyright_file.c @@ -0,0 +1,12 @@ +/* + * © 2022 Google LLC + * + * Custom License Template + */ + +#include + +int main() { + printf("Hello world\n"); + return 0; +}