git-workflow.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. # Functional tests for git-workflow skill
  3. # Tests gh (GitHub CLI) and delta
  4. set -euo pipefail
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. PROJECT_ROOT="$SCRIPT_DIR/../../.."
  7. # Colors
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. NC='\033[0m'
  12. PASSED=0
  13. FAILED=0
  14. SKIPPED=0
  15. pass() { ((PASSED++)); echo -e "${GREEN}✓${NC} $1"; }
  16. fail() { ((FAILED++)); echo -e "${RED}✗${NC} $1: $2"; }
  17. skip() { ((SKIPPED++)); echo -e "${YELLOW}○${NC} $1 (skipped: $2)"; }
  18. check_prereqs() {
  19. local missing=()
  20. command -v gh >/dev/null 2>&1 || missing+=("gh")
  21. command -v delta >/dev/null 2>&1 || missing+=("delta")
  22. if [[ ${#missing[@]} -gt 0 ]]; then
  23. echo -e "${YELLOW}Missing tools: ${missing[*]}${NC}"
  24. echo "Install with: brew install ${missing[*]}"
  25. echo "Some tests will be skipped."
  26. echo ""
  27. fi
  28. }
  29. # === gh Tests ===
  30. test_gh_version() {
  31. if ! command -v gh >/dev/null 2>&1; then
  32. skip "gh: version check" "gh not installed"
  33. return
  34. fi
  35. local result
  36. result=$(gh --version 2>/dev/null | head -1)
  37. if [[ "$result" == *"gh version"* ]]; then
  38. pass "gh: version command works"
  39. else
  40. fail "gh: version" "unexpected output: $result"
  41. fi
  42. }
  43. test_gh_auth_status() {
  44. if ! command -v gh >/dev/null 2>&1; then
  45. skip "gh: auth status" "gh not installed"
  46. return
  47. fi
  48. local result exit_code
  49. result=$(gh auth status 2>&1) || exit_code=$?
  50. if [[ "$result" == *"Logged in"* ]]; then
  51. pass "gh: authenticated"
  52. elif [[ "$result" == *"not logged"* ]]; then
  53. skip "gh: auth status" "not authenticated (run 'gh auth login')"
  54. else
  55. fail "gh: auth status" "unexpected: $result"
  56. fi
  57. }
  58. test_gh_repo_view() {
  59. if ! command -v gh >/dev/null 2>&1; then
  60. skip "gh: repo view" "gh not installed"
  61. return
  62. fi
  63. # Check if we're in a git repo with a remote
  64. if ! git remote get-url origin >/dev/null 2>&1; then
  65. skip "gh: repo view" "no git remote configured"
  66. return
  67. fi
  68. local result
  69. result=$(gh repo view --json name 2>/dev/null | jq -r '.name' 2>/dev/null || echo "")
  70. if [[ -n "$result" && "$result" != "null" ]]; then
  71. pass "gh: repo view (name: $result)"
  72. else
  73. skip "gh: repo view" "not a GitHub repo or not authenticated"
  74. fi
  75. }
  76. test_gh_api() {
  77. if ! command -v gh >/dev/null 2>&1; then
  78. skip "gh: API access" "gh not installed"
  79. return
  80. fi
  81. local result
  82. result=$(gh api user --jq '.login' 2>/dev/null || echo "")
  83. if [[ -n "$result" ]]; then
  84. pass "gh: API access works (user: $result)"
  85. else
  86. skip "gh: API access" "not authenticated"
  87. fi
  88. }
  89. # === delta Tests ===
  90. test_delta_version() {
  91. if ! command -v delta >/dev/null 2>&1; then
  92. skip "delta: version check" "delta not installed"
  93. return
  94. fi
  95. local result
  96. result=$(delta --version 2>/dev/null)
  97. if [[ "$result" == *"delta"* ]]; then
  98. pass "delta: version command works"
  99. else
  100. fail "delta: version" "unexpected output"
  101. fi
  102. }
  103. test_delta_diff() {
  104. if ! command -v delta >/dev/null 2>&1; then
  105. skip "delta: diff formatting" "delta not installed"
  106. return
  107. fi
  108. local file1 file2 result
  109. file1=$(mktemp)
  110. file2=$(mktemp)
  111. echo "line 1" > "$file1"
  112. echo "line 2" > "$file2"
  113. result=$(diff -u "$file1" "$file2" | delta 2>/dev/null || true)
  114. rm -f "$file1" "$file2"
  115. if [[ -n "$result" ]]; then
  116. pass "delta: formats diff output"
  117. else
  118. fail "delta: diff formatting" "no output"
  119. fi
  120. }
  121. test_delta_git_diff() {
  122. if ! command -v delta >/dev/null 2>&1; then
  123. skip "delta: git diff" "delta not installed"
  124. return
  125. fi
  126. # Check if we're in a git repo
  127. if ! git rev-parse --git-dir >/dev/null 2>&1; then
  128. skip "delta: git diff" "not in a git repository"
  129. return
  130. fi
  131. # This just verifies delta can process git diff output
  132. local result
  133. result=$(git diff HEAD~1 --stat 2>/dev/null | delta 2>/dev/null || echo "ok")
  134. pass "delta: processes git diff"
  135. }
  136. # === lazygit Tests ===
  137. test_lazygit_version() {
  138. if ! command -v lazygit >/dev/null 2>&1; then
  139. skip "lazygit: version check" "lazygit not installed"
  140. return
  141. fi
  142. local result
  143. result=$(lazygit --version 2>/dev/null)
  144. if [[ -n "$result" ]]; then
  145. pass "lazygit: version command works"
  146. else
  147. fail "lazygit: version" "no output"
  148. fi
  149. }
  150. # === Run Tests ===
  151. main() {
  152. echo "=== git-workflow functional tests ==="
  153. echo ""
  154. check_prereqs
  155. echo "--- gh (GitHub CLI) tests ---"
  156. test_gh_version
  157. test_gh_auth_status
  158. test_gh_repo_view
  159. test_gh_api
  160. echo ""
  161. echo "--- delta tests ---"
  162. test_delta_version
  163. test_delta_diff
  164. test_delta_git_diff
  165. echo ""
  166. echo "--- lazygit tests ---"
  167. test_lazygit_version
  168. echo ""
  169. echo "=== Results ==="
  170. echo -e "Passed: ${GREEN}$PASSED${NC}"
  171. echo -e "Failed: ${RED}$FAILED${NC}"
  172. echo -e "Skipped: ${YELLOW}$SKIPPED${NC}"
  173. [[ $FAILED -eq 0 ]]
  174. }
  175. main "$@"