(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestFindGitRoot(t *testing.T) { |
| 14 | // Save current directory |
| 15 | originalWd, err := os.Getwd() |
| 16 | if err != nil { |
| 17 | t.Fatalf("Failed to get current working directory: %v", err) |
| 18 | } |
| 19 | |
| 20 | // Try to find the git root from current location |
| 21 | root, err := gitutil.FindGitRoot() |
| 22 | if err != nil { |
| 23 | // If we're not in a git repository, try changing to the project root |
| 24 | // This handles cases where tests are run from outside the git repo |
| 25 | projectRoot := filepath.Join(originalWd, "..", "..") |
| 26 | if err := os.Chdir(projectRoot); err != nil { |
| 27 | t.Skipf("Cannot find git root and cannot change to project root: %v", err) |
| 28 | } |
| 29 | defer func() { |
| 30 | _ = os.Chdir(originalWd) // Best effort restoration |
| 31 | }() |
| 32 | |
| 33 | // Try again from project root |
| 34 | root, err = gitutil.FindGitRoot() |
| 35 | if err != nil { |
| 36 | t.Skipf("Expected to find git root, but got error: %v", err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if root == "" { |
| 41 | t.Fatal("Expected non-empty git root") |
| 42 | } |
| 43 | |
| 44 | // Check that the returned path exists |
| 45 | if _, err := os.Stat(root); os.IsNotExist(err) { |
| 46 | t.Fatalf("Git root path does not exist: %s", root) |
| 47 | } |
| 48 | |
| 49 | // Check that .git directory exists in the root |
| 50 | gitDir := filepath.Join(root, ".git") |
| 51 | if _, err := os.Stat(gitDir); os.IsNotExist(err) { |
| 52 | t.Fatalf(".git directory does not exist in reported git root: %s", gitDir) |
| 53 | } |
| 54 | |
| 55 | t.Logf("Git root found: %s", root) |
| 56 | } |
nothing calls this directly
no test coverage detected