@@ -205,9 +205,10 @@ func rungo(t *testing.T, args ...string) {
205205}
206206
207207func TestExportedSymbolsHaveDocs (t * testing.T ) {
208+ packageHasComment := make (map [string ]bool )
208209 err := filepath .WalkDir ("." , func (path string , d os.DirEntry , err error ) error {
209210 if err != nil || d .IsDir () || ! strings .HasSuffix (path , ".go" ) ||
210- strings .HasSuffix (path , "_test.go" ) || strings .HasSuffix (path , ".pb.go" ) {
211+ strings .HasSuffix (path , "_test.go" ) || strings .HasSuffix (path , ".pb.go" ) || strings . Contains ( path , "testdata" ) {
211212 return nil
212213 }
213214
@@ -218,6 +219,8 @@ func TestExportedSymbolsHaveDocs(t *testing.T) {
218219 return nil
219220 }
220221
222+ recordPackageCommentStatus (t , node , packageHasComment )
223+
221224 // Visit every top-level declaration in the file.
222225 for _ , decl := range node .Decls {
223226 gen , ok := decl .(* ast.GenDecl )
@@ -242,6 +245,12 @@ func TestExportedSymbolsHaveDocs(t *testing.T) {
242245 if err != nil {
243246 t .Fatal (err )
244247 }
248+
249+ for name , hasPkgComment := range packageHasComment {
250+ if ! hasPkgComment {
251+ t .Errorf ("package %s does not have package comment" , name )
252+ }
253+ }
245254}
246255
247256func checkDoc (t * testing.T , name * ast.Ident , doc * ast.CommentGroup , path string ) {
@@ -254,3 +263,13 @@ func checkDoc(t *testing.T, name *ast.Ident, doc *ast.CommentGroup, path string)
254263 path , name .Name )
255264 }
256265}
266+
267+ // recordPackageCommentStatus updates the seen map with the package comment status for a given package, processing each
268+ // package only once.
269+ func recordPackageCommentStatus (t * testing.T , file * ast.File , packageHasComment map [string ]bool ) {
270+ t .Helper ()
271+ pkg := file .Name .String ()
272+ if ! packageHasComment [pkg ] {
273+ packageHasComment [pkg ] = file .Doc != nil
274+ }
275+ }
0 commit comments