Skip to content

Commit 6b3df82

Browse files
committed
Fix #56 - Customizable copyright string check
1 parent 2b44b36 commit 6b3df82

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ addlicense requires go 1.16 or later.
2020
-f custom license file (no default)
2121
-l license type: apache, bsd, mit, mpl (defaults to "apache")
2222
-y year (defaults to current year)
23+
-x custom license check, can be used with custom license to properly check presence of header (defaults to "(c)")
2324
-check check only mode: verify presence of license headers and exit with non-zero code if missing
2425
-ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/**
2526

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var (
5757
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl")
5858
licensef = flag.String("f", "", "license file")
5959
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)")
60+
copyright = flag.String("x", "(c)", "custom copyright string used to determine license header presence")
6061
verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified")
6162
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing")
6263
)
@@ -371,5 +372,6 @@ func hasLicense(b []byte) bool {
371372
}
372373
return bytes.Contains(bytes.ToLower(b[:n]), []byte("copyright")) ||
373374
bytes.Contains(bytes.ToLower(b[:n]), []byte("mozilla public")) ||
374-
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier"))
375+
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier")) ||
376+
bytes.Contains(bytes.ToLower(b[:n]), []byte(*copyright))
375377
}

main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ func TestMultiyear(t *testing.T) {
8989
run(t, "diff", samplefile, sampleLicensed)
9090
}
9191

92+
func TestCustomCopyright(t *testing.T) {
93+
if os.Getenv("RUNME") != "" {
94+
main()
95+
return
96+
}
97+
98+
tmp := tempDir(t)
99+
t.Logf("tmp dir: %s", tmp)
100+
samplefile := filepath.Join(tmp, "file.c")
101+
const sampleLicensed = "testdata/custom_copyright_file.c"
102+
const customLicense = "testdata/custom_copyright.tpl"
103+
104+
run(t, "cp", "testdata/initial/file.c", samplefile)
105+
cmd := exec.Command(os.Args[0],
106+
"-test.run=TestCustomCopyright",
107+
"-f", customLicense, "-c", "Google LLC", "-y", "2022",
108+
"-x", "©", samplefile,
109+
)
110+
cmd.Env = []string{"RUNME=1"}
111+
if out, err := cmd.CombinedOutput(); err != nil {
112+
t.Fatalf("%v\n%s", err, out)
113+
}
114+
run(t, "diff", samplefile, sampleLicensed)
115+
}
116+
92117
func TestWriteErrors(t *testing.T) {
93118
if os.Getenv("RUNME") != "" {
94119
main()
@@ -248,6 +273,7 @@ func TestAddLicense(t *testing.T) {
248273
// ensure files with existing license or generated files are
249274
// skipped. No need to test all permutations of these, since
250275
// there are specific tests below.
276+
{"// (c) 2022 Acme\ncontent", "// (c) 2022 Acme\ncontent", false},
251277
{"// Copyright 2000 Acme\ncontent", "// Copyright 2000 Acme\ncontent", false},
252278
{"// Code generated by go generate; DO NOT EDIT.\ncontent", "// Code generated by go generate; DO NOT EDIT.\ncontent", false},
253279
}
@@ -395,6 +421,7 @@ func TestHasLicense(t *testing.T) {
395421

396422
{"Copyright 2000", true},
397423
{"CoPyRiGhT 2000", true},
424+
{"(c) 2022", true},
398425
{"Subject to the terms of the Mozilla Public License", true},
399426
{"SPDX-License-Identifier: MIT", true},
400427
{"spdx-license-identifier: MIT", true},

testdata/custom_copyright.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
© {{.Year}} {{.Holder}}
2+
3+
Custom License Template

testdata/custom_copyright_file.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* © 2022 Google LLC
3+
*
4+
* Custom License Template
5+
*/
6+
7+
#include <stdio.h>
8+
9+
int main() {
10+
printf("Hello world\n");
11+
return 0;
12+
}

0 commit comments

Comments
 (0)