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", 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", 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") } }