(t *testing.T)
| 26 | } |
| 27 | |
| 28 | func TestPackageDocs(t *testing.T) { |
| 29 | switch runtime.GOOS { |
| 30 | case "darwin", "linux": |
| 31 | // Enough coverage for CI+devs. |
| 32 | default: |
| 33 | t.Skipf("skipping on %s", runtime.GOOS) |
| 34 | } |
| 35 | |
| 36 | var goFiles []string |
| 37 | err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if fi.Mode().IsDir() && path != "." && strings.HasPrefix(filepath.Base(path), ".") { |
| 42 | return filepath.SkipDir // No documentation lives in dot directories (.git, .claude, etc) |
| 43 | } |
| 44 | if fi.Mode().IsDir() && filepath.Base(path) == "testdata" { |
| 45 | return filepath.SkipDir // testdata is ignored by the go tool; not real packages |
| 46 | } |
| 47 | if fi.Mode().IsRegular() && strings.HasSuffix(path, ".go") { |
| 48 | if strings.HasSuffix(path, "_test.go") { |
| 49 | return nil |
| 50 | } |
| 51 | goFiles = append(goFiles, path) |
| 52 | } |
| 53 | return nil |
| 54 | }) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | |
| 59 | byDir := map[string][]string{} // dir => files |
| 60 | for _, fileName := range goFiles { |
| 61 | fset := token.NewFileSet() |
| 62 | f, err := parser.ParseFile(fset, fileName, nil, parser.PackageClauseOnly|parser.ParseComments) |
| 63 | if err != nil { |
| 64 | t.Fatalf("failed to ParseFile %q: %v", fileName, err) |
| 65 | } |
| 66 | if hasIgnoreBuildTag(f) { |
| 67 | continue |
| 68 | } |
| 69 | dir := filepath.Dir(fileName) |
| 70 | if _, ok := byDir[dir]; !ok { |
| 71 | byDir[dir] = nil |
| 72 | } |
| 73 | if f.Doc != nil { |
| 74 | byDir[dir] = append(byDir[dir], fileName) |
| 75 | txt := f.Doc.Text() |
| 76 | if strings.Contains(txt, "SPDX-License-Identifier") { |
| 77 | t.Errorf("the copyright header for %s became its package doc due to missing blank line", fileName) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | for dir, ff := range byDir { |
| 82 | if len(ff) > 1 { |
| 83 | t.Errorf("multiple files with package doc in %s: %q", dir, ff) |
| 84 | } |
| 85 | if len(ff) == 0 { |
nothing calls this directly
no test coverage detected
searching dependent graphs…