Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Commit 4faf495

Browse files
committed
test(cli): add coverage for exit codes, output parsing and NO_COLOR
1 parent 5923307 commit 4faf495

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

cmd/root_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/RewriteToday/cli/internal/clierr"
7+
)
8+
9+
func TestResolveOutputFormat(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
args []string
13+
want string
14+
}{
15+
{name: "default", args: []string{}, want: "text"},
16+
{name: "long flag", args: []string{"--output", "json"}, want: "json"},
17+
{name: "short flag", args: []string{"-o", "json"}, want: "json"},
18+
{name: "equals", args: []string{"--output=json"}, want: "json"},
19+
{name: "invalid falls back", args: []string{"--output=xml"}, want: "text"},
20+
}
21+
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := ResolveOutputFormat(tt.args); got != tt.want {
25+
t.Fatalf("expected %q, got %q", tt.want, got)
26+
}
27+
})
28+
}
29+
}
30+
31+
func TestNormalizeOutputFormat_InvalidReturnsUsageError(t *testing.T) {
32+
_, err := normalizeOutputFormat("xml")
33+
if err == nil {
34+
t.Fatal("expected an error")
35+
}
36+
37+
if got := clierr.ExitCode(err); got != int(clierr.CodeUsage) {
38+
t.Fatalf("expected code %d, got %d", clierr.CodeUsage, got)
39+
}
40+
}

internal/clierr/clierr_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package clierr
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
)
8+
9+
func TestExitCode_WithWrappedCode(t *testing.T) {
10+
err := Wrap(CodeUsage, errors.New("bad args"))
11+
12+
if got := ExitCode(err); got != int(CodeUsage) {
13+
t.Fatalf("expected %d, got %d", CodeUsage, got)
14+
}
15+
}
16+
17+
func TestExitCode_WithNetworkDeadline(t *testing.T) {
18+
err := context.DeadlineExceeded
19+
20+
if got := ExitCode(err); got != int(CodeNetwork) {
21+
t.Fatalf("expected %d, got %d", CodeNetwork, got)
22+
}
23+
}
24+
25+
func TestExitCode_WithUnknownError(t *testing.T) {
26+
err := errors.New("boom")
27+
28+
if got := ExitCode(err); got != int(CodeInternal) {
29+
t.Fatalf("expected %d, got %d", CodeInternal, got)
30+
}
31+
}

internal/render/colors_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package render
2+
3+
import "testing"
4+
5+
func TestIsColorEnabled_RespectsNoColor(t *testing.T) {
6+
t.Setenv("NO_COLOR", "1")
7+
if IsColorEnabled() {
8+
t.Fatal("expected colors to be disabled when NO_COLOR is set")
9+
}
10+
}
11+
12+
func TestIsColorEnabled_DefaultEnabled(t *testing.T) {
13+
t.Setenv("NO_COLOR", "")
14+
if !IsColorEnabled() {
15+
t.Fatal("expected colors to be enabled when NO_COLOR is empty")
16+
}
17+
}

0 commit comments

Comments
 (0)