Skip to content

Commit

Permalink
Fix #56 - Customizable copyright string check
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeacom committed Jan 28, 2022
1 parent 2b44b36 commit 6b3df82
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/**

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down Expand Up @@ -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))
}
27 changes: 27 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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},
}
Expand Down Expand Up @@ -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},
Expand Down
3 changes: 3 additions & 0 deletions testdata/custom_copyright.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
© {{.Year}} {{.Holder}}

Custom License Template
12 changes: 12 additions & 0 deletions testdata/custom_copyright_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* © 2022 Google LLC
*
* Custom License Template
*/

#include <stdio.h>

int main() {
printf("Hello world\n");
return 0;
}

0 comments on commit 6b3df82

Please sign in to comment.