Replace the interim renderers with a dedicated presenter package that
formats scan results in three ways.
Presenter (internal/presenter):
- Formatter interface with Present()/For() dispatch on format name:
table, json, compact
- TableFormatter: aligned tabwriter table with REPOSITORY / BRANCH /
STATUS / AHEAD-BEHIND / CHANGES / STASH columns, per-repo change
summaries like "2M 1U", error details inline, and a colored summary
line ("N repos | N clean | N need attention | N errors")
- JSONFormatter: indented document with scanned_at, parent_dir, repos,
and an aggregate summary for scripting; statuses render as string
labels ("modified") instead of raw integers
- CompactFormatter: one line per repo with color-coded status symbols
(✓ ✗ ↑ ↓ ⇄ ◉ ▢ !) plus branch, ahead/behind, and change counts
- Color handling: auto/always/never modes, TTY detection, and the
NO_COLOR convention (explicit --color=always still wins); paths have
$HOME collapsed to "~" in table and compact views
Domain model (pkg/status):
- JSON tags on RepoInfo/ScanResult/Summary for clean field names
- RepoStatus now marshals to its string label and unmarshals from both
string labels and numeric values, so JSON output round-trips
Configuration (internal/config):
- New --color flag (auto/always/never) validated in Load and included in
the config dump; keyed as "color" in viper
CLI (cmd/gitflow):
- scan now routes through presenter.Present with the resolved color mode;
the interim renderers are removed
Testing:
- Table content (columns, change summaries, error text, summary line) and
absence of escape codes with ColorNever
- ColorAlways emits ANSI codes even under NO_COLOR; auto stays clean on
non-terminal writers
- JSON decodes back into the domain types (string statuses round-trip)
- Compact symbols and counts; unknown formats rejected
- RepoStatus JSON round trip covers every status
Verified: go build, go vet, go test -race, gofmt clean; manual smoke of
table / compact / forced-color / JSON output against a scratch directory.
148 lines
4.5 KiB
Go
148 lines
4.5 KiB
Go
package presenter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.oblak.solutions/dimitar/gitFlow/pkg/status"
|
|
)
|
|
|
|
func sampleResult() status.ScanResult {
|
|
return status.ScanResult{
|
|
ScannedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC),
|
|
ParentDir: "/home/user/projects",
|
|
Repos: []status.RepoInfo{
|
|
{Path: "/home/user/projects/api", Name: "api", Branch: "main", Status: status.StatusClean},
|
|
{
|
|
Path: "/home/user/projects/web",
|
|
Name: "web",
|
|
Branch: "feat/login",
|
|
Status: status.StatusModified,
|
|
ModifiedFiles: []string{"a.go"},
|
|
UntrackedFiles: []string{"b"},
|
|
AheadBy: 3,
|
|
},
|
|
{Path: "/home/user/projects/lib", Name: "lib", Branch: "main", Status: status.StatusBehind, BehindBy: 5},
|
|
{Path: "/home/user/projects/legacy", Name: "legacy", Branch: "(detached)", Detached: true, Status: status.StatusDetached},
|
|
{Path: "/home/user/projects/broken", Name: "broken", Status: status.StatusError, Error: "git status: fatal: not a git repository"},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestParseColorMode(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
want ColorMode
|
|
}{
|
|
{"auto", ColorAuto},
|
|
{"always", ColorAlways},
|
|
{"never", ColorNever},
|
|
{"bogus", ColorAuto},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := ParseColorMode(tc.in); got != tc.want {
|
|
t.Errorf("ParseColorMode(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTableFormat(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
opts := Options{Color: ColorNever}
|
|
if err := Present(&buf, "table", sampleResult(), opts); err != nil {
|
|
t.Fatalf("Present: %v", err)
|
|
}
|
|
out := buf.String()
|
|
for _, want := range []string{
|
|
"REPOSITORY", "BRANCH", "STATUS", "AHEAD/BEHIND", "CHANGES",
|
|
"api", "feat/login", "1M 1U", "0/5", "3/0",
|
|
"5 repos | 1 clean | 3 need attention | 1 errors",
|
|
"not a git repository",
|
|
} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("table output missing %q:\n%s", want, out)
|
|
}
|
|
}
|
|
if strings.Contains(out, "\x1b[") {
|
|
t.Errorf("table output contains escape codes with ColorNever:\n%q", out)
|
|
}
|
|
}
|
|
|
|
func TestTableColorAlways(t *testing.T) {
|
|
t.Setenv("NO_COLOR", "1") // explicit always must win over NO_COLOR
|
|
var buf bytes.Buffer
|
|
if err := Present(&buf, "table", sampleResult(), Options{Color: ColorAlways}); err != nil {
|
|
t.Fatalf("Present: %v", err)
|
|
}
|
|
if !strings.Contains(buf.String(), "\x1b[") {
|
|
t.Error("ColorAlways produced no escape codes")
|
|
}
|
|
}
|
|
|
|
func TestTableRespectsNoColor(t *testing.T) {
|
|
t.Setenv("NO_COLOR", "1")
|
|
var buf bytes.Buffer
|
|
// ColorAuto with a non-terminal writer: no color regardless of NO_COLOR,
|
|
// but this also verifies NO_COLOR is read without panicking.
|
|
if err := Present(&buf, "table", sampleResult(), Options{Color: ColorAuto}); err != nil {
|
|
t.Fatalf("Present: %v", err)
|
|
}
|
|
if strings.Contains(buf.String(), "\x1b[") {
|
|
t.Error("auto color on non-TTY writer produced escape codes")
|
|
}
|
|
}
|
|
|
|
func TestJSONFormat(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
if err := Present(&buf, "json", sampleResult(), Options{}); err != nil {
|
|
t.Fatalf("Present: %v", err)
|
|
}
|
|
var doc struct {
|
|
ScannedAt time.Time `json:"scanned_at"`
|
|
ParentDir string `json:"parent_dir"`
|
|
Repos []status.RepoInfo `json:"repos"`
|
|
Summary status.Summary `json:"summary"`
|
|
}
|
|
if err := json.Unmarshal(buf.Bytes(), &doc); err != nil {
|
|
t.Fatalf("output is not valid JSON: %v\n%s", err, buf.String())
|
|
}
|
|
if doc.ParentDir != "/home/user/projects" {
|
|
t.Errorf("parent_dir = %q", doc.ParentDir)
|
|
}
|
|
if len(doc.Repos) != 5 {
|
|
t.Errorf("len(repos) = %d, want 5", len(doc.Repos))
|
|
}
|
|
if doc.Repos[1].Status != status.StatusModified {
|
|
t.Errorf("repos[1].status = %v, want modified", doc.Repos[1].Status)
|
|
}
|
|
if doc.Summary.Total != 5 || doc.Summary.Clean != 1 || doc.Summary.Attention != 3 || doc.Summary.Errored != 1 {
|
|
t.Errorf("summary = %+v", doc.Summary)
|
|
}
|
|
// Status must marshal as a string label, not an int.
|
|
if !strings.Contains(buf.String(), `"status": "modified"`) {
|
|
t.Errorf("status not marshaled as string label:\n%s", buf.String())
|
|
}
|
|
}
|
|
|
|
func TestCompactFormat(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
if err := Present(&buf, "compact", sampleResult(), Options{Color: ColorNever}); err != nil {
|
|
t.Fatalf("Present: %v", err)
|
|
}
|
|
out := buf.String()
|
|
for _, want := range []string{"✓", "✗", "↓", "◉", "!", "+3/-0", "2 change(s)"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("compact output missing %q:\n%s", want, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestForRejectsUnknownFormat(t *testing.T) {
|
|
if _, err := For("xml", Options{}); err == nil {
|
|
t.Error("For(xml) succeeded, want error")
|
|
}
|
|
}
|