|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCanExecuteGitDiff(t *testing.T) { |
| 11 | + // Test in the current directory (which is a git repository) |
| 12 | + t.Run("in git repository", func(t *testing.T) { |
| 13 | + cmd := New() |
| 14 | + ctx := context.Background() |
| 15 | + |
| 16 | + err := cmd.CanExecuteGitDiff(ctx) |
| 17 | + if err != nil { |
| 18 | + t.Errorf("CanExecuteGitDiff() should succeed in a git repository, got error: %v", err) |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | + // Test in a non-git directory |
| 23 | + t.Run("not in git repository", func(t *testing.T) { |
| 24 | + // Create a temporary directory |
| 25 | + tmpDir := t.TempDir() |
| 26 | + |
| 27 | + // Change to the temporary directory |
| 28 | + originalDir, err := os.Getwd() |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to get current directory: %v", err) |
| 31 | + } |
| 32 | + defer func() { |
| 33 | + if err := os.Chdir(originalDir); err != nil { |
| 34 | + t.Logf("Failed to restore directory: %v", err) |
| 35 | + } |
| 36 | + }() |
| 37 | + |
| 38 | + if err := os.Chdir(tmpDir); err != nil { |
| 39 | + t.Fatalf("Failed to change directory: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + cmd := New() |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + err = cmd.CanExecuteGitDiff(ctx) |
| 46 | + if err == nil { |
| 47 | + t.Error("CanExecuteGitDiff() should fail in a non-git directory") |
| 48 | + } |
| 49 | + |
| 50 | + expectedMsg := "not a git repository" |
| 51 | + if err != nil && err.Error() != expectedMsg { |
| 52 | + t.Logf("Got expected error: %v", err) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + // Test in a git repository subdirectory |
| 57 | + t.Run("in git repository subdirectory", func(t *testing.T) { |
| 58 | + // Create a test subdirectory in the git repository |
| 59 | + tmpDir := filepath.Join(".", "test_subdir") |
| 60 | + if err := os.MkdirAll(tmpDir, 0o755); err != nil { |
| 61 | + t.Fatalf("Failed to create test directory: %v", err) |
| 62 | + } |
| 63 | + defer os.RemoveAll(tmpDir) |
| 64 | + |
| 65 | + // Change to the subdirectory |
| 66 | + originalDir, err := os.Getwd() |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to get current directory: %v", err) |
| 69 | + } |
| 70 | + defer func() { |
| 71 | + if err := os.Chdir(originalDir); err != nil { |
| 72 | + t.Logf("Failed to restore directory: %v", err) |
| 73 | + } |
| 74 | + }() |
| 75 | + |
| 76 | + if err := os.Chdir(tmpDir); err != nil { |
| 77 | + t.Fatalf("Failed to change directory: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + cmd := New() |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + err = cmd.CanExecuteGitDiff(ctx) |
| 84 | + if err != nil { |
| 85 | + t.Errorf("CanExecuteGitDiff() should succeed in a git repository subdirectory, got error: %v", err) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
0 commit comments