(t *testing.T)
| 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 | t.Chdir(tmpDir) |
| 29 | |
| 30 | cmd := New() |
| 31 | ctx := context.Background() |
| 32 | |
| 33 | err := cmd.CanExecuteGitDiff(ctx) |
| 34 | if err == nil { |
| 35 | t.Error("CanExecuteGitDiff() should fail in a non-git directory") |
| 36 | } |
| 37 | |
| 38 | expectedMsg := "not a git repository" |
| 39 | if err != nil && err.Error() != expectedMsg { |
| 40 | t.Logf("Got expected error: %v", err) |
| 41 | } |
| 42 | }) |
| 43 | |
| 44 | // Test in a git repository subdirectory |
| 45 | t.Run("in git repository subdirectory", func(t *testing.T) { |
| 46 | // Create a test subdirectory in the git repository |
| 47 | tmpDir := filepath.Join(".", "test_subdir") |
| 48 | if err := os.MkdirAll(tmpDir, 0o755); err != nil { |
| 49 | t.Fatalf("Failed to create test directory: %v", err) |
| 50 | } |
| 51 | defer os.RemoveAll(tmpDir) |
| 52 | |
| 53 | // Change to the subdirectory |
| 54 | t.Chdir(tmpDir) |
| 55 | |
| 56 | cmd := New() |
| 57 | ctx := context.Background() |
| 58 | |
| 59 | err := cmd.CanExecuteGitDiff(ctx) |
| 60 | if err != nil { |
| 61 | t.Errorf( |
| 62 | "CanExecuteGitDiff() should succeed in a git repository subdirectory, got error: %v", |
| 63 | err, |
| 64 | ) |
| 65 | } |
| 66 | }) |
| 67 | } |
nothing calls this directly
no test coverage detected