Skip to content

Commit f2b1bf9

Browse files
authored
Merge pull request #224 from oferchen/codex/add-comment-checker-and-tests
Add comment checker and integrate into CI
2 parents f457e57 + 6d8ec78 commit f2b1bf9

File tree

5 files changed

+108
-9
lines changed

5 files changed

+108
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- name: Govulncheck
3131
run: govulncheck ./...
3232
continue-on-error: true
33+
- run: make commentcheck
3334
- run: go test -race -shuffle=on -cover -coverprofile=coverage.out ./...
3435
- run: go build ./...
3536
- run: make cover

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ jobs:
3434
- name: Lint
3535
run: make lint
3636

37+
- name: Comment check
38+
run: make commentcheck
39+
3740
- name: Test (race)
3841
run: make test-race
3942

4043
- name: Cover
4144
run: make cover
4245

43-
- name: Comment check
44-
run: make commentcheck
45-
4646
- name: Vulnerability check (non-blocking)
4747
continue-on-error: true
4848
run: |

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ GO ?= go
99
FMT_PKGS := $(shell $(GO) list $(PKG))
1010
FMT_DIRS := $(shell $(GO) list -f '{{.Dir}}' $(PKG))
1111

12-
.PHONY: all init tidy fmt lint vet vuln test test-race cover cover-html build ci clean
12+
.PHONY: all init tidy fmt lint vet vuln commentcheck test test-race cover cover-html build ci clean
1313

1414
all: build
1515

@@ -25,11 +25,14 @@ fmt:
2525
$(GO) run mvdan.cc/gofumpt@latest -w $(FMT_DIRS)
2626

2727
lint:
28-
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
29-
golangci-lint run --timeout=5m
28+
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
29+
golangci-lint run --timeout=5m
30+
31+
commentcheck:
32+
$(GO) run ./cmd/commentcheck
3033

3134
vet:
32-
$(GO) vet $(PKG)
35+
$(GO) vet $(PKG)
3336

3437
test:
3538
mkdir -p $(BUILD_DIR)
@@ -50,9 +53,9 @@ build:
5053
$(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(BUILD_DIR)/$(APP) ./cmd/hclalign
5154

5255
vuln:
53-
$(GO) run golang.org/x/vuln/cmd/govulncheck@latest $(PKG)
56+
$(GO) run golang.org/x/vuln/cmd/govulncheck@latest $(PKG)
5457

55-
ci: tidy fmt lint vuln test cover build
58+
ci: tidy fmt lint vuln commentcheck test cover build
5659

5760
clean:
5861
rm -rf $(BUILD_DIR)

cmd/commentcheck/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// cmd/commentcheck/main.go
2+
package main
3+
4+
import (
5+
"fmt"
6+
"go/parser"
7+
"go/token"
8+
"io/fs"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
func check(root string) error {
15+
var bad []string
16+
walk := func(path string, d fs.DirEntry, err error) error {
17+
if err != nil {
18+
return err
19+
}
20+
if d.IsDir() {
21+
if d.Name() == "vendor" || strings.HasPrefix(d.Name(), ".") {
22+
return filepath.SkipDir
23+
}
24+
return nil
25+
}
26+
if filepath.Ext(path) != ".go" {
27+
return nil
28+
}
29+
fset := token.NewFileSet()
30+
src, err := os.ReadFile(path)
31+
if err != nil {
32+
return err
33+
}
34+
file, err := parser.ParseFile(fset, path, src, parser.ParseComments)
35+
if err != nil {
36+
return err
37+
}
38+
for _, cg := range file.Comments {
39+
for _, c := range cg.List {
40+
if fset.Position(c.Slash).Line > 1 {
41+
bad = append(bad, path)
42+
return nil
43+
}
44+
}
45+
}
46+
return nil
47+
}
48+
if err := filepath.WalkDir(root, walk); err != nil {
49+
return err
50+
}
51+
if len(bad) > 0 {
52+
return fmt.Errorf("comments beyond first line: %s", strings.Join(bad, ", "))
53+
}
54+
return nil
55+
}
56+
57+
func main() {
58+
if err := check("."); err != nil {
59+
fmt.Fprintln(os.Stderr, err)
60+
os.Exit(1)
61+
}
62+
}

cmd/commentcheck/main_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// cmd/commentcheck/main_test.go
2+
package main
3+
4+
import (
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
func TestCheck(t *testing.T) {
11+
t.Run("compliant", func(t *testing.T) {
12+
dir := t.TempDir()
13+
write(t, dir, "ok.go", "// ok.go\npackage main\n")
14+
if err := check(dir); err != nil {
15+
t.Fatalf("unexpected error: %v", err)
16+
}
17+
})
18+
t.Run("noncompliant", func(t *testing.T) {
19+
dir := t.TempDir()
20+
write(t, dir, "bad.go", "// bad.go\npackage main\n// bad\n")
21+
if err := check(dir); err == nil {
22+
t.Fatalf("expected error")
23+
}
24+
})
25+
}
26+
27+
func write(t *testing.T, dir, name, content string) {
28+
t.Helper()
29+
path := filepath.Join(dir, name)
30+
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
31+
t.Fatalf("write %s: %v", name, err)
32+
}
33+
}

0 commit comments

Comments
 (0)