|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/dfanso/commit-msg/pkg/types" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIsRepository(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + if _, err := exec.LookPath("git"); err != nil { |
| 17 | + t.Skip("git executable not available") |
| 18 | + } |
| 19 | + |
| 20 | + t.Run("returns true for initialized repo", func(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + dir := t.TempDir() |
| 24 | + |
| 25 | + cmd := exec.Command("git", "init", dir) |
| 26 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 27 | + t.Fatalf("failed to init git repo: %v: %s", err, string(output)) |
| 28 | + } |
| 29 | + |
| 30 | + if got := IsRepository(dir); !got { |
| 31 | + t.Fatalf("IsRepository(%q) = false, want true", dir) |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("returns false for non repo", func(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + dir := t.TempDir() |
| 39 | + if got := IsRepository(dir); got { |
| 40 | + t.Fatalf("IsRepository(%q) = true, want false", dir) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("returns false for missing path", func(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + dir := t.TempDir() |
| 48 | + missing := filepath.Join(dir, "does-not-exist") |
| 49 | + if got := IsRepository(missing); got { |
| 50 | + t.Fatalf("IsRepository(%q) = true, want false", missing) |
| 51 | + } |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func TestGetChangesIncludesSections(t *testing.T) { |
| 56 | + if testing.Short() { |
| 57 | + t.Skip("skipping integration-style test in short mode") |
| 58 | + } |
| 59 | + |
| 60 | + if _, err := exec.LookPath("git"); err != nil { |
| 61 | + t.Skip("git executable not available") |
| 62 | + } |
| 63 | + |
| 64 | + dir := t.TempDir() |
| 65 | + |
| 66 | + runGit(t, dir, "init") |
| 67 | + runGit(t, dir, "config", "user.name", "Test User") |
| 68 | + runGit(t, dir, "config", "user.email", "test@example.com") |
| 69 | + |
| 70 | + tracked := filepath.Join(dir, "tracked.txt") |
| 71 | + if err := os.WriteFile(tracked, []byte("first version\n"), 0o644); err != nil { |
| 72 | + t.Fatalf("failed to write tracked file: %v", err) |
| 73 | + } |
| 74 | + runGit(t, dir, "add", "tracked.txt") |
| 75 | + runGit(t, dir, "commit", "-m", "initial commit") |
| 76 | + |
| 77 | + if err := os.WriteFile(tracked, []byte("updated version\n"), 0o644); err != nil { |
| 78 | + t.Fatalf("failed to modify tracked file: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + staged := filepath.Join(dir, "staged.txt") |
| 82 | + if err := os.WriteFile(staged, []byte("staged content\n"), 0o644); err != nil { |
| 83 | + t.Fatalf("failed to write staged file: %v", err) |
| 84 | + } |
| 85 | + runGit(t, dir, "add", "staged.txt") |
| 86 | + |
| 87 | + untracked := filepath.Join(dir, "new.txt") |
| 88 | + if err := os.WriteFile(untracked, []byte("brand new file\n"), 0o644); err != nil { |
| 89 | + t.Fatalf("failed to write untracked file: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + output, err := GetChanges(&types.RepoConfig{Path: dir}) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("GetChanges returned error: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + fragments := []string{ |
| 98 | + "Unstaged changes:", |
| 99 | + "Staged changes:", |
| 100 | + "Untracked files:", |
| 101 | + "Content of new file new.txt:", |
| 102 | + "Recent commits for context:", |
| 103 | + "brand new file", |
| 104 | + } |
| 105 | + |
| 106 | + for _, fragment := range fragments { |
| 107 | + if !strings.Contains(output, fragment) { |
| 108 | + t.Fatalf("output missing fragment %q\noutput: %s", fragment, output) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestGetChangesErrorsOutsideRepo(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + |
| 116 | + dir := t.TempDir() |
| 117 | + |
| 118 | + if _, err := GetChanges(&types.RepoConfig{Path: dir}); err == nil { |
| 119 | + t.Fatal("expected error for directory without git repository") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func runGit(t *testing.T, dir string, args ...string) { |
| 124 | + t.Helper() |
| 125 | + |
| 126 | + cmdArgs := append([]string{"-C", dir}, args...) |
| 127 | + cmd := exec.Command("git", cmdArgs...) |
| 128 | + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") |
| 129 | + output, err := cmd.CombinedOutput() |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("git %v failed: %v\n%s", args, err, output) |
| 132 | + } |
| 133 | +} |
0 commit comments