Skip to content

Commit 328cfe4

Browse files
committed
all: add a test that checks for missing or mismatching file copyright
In #294, we accidentally checked files with the default Go project copyright header. Avoid this in the future with a copyright test.
1 parent 728e0e3 commit 328cfe4

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

copyright_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gosdk
6+
7+
import (
8+
"fmt"
9+
"go/parser"
10+
"go/token"
11+
"io/fs"
12+
"path/filepath"
13+
"regexp"
14+
"strings"
15+
"testing"
16+
)
17+
18+
func TestCopyrightHeaders(t *testing.T) {
19+
var re = regexp.MustCompile(`Copyright \d{4} The Go MCP SDK Authors. All rights reserved.
20+
Use of this source code is governed by an MIT-style
21+
license that can be found in the LICENSE file.`)
22+
23+
err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
24+
if err != nil {
25+
return err
26+
}
27+
28+
// Skip directories starting with "." or "_", and testdata directories.
29+
if d.IsDir() && d.Name() != "." &&
30+
(strings.HasPrefix(d.Name(), ".") ||
31+
strings.HasPrefix(d.Name(), "_") ||
32+
filepath.Base(d.Name()) == "testdata") {
33+
34+
return filepath.SkipDir
35+
}
36+
37+
// Skip non-go files.
38+
if !strings.HasSuffix(path, ".go") {
39+
return nil
40+
}
41+
42+
// Check the copyright header.
43+
f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments|parser.PackageClauseOnly)
44+
if err != nil {
45+
return fmt.Errorf("parsing %s: %v", path, err)
46+
}
47+
if len(f.Comments) == 0 {
48+
t.Errorf("File %s must start with a copyright header matching %s", path, re)
49+
} else if !re.MatchString(f.Comments[0].Text()) {
50+
t.Errorf("Header comment for %s does not match expected copyright header.\ngot:\n%s\nwant matching:%s", path, f.Comments[0].Text(), re)
51+
}
52+
return nil
53+
})
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
}

0 commit comments

Comments
 (0)