test-environment-detection.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Test environment detection logic from PRI-823
  4. # Tests the git-dir vs git-common-dir comparison used by
  5. # using-git-worktrees Step 0 and finishing-a-development-branch Step 1.5
  6. PASS=0
  7. FAIL=0
  8. TEMP_DIR=$(mktemp -d)
  9. trap 'rm -rf "$TEMP_DIR"' EXIT
  10. log_pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
  11. log_fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
  12. # Helper: run detection and return "linked" or "normal"
  13. detect_worktree() {
  14. local git_dir git_common
  15. git_dir=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
  16. git_common=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
  17. if [ "$git_dir" != "$git_common" ]; then
  18. echo "linked"
  19. else
  20. echo "normal"
  21. fi
  22. }
  23. echo "=== Test 1: Normal repo detection ==="
  24. cd "$TEMP_DIR"
  25. git init test-repo > /dev/null 2>&1
  26. cd test-repo
  27. git commit --allow-empty -m "init" > /dev/null 2>&1
  28. result=$(detect_worktree)
  29. if [ "$result" = "normal" ]; then
  30. log_pass "Normal repo detected as normal"
  31. else
  32. log_fail "Normal repo detected as '$result' (expected 'normal')"
  33. fi
  34. echo "=== Test 2: Linked worktree detection ==="
  35. git worktree add "$TEMP_DIR/test-wt" -b test-branch > /dev/null 2>&1
  36. cd "$TEMP_DIR/test-wt"
  37. result=$(detect_worktree)
  38. if [ "$result" = "linked" ]; then
  39. log_pass "Linked worktree detected as linked"
  40. else
  41. log_fail "Linked worktree detected as '$result' (expected 'linked')"
  42. fi
  43. echo "=== Test 3: Detached HEAD detection ==="
  44. git checkout --detach HEAD > /dev/null 2>&1
  45. branch=$(git branch --show-current)
  46. if [ -z "$branch" ]; then
  47. log_pass "Detached HEAD: branch is empty"
  48. else
  49. log_fail "Detached HEAD: branch is '$branch' (expected empty)"
  50. fi
  51. echo "=== Test 4: Linked worktree + detached HEAD (Codex App simulation) ==="
  52. result=$(detect_worktree)
  53. branch=$(git branch --show-current)
  54. if [ "$result" = "linked" ] && [ -z "$branch" ]; then
  55. log_pass "Codex App simulation: linked + detached HEAD"
  56. else
  57. log_fail "Codex App simulation: result='$result', branch='$branch'"
  58. fi
  59. echo "=== Test 5: Cleanup guard — linked worktree should NOT remove ==="
  60. cd "$TEMP_DIR/test-wt"
  61. result=$(detect_worktree)
  62. if [ "$result" = "linked" ]; then
  63. log_pass "Cleanup guard: linked worktree correctly detected (would skip removal)"
  64. else
  65. log_fail "Cleanup guard: expected 'linked', got '$result'"
  66. fi
  67. echo "=== Test 6: Cleanup guard — main repo SHOULD remove ==="
  68. cd "$TEMP_DIR/test-repo"
  69. result=$(detect_worktree)
  70. if [ "$result" = "normal" ]; then
  71. log_pass "Cleanup guard: main repo correctly detected (would proceed with removal)"
  72. else
  73. log_fail "Cleanup guard: expected 'normal', got '$result'"
  74. fi
  75. # Cleanup worktree before temp dir removal
  76. cd "$TEMP_DIR/test-repo"
  77. git worktree remove "$TEMP_DIR/test-wt" > /dev/null 2>&1 || true
  78. echo ""
  79. echo "=== Results: $PASS passed, $FAIL failed ==="
  80. if [ "$FAIL" -gt 0 ]; then
  81. exit 1
  82. fi