open-code-review/cmd/opencodereview/rules_check_test.go
kite 840f85f9bc
test: raise statement coverage to 90% and enforce it in CI (#747)
* test: raise statement coverage to 90% and enforce it in CI

Add unit tests across the cmd and internal packages to bring total
statement coverage above 90%, and gate future regressions.

- Cover CLI helpers, provider TUI handlers, resume/manifest paths, and
  error branches in config, llm, llmloop, scan, session, agent, viewer,
  mcp, pathutil, and telemetry.
- Raise the coverage threshold from 80% to 90% in the Makefile
  (COVERAGE_THRESHOLD) and in the CI "Check coverage threshold" step.
- Ignore generated coverage.out and coverage.html artifacts.

Total statement coverage is now 90.5%, measured consistently by both
`make coverage` and the CI `go test ./...` scope.

* test: widen statement coverage margin with environment-independent unit tests

Add table-driven unit tests for pure, environment-independent functions to
raise the statement-coverage safety margin above the 90% threshold:

- session.ResumeState.ValidateScanOptions (70% -> 100%)
- rules.SystemRule.UnmarshalJSON error branches (71% -> 82%)
- llmloop.stripMarkdownFences no-newline branch (82% -> 100%)
- diff.firstLine empty/blank-input branch
- diff.extractCodeBlock missing-newline and no-closing-fence branches
- main.truncate n<=1 and normalization branches
- agent.Agent nil-receiver accessor guards
2026-08-06 12:55:44 +08:00

36 lines
1,003 B
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package main
import (
"testing"
)
// TestRunRulesCheck drives the full `ocr rules check` helper against a real git
// repo so the resolver-build, DetailResolver assertion, and formatted print
// path all run.
func TestRunRulesCheck(t *testing.T) {
dir := initTestGitRepo(t)
// runRulesCheck reads the package-level flag var; set and restore it.
prev := rulesCheckRepoDir
rulesCheckRepoDir = dir
t.Cleanup(func() { rulesCheckRepoDir = prev })
t.Run("resolves a rule for a Go file", func(t *testing.T) {
silenceStdout(t, func() {
if err := runRulesCheck("internal/foo/bar.go"); err != nil {
t.Fatalf("runRulesCheck error: %v", err)
}
})
})
t.Run("non-git repo dir errors", func(t *testing.T) {
rulesCheckRepoDir = t.TempDir()
defer func() { rulesCheckRepoDir = dir }()
if err := runRulesCheck("x.go"); err == nil {
t.Fatal("expected error for non-git repo dir")
}
})
}