Skip to content

Commit 1bd3e32

Browse files
authored
feat: add a package comment linter (#2712)
Update `TestExportedSymbolsHaveDocs` to check that each Go package includes a top-level doc comment. Fixes #628
1 parent 307136c commit 1bd3e32

File tree

22 files changed

+48
-2
lines changed

22 files changed

+48
-2
lines changed

all_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ func rungo(t *testing.T, args ...string) {
205205
}
206206

207207
func 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

247256
func 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+
}

internal/automation/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package automation implements the command-line interface and core logic
16+
// for Librarian's automated workflows.
1517
package automation
1618

1719
import (

internal/container/java/bazel/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package bazel provides utilities for parsing BUILD.bazel files.
1516
package bazel
1617

1718
import (

internal/container/java/execv/execv.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package execv provides a helper function for executing external commands.
1516
package execv
1617

1718
import (

internal/container/java/pom/pom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package pom handles the generation of Maven pom.xml files for a Java library.
1516
package pom
1617

1718
import (

internal/container/java/protoc/protoc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package protoc provides utilities for constructing protoc command arguments.
1516
package protoc
1617

1718
import (

internal/librarian/librarian.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package librarian provides the core implementation for the Librarian CLI tool.
1516
package librarian
1617

1718
import (

internal/semver/semver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package semver provides functionality for parsing, comparing, and manipulating
16+
// semantic version strings according to the SemVer 2.0.0 spec.
1517
package semver
1618

1719
import (
@@ -25,7 +27,7 @@ import (
2527
// Version represents a semantic version.
2628
type Version struct {
2729
Major, Minor, Patch int
28-
// Prerelease is the non-numeric part of the pre-release string (e.g., "alpha", "beta").
30+
// Prerelease is the non-numeric part of the prerelease string (e.g., "alpha", "beta").
2931
Prerelease string
3032
// PrereleaseSeparator is the separator between the pre-release string and
3133
// its version (e.g., ".").

internal/sidekick/internal/api/apitest/apitest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package apitest provides helper functions for testing the api package.
1516
package apitest
1617

1718
import (

internal/sidekick/internal/api/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package api defines the data model representing a parsed API surface.
1516
package api
1617

1718
import (

0 commit comments

Comments
 (0)