|
| 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 | + if d.IsDir() && d.Name() != "." && |
| 28 | + (strings.HasPrefix(d.Name(), ".") || |
| 29 | + strings.HasPrefix(d.Name(), "_") || |
| 30 | + filepath.Base(d.Name()) == "testdata") { |
| 31 | + |
| 32 | + // Skip directories starting with "." or "_", and testdata directories. |
| 33 | + return filepath.SkipDir |
| 34 | + } |
| 35 | + |
| 36 | + if !strings.HasSuffix(path, ".go") { |
| 37 | + // Skip non-go files. |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + // Check the copyright header. |
| 42 | + f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments|parser.PackageClauseOnly) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("parsing %s: %v", path, err) |
| 45 | + } |
| 46 | + if len(f.Comments) == 0 { |
| 47 | + t.Errorf("File %s must start with a copyright header matching %s", path, re) |
| 48 | + } else if !re.MatchString(f.Comments[0].Text()) { |
| 49 | + t.Errorf("Header comment for %s does not match expected copyright header.\ngot:\n%s\nwant matching:%s", path, f.Comments[0].Text(), re) |
| 50 | + } |
| 51 | + return nil |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | +} |
0 commit comments