Skip to content

Commit 90b78bd

Browse files
committed
fix(coverage): cover three uncovered diff paths to pass patch gate
- extract executeMetricsGet(w io.Writer, format, path) int from runMetricsGet, matching the executeMetricsRank pattern, so the writeGetOutput error path is testable with an injected writer - add TestExecuteMetricsGet_WriteError_ExitsTwo to exercise the new function's write-error branch - add TestExecuteMetricsRank_UnknownFormat_ExitsTwo to exercise the validateOutputFormat early-return in executeMetricsRank - add TestMET008_Readability_PlainTextErrorAfterWordCountSuccess to cover the PlainText error path in MET008 when WordCount succeeds with a non-zero count (the existing PlainTextError test only hits the WordCount error path) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BjaGA7w2gqtcdgMbGjgsh9
1 parent 7143b69 commit 90b78bd

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

cmd/mdsmith/metrics.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,18 @@ func runMetricsGet(args []string) int {
451451
return 2
452452
}
453453

454+
return executeMetricsGet(os.Stdout, format, path)
455+
}
456+
457+
func executeMetricsGet(w io.Writer, format, path string) int {
454458
defs := metricspkg.ForScope(metricspkg.ScopeFile)
455459
rows, err := metricspkg.Collect([]string{path}, defs, bytelimit.DefaultMaxInputBytes)
456460
if err != nil {
457461
fmt.Fprintf(os.Stderr, "mdsmith: %v\n", err)
458462
return 2
459463
}
460464

461-
if err := writeGetOutput(os.Stdout, format, rows[0], defs); err != nil {
465+
if err := writeGetOutput(w, format, rows[0], defs); err != nil {
462466
fmt.Fprintf(os.Stderr, "mdsmith: writing output: %v\n", err)
463467
return 2
464468
}

cmd/mdsmith/metrics_unit_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ func TestResolveRankSelection_ByNotInDefaultsGetsAppended(t *testing.T) {
168168
assert.True(t, containsMetric(defs, byDef.ID))
169169
}
170170

171+
// --- executeMetricsRank ---
172+
173+
func TestExecuteMetricsRank_UnknownFormat_ExitsTwo(t *testing.T) {
174+
captureStderr(func() {
175+
opts := metricsRankOptions{format: "toml"}
176+
code := executeMetricsRank(opts, nil)
177+
assert.Equal(t, 2, code)
178+
})
179+
}
180+
171181
// --- writeRankOutput ---
172182

173183
func TestWriteRankOutput_UnknownFormat_Error(t *testing.T) {
@@ -641,6 +651,14 @@ func TestRunMetricsGet_RealFile_YAMLContainsSentences(t *testing.T) {
641651
assert.Contains(t, out, "readability")
642652
}
643653

654+
func TestExecuteMetricsGet_WriteError_ExitsTwo(t *testing.T) {
655+
path := testMetricsFile(t)
656+
captureStderr(func() {
657+
code := executeMetricsGet(&alwaysErrorWriter{}, "json", path)
658+
assert.Equal(t, 2, code)
659+
})
660+
}
661+
644662
func TestRunMetricsGet_RealFile_JSONAndYAMLAgreeOnSentences(t *testing.T) {
645663
path := testMetricsFile(t)
646664
jsonOut := captureStdout(func() {

internal/metrics/metrics_morecoverage_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ func TestMET008_Readability_PlainTextError(t *testing.T) {
249249
assert.False(t, v.Available)
250250
}
251251

252+
func TestMET008_Readability_PlainTextErrorAfterWordCountSuccess(t *testing.T) {
253+
def, ok := Lookup("MET008")
254+
require.True(t, ok, "MET008 must be registered")
255+
256+
doc := NewDocument("test.md", []byte("# Hello\n"))
257+
sentinel := errors.New("plain-text failure")
258+
// WordCount succeeds with a non-zero result; the subsequent PlainText call fails.
259+
doc.wordCountReady = true
260+
doc.wordCount = 5
261+
doc.wordCountErr = nil
262+
doc.plainTextReady = true
263+
doc.plainTextErr = sentinel
264+
265+
v, err := def.Compute(doc)
266+
require.Error(t, err)
267+
assert.ErrorIs(t, err, sentinel)
268+
assert.False(t, v.Available)
269+
}
270+
252271
func TestMET009_Sentences_PlainTextError(t *testing.T) {
253272
def, ok := Lookup("MET009")
254273
require.True(t, ok, "MET009 must be registered")

0 commit comments

Comments
 (0)