Skip to content

Commit 6463da2

Browse files
committed
test: cover ErrorFormatter direct path and single-char inverse flag
Brings codecov patch coverage to 100% for the changed lines: - TestHandleExitCoder_ErrorFormatterDirect exercises the Fprintf branch in HandleExitCoder when an ExitCoder also implements ErrorFormatter and is passed in directly (the existing TestHandleExitCoder_ErrorFormatter routes through a multiError which goes down a different branch). - TestBoolWithInverseFlagStringNoPanicWithNoTabStringer is split into multi-character and single-character sub-tests so the `-` vs `--` prefix branch is covered. Signed-off-by: alliasgher <alliasgher123@gmail.com>
1 parent bb2e3ea commit 6463da2

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

errors_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,23 @@ func TestHandleExitCoder_EmptyMessage(t *testing.T) {
257257
assert.True(t, called)
258258
assert.Empty(t, ErrWriter.(*bytes.Buffer).String(), "expected no output for empty-message exit")
259259
}
260+
261+
// TestHandleExitCoder_ErrorFormatterDirect verifies the ErrorFormatter branch
262+
// (Fprintf path) is exercised when the ExitCoder is passed to HandleExitCoder
263+
// directly with a non-empty message. Distinct from
264+
// TestHandleExitCoder_ErrorFormatter above which routes through a multiError.
265+
func TestHandleExitCoder_ErrorFormatterDirect(t *testing.T) {
266+
called := false
267+
OsExiter = func(rc int) { called = true }
268+
ErrWriter = &bytes.Buffer{}
269+
270+
defer func() {
271+
OsExiter = fakeOsExiter
272+
ErrWriter = fakeErrWriter
273+
}()
274+
275+
HandleExitCoder(&exitFormatter{code: 7})
276+
277+
assert.True(t, called)
278+
assert.Contains(t, ErrWriter.(*bytes.Buffer).String(), "some other special")
279+
}

flag_bool_with_inverse_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,19 @@ func TestBoolWithInverseFlagStringNoPanicWithNoTabStringer(t *testing.T) {
533533
return "no tab here"
534534
}
535535

536-
flag := &BoolWithInverseFlag{
537-
Name: "verbose",
538-
}
536+
t.Run("multi-character name uses -- prefix", func(t *testing.T) {
537+
flag := &BoolWithInverseFlag{Name: "verbose"}
538+
got := flag.String() // must not panic
539+
if !strings.Contains(got, "--[no-]verbose") {
540+
t.Errorf("expected String() to contain --[no-]verbose, got %q", got)
541+
}
542+
})
539543

540-
// Must not panic.
541-
got := flag.String()
542-
if !strings.Contains(got, "verbose") {
543-
t.Errorf("expected String() to contain the flag name, got %q", got)
544-
}
544+
t.Run("single-character name uses - prefix", func(t *testing.T) {
545+
flag := &BoolWithInverseFlag{Name: "v"}
546+
got := flag.String() // must not panic
547+
if !strings.Contains(got, "-[no-]v") {
548+
t.Errorf("expected String() to contain -[no-]v, got %q", got)
549+
}
550+
})
545551
}

0 commit comments

Comments
 (0)