/ tests / test_cargo_completers.sh
test_cargo_completers.sh
  1  #!/bin/bash
  2  # Test cargo completion functions
  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 cargo completion functions..."
 11  
 12  # Load the cargo module
 13  if [[ -f "$REPO_ROOT/Bar.d/cargo" ]]; then
 14      source "$REPO_ROOT/Bar.d/cargo"
 15  else
 16      echo "✗ FAIL: Cannot find $REPO_ROOT/Bar.d/cargo module"
 17      exit 1
 18  fi
 19  
 20  # Test 1: cargo_tool_complete
 21  echo ""
 22  echo "Test 1: Testing cargo_tool_complete..."
 23  if command -v cargo &>/dev/null; then
 24      tools=$(cargo_tool_complete)
 25      tool_count=$(echo "$tools" | wc -l)
 26      
 27      if [[ $tool_count -gt 10 ]]; then
 28          echo "✓ PASS: cargo_tool_complete returned $tool_count tools"
 29          echo "  Sample tools: $(echo "$tools" | head -5 | tr '\n' ' ')"
 30      else
 31          echo "✗ FAIL: Expected more than 10 tools, got $tool_count"
 32      fi
 33      
 34      # Check for common tools
 35      if echo "$tools" | grep -q "build"; then
 36          echo "✓ PASS: Found 'build' in tool list"
 37      else
 38          echo "✗ FAIL: 'build' not found in tool list"
 39      fi
 40  else
 41      echo "ℹ INFO: cargo not installed, skipping cargo_tool_complete test"
 42  fi
 43  
 44  # Test 2: cargo toolchain completion via extcomp
 45  echo ""
 46  echo "Test 2: Testing cargo toolchain completion via extcomp..."
 47  if command -v cargo &>/dev/null; then
 48      toolchain_completions=$(__bar_comp_extcomp cargo "+")
 49  
 50      if [[ -n "$toolchain_completions" ]]; then
 51          echo "✓ PASS: __bar_comp_extcomp returned toolchain suggestions"
 52          echo "  Sample: $(echo "$toolchain_completions" | head -3 | tr '\n' ' ')"
 53      else
 54          echo "ℹ INFO: No toolchain suggestions returned (cargo completion may be unavailable)"
 55      fi
 56  else
 57      echo "ℹ INFO: cargo not installed, skipping toolchain completion test"
 58  fi
 59  
 60  # Test 3: __bar_comp_extcomp with cargo
 61  echo ""
 62  echo "Test 3: Testing __bar_comp_extcomp with cargo (black box forwarding)..."
 63  if command -v rustc &>/dev/null; then
 64      # Test completing cargo subcommands
 65      completions=$(__bar_comp_extcomp cargo "bui")
 66      
 67      if echo "$completions" | grep -q "build"; then
 68          echo "✓ PASS: __bar_comp_extcomp finds 'build' from 'bui' prefix"
 69      else
 70          echo "ℹ INFO: __bar_comp_extcomp fallback mode (no native completion)"
 71          echo "  Got: $(echo "$completions" | head -3 | tr '\n' ' ')"
 72      fi
 73      
 74      # Test with no prefix
 75      all_completions=$(__bar_comp_extcomp cargo "")
 76      completion_count=$(echo "$all_completions" | wc -l)
 77      
 78      if [[ $completion_count -gt 20 ]]; then
 79          echo "✓ PASS: __bar_comp_extcomp returns many completions ($completion_count)"
 80          echo "  Sample: $(echo "$all_completions" | head -5 | tr '\n' ' ')"
 81      else
 82          echo "ℹ INFO: __bar_comp_extcomp using fallback mode"
 83      fi
 84  else
 85      echo "ℹ INFO: rustc not installed, skipping __bar_comp_extcomp test"
 86  fi
 87  
 88  # Test 4: Prototype definitions
 89  echo ""
 90  echo "Test 4: Testing prototype definitions..."
 91  __bar_init_completion_registry
 92  
 93  # Parse cargo module
 94  __bar_parse_file --module cargo "$REPO_ROOT/Bar.d/cargo"
 95  
 96  # Check if toolchain prototype is registered
 97  if [[ -v __bar_protoregistry["cargo@toolchain"] ]]; then
 98      echo "✓ PASS: toolchain prototype registered for cargo module"
 99      echo "  Spec: ${__bar_protoregistry[cargo@toolchain]}"
100  else
101      echo "✗ FAIL: toolchain prototype not registered"
102  fi
103  
104  # Check if tool prototype is registered
105  if [[ -v __bar_protoregistry["cargo@tool"] ]]; then
106      echo "✓ PASS: tool prototype registered for cargo module"
107      echo "  Spec: ${__bar_protoregistry[cargo@tool]}"
108  else
109      echo "✗ FAIL: tool prototype not registered"
110  fi
111  
112  # Test 5: Completer expansion
113  echo ""
114  echo "Test 5: Testing completer expansion..."
115  toolchain_completer=$(__bar_get_completer "" "cargo@toolchain")
116  if [[ "$toolchain_completer" == "__bar_comp_extcomp cargo" ]]; then
117      echo "✓ PASS: toolchain completer expands correctly"
118  else
119      echo "✗ FAIL: toolchain completer expansion incorrect"
120      echo "  Expected: __bar_comp_extcomp cargo"
121      echo "  Got: $toolchain_completer"
122  fi
123  
124  tool_completer=$(__bar_get_completer "" "cargo@tool")
125  if [[ "$tool_completer" == "__bar_comp_ext cargo_tool_complete" ]]; then
126      echo "✓ PASS: tool completer expands correctly"
127  else
128      echo "✗ FAIL: tool completer expansion incorrect"
129      echo "  Expected: __bar_comp_ext cargo_tool_complete"
130      echo "  Got: $tool_completer"
131  fi
132  
133  echo ""
134  echo "Cargo completer tests complete"