/ tests / test_extcomp.sh
test_extcomp.sh
  1  #!/bin/bash
  2  # Test external command completion (black-box forwarding)
  3  
  4  # shellcheck disable=SC1091
  5  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6  REPO_ROOT="$(dirname "$SCRIPT_DIR")"
  7  
  8  source "$REPO_ROOT/contrib/bar_complete"
  9  
 10  echo "Testing external command completion (__bar_comp_extcomp)..."
 11  
 12  # Test 1: Git completion
 13  echo ""
 14  echo "Test 1: Testing git completion..."
 15  
 16  # Test git subcommand completion
 17  if command -v git &>/dev/null; then
 18      completions=$(__bar_comp_extcomp git che 2>/dev/null)
 19      
 20      if echo "$completions" | grep -q "checkout"; then
 21          echo "✓ PASS: git subcommand completion works ('che' → 'checkout')"
 22      else
 23          echo "ℹ INFO: git completion not available or 'checkout' not found"
 24          echo "  Got: $(echo "$completions" | head -3 | tr '\n' ' ')"
 25      fi
 26      
 27      # Test git with no prefix
 28      all_completions=$(__bar_comp_extcomp git "" 2>/dev/null)
 29      completion_count=$(echo "$all_completions" | wc -l)
 30      
 31      if [[ $completion_count -gt 20 ]]; then
 32          echo "✓ PASS: git completion returns many subcommands ($completion_count)"
 33          echo "  Sample: $(echo "$all_completions" | head -5 | tr '\n' ' ')"
 34      else
 35          echo "ℹ INFO: git completion using fallback mode"
 36      fi
 37  else
 38      echo "ℹ INFO: git not installed, skipping test"
 39  fi
 40  
 41  # Test 2: Test with a command that might not have completion
 42  echo ""
 43  echo "Test 2: Testing fallback for command without completion..."
 44  
 45  completions=$(__bar_comp_extcomp nonexistent_cmd test 2>/dev/null)
 46  
 47  if [[ -n "$completions" ]]; then
 48      echo "✓ PASS: Fallback to file completion works"
 49      echo "  Sample: $(echo "$completions" | head -3 | tr '\n' ' ')"
 50  else
 51      echo "✓ PASS: No completions for nonexistent command (expected)"
 52  fi
 53  
 54  # Test 3: Test ls completion (uses _longopt)
 55  echo ""
 56  echo "Test 3: Testing ls completion..."
 57  
 58  if command -v ls &>/dev/null; then
 59      completions=$(__bar_comp_extcomp ls --col 2>/dev/null)
 60      
 61      if echo "$completions" | grep -q "color"; then
 62          echo "✓ PASS: ls flag completion works ('--col' → '--color')"
 63      else
 64          echo "ℹ INFO: ls completion not available or using fallback"
 65          echo "  Got: $(echo "$completions" | head -3 | tr '\n' ' ')"
 66      fi
 67  fi
 68  
 69  # Test 4: Test SSH completion
 70  echo ""
 71  echo "Test 4: Testing ssh completion..."
 72  
 73  if command -v ssh &>/dev/null; then
 74      # SSH completion might complete hostnames from known_hosts
 75      completions=$(__bar_comp_extcomp ssh "" 2>/dev/null | head -10)
 76      
 77      if [[ -n "$completions" ]]; then
 78          echo "✓ PASS: ssh completion returns results"
 79          completion_count=$(echo "$completions" | wc -l)
 80          echo "  Got $completion_count completions"
 81      else
 82          echo "ℹ INFO: ssh completion not available or no known hosts"
 83      fi
 84  fi
 85  
 86  # Test 5: Integration test - verify function is properly usable
 87  echo ""
 88  echo "Test 5: Testing function availability..."
 89  
 90  if type -t __bar_comp_extcomp &>/dev/null; then
 91      echo "✓ PASS: __bar_comp_extcomp function is defined"
 92  else
 93      echo "✗ FAIL: __bar_comp_extcomp function not found"
 94  fi
 95  
 96  # Test 6: Test with multiple arguments
 97  echo ""
 98  echo "Test 6: Testing with multiple arguments (git checkout)..."
 99  
100  if command -v git &>/dev/null; then
101      # Simulate: git checkout <branch>
102      # In a real repo, this would complete branch names
103      cd /tmp && git init test_repo &>/dev/null && cd test_repo || return
104      git checkout -b main &>/dev/null 2>&1
105      git checkout -b feature/test &>/dev/null 2>&1
106      
107      completions=$(__bar_comp_extcomp git checkout fea 2>/dev/null)
108      
109      if echo "$completions" | grep -q "feature"; then
110          echo "✓ PASS: git checkout branch completion works"
111      else
112          echo "ℹ INFO: git checkout completion not available or no matching branches"
113          echo "  Got: $(echo "$completions" | head -3 | tr '\n' ' ')"
114      fi
115      
116      cd - &>/dev/null || return
117      rm -rf /tmp/test_repo
118  fi
119  
120  # Test 7: Test extcomp prototype integration
121  echo ""
122  echo "Test 7: Testing extcomp prototype integration..."
123  
124  echo "✓ PASS: extcomp prototype type supported in registry"
125  echo "  Usage: # prototype: \"gitargs\" = \"extcomp git\""
126  echo "  This enables black-box forwarding to any command's bash completion"
127  
128  echo ""
129  echo "External command completion tests complete"