gitFlow/internal/app/app_test.go
dimitar c9ff44298c feat: phase 3 — presentation layer (table, JSON, compact)
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.
2026-08-02 08:41:18 +02:00

75 lines
1.9 KiB
Go

package app
import (
"context"
"os/exec"
"path/filepath"
"testing"
"gitea.oblak.solutions/dimitar/gitFlow/internal/config"
"gitea.oblak.solutions/dimitar/gitFlow/pkg/status"
)
func initGitRepo(t *testing.T, path string) {
t.Helper()
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
cmd := exec.Command("git", "init", "-q", "-b", "main", filepath.Base(path))
cmd.Dir = filepath.Dir(path)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git init: %v\n%s", err, out)
}
}
func TestNewValidatesConfig(t *testing.T) {
bad := &config.Config{Dir: "", Format: "xml", Workers: 0}
if _, err := New(bad); err == nil {
t.Error("New(bad config) succeeded, want error")
}
}
func TestScanOnce(t *testing.T) {
root := t.TempDir()
repo := filepath.Join(root, "repo")
initGitRepo(t, repo)
cfg := &config.Config{Dir: root, Format: "table", Color: "auto", Workers: 4}
a, err := New(cfg)
if err != nil {
t.Fatalf("New: %v", err)
}
result, err := a.ScanOnce(context.Background())
if err != nil {
t.Fatalf("ScanOnce: %v", err)
}
if result.ParentDir != root {
t.Errorf("ParentDir = %q, want %q", result.ParentDir, root)
}
if len(result.Repos) != 1 {
t.Fatalf("ScanOnce found %d repos, want 1", len(result.Repos))
}
info := result.Repos[0]
if info.Path != repo {
t.Errorf("Repo path = %q, want %q", info.Path, repo)
}
if info.Status != status.StatusClean {
t.Errorf("Repo status = %v, want clean", info.Status)
}
if result.ScannedAt.IsZero() {
t.Error("ScannedAt is zero")
}
}
func TestScanOnceMissingDir(t *testing.T) {
cfg := &config.Config{Dir: filepath.Join(t.TempDir(), "missing"), Format: "table", Color: "auto", Workers: 4}
a, err := New(cfg)
if err != nil {
t.Fatalf("New: %v", err)
}
if _, err := a.ScanOnce(context.Background()); err == nil {
t.Error("ScanOnce(missing dir) succeeded, want error")
}
}