/ tests / test_is_cargo_tool_completion.sh
test_is_cargo_tool_completion.sh
  1  #!/bin/bash
  2  
  3  # Test completion for is_cargo_tool_installed
  4  #
  5  # This test verifies that:
  6  # 1. Functions with literal punctuation in params (like [+toolchain]) are properly tracked
  7  # 2. The literal prefix "+" is extracted correctly  
  8  # 3. Prototypes are registered and can be looked up
  9  
 10  cd "$(dirname "$0")/.." || exit 1
 11  
 12  echo "=== Test 1: Source completion and parse cargo module ==="
 13  source contrib/bar_complete
 14  __bar_parse_file Bar.d/cargo
 15  echo "✓ Completion script sourced and cargo module parsed"
 16  
 17  echo ""
 18  echo "=== Test 2: Verify function is tracked ==="
 19  if [[ " ${__bar_functions[*]} " == *" is_cargo_tool_installed "* ]]; then
 20      echo "✓ is_cargo_tool_installed is tracked"
 21  else
 22      echo "✗ is_cargo_tool_installed is NOT tracked"
 23      exit 1
 24  fi
 25  
 26  echo ""
 27  echo "=== Test 3: Verify params are stored correctly ==="
 28  params="${__bar_func_params[is_cargo_tool_installed]}"
 29  expected="[+toolchain] <tool> [args..]"
 30  if [[ "$params" == "$expected" ]]; then
 31      echo "✓ Params stored correctly: '$params'"
 32  else
 33      echo "✗ Params incorrect. Expected: '$expected', Got: '$params'"
 34      exit 1
 35  fi
 36  
 37  echo ""
 38  echo "=== Test 4: Verify prototypes are registered for cargo module ==="
 39  if [[ -v __bar_protoregistry[cargo@toolchain] ]]; then
 40      echo "✓ cargo@toolchain prototype registered: ${__bar_protoregistry[cargo@toolchain]}"
 41  else
 42      echo "✗ cargo@toolchain prototype NOT registered"
 43      exit 1
 44  fi
 45  
 46  if [[ -v __bar_protoregistry[cargo@tool] ]]; then
 47      echo "✓ cargo@tool prototype registered: ${__bar_protoregistry[cargo@tool]}"
 48  else
 49      echo "✗ cargo@tool prototype NOT registered"
 50      exit 1
 51  fi
 52  
 53  echo ""
 54  echo "=== Test 5: Verify module tracking ==="
 55  if [[ "${__bar_func_module[is_cargo_tool_installed]}" == "cargo" ]]; then
 56      echo "✓ Module tracked correctly: cargo"
 57  else
 58      echo "✗ Module not tracked. Got: '${__bar_func_module[is_cargo_tool_installed]}'"
 59      exit 1
 60  fi
 61  
 62  echo ""
 63  echo "=== Test 6: Verify literal punctuation extraction ==="
 64  mapfile -t punct_parts < <(_bar_extract_literal_punct "+toolchain")
 65  proto_clean="${punct_parts[0]}"
 66  literal_prefix="${punct_parts[1]}"
 67  literal_suffix="${punct_parts[2]}"
 68  
 69  if [[ "$proto_clean" == "toolchain" && "$literal_prefix" == "+" && "$literal_suffix" == "" ]]; then
 70      echo "✓ Literal punctuation extracted correctly"
 71      echo "  Clean proto: '$proto_clean'"
 72      echo "  Literal prefix: '$literal_prefix'"
 73  else
 74      echo "✗ Literal punctuation extraction failed"
 75      echo "  Clean proto: '$proto_clean' (expected 'toolchain')"
 76      echo "  Literal prefix: '$literal_prefix' (expected '+')"
 77      exit 1
 78  fi
 79  
 80  echo ""
 81  echo "=== Test 7: Verify completer lookup ==="
 82  completer=$(__bar_get_completer "is_cargo_tool_installed" "toolchain")
 83  expected_completer="__bar_comp_extcomp cargo"
 84  if [[ "$completer" == "$expected_completer" ]]; then
 85      echo "✓ Completer lookup successful: '$completer'"
 86  else
 87      echo "✗ Completer lookup failed. Expected: '$expected_completer', Got: '$completer'"
 88      exit 1
 89  fi
 90  
 91  echo ""
 92  echo "=== Test 8: Verify end-to-end completion ==="
 93  # Stub the external completer so we do not depend on cargo's native completion results.
 94  __bar_comp_extcomp()
 95  {
 96      local command_name="$1"
 97      local cur="$2"
 98  
 99      if [[ "$command_name" == "cargo" ]]; then
100          for item in +stable +nightly; do
101              if [[ -z "$cur" || "$item" == "$cur"* ]]; then
102                  echo "$item"
103              fi
104          done
105          return 0
106      fi
107  
108      return 1
109  }
110  
111  COMP_WORDS=("./bar" "is_cargo_toolchain_available" "")
112  COMP_CWORD=2
113  COMPREPLY=()
114  _bar_complete
115  
116  if [[ " ${COMPREPLY[*]} " == *" +stable "* && " ${COMPREPLY[*]} " == *" +nightly "* ]]; then
117      echo "✓ Completion returns toolchains via _bar_complete"
118  else
119      echo "✗ Completion did not return expected toolchains"
120      echo "  Got: ${COMPREPLY[*]}"
121      exit 1
122  fi
123  
124  echo ""
125  echo "=== Test 9: Completion after selecting toolchain ==="
126  __bar_comp_ext()
127  {
128      local completer_name="$1"
129      local cur="$2"
130  
131      if [[ "$completer_name" == "cargo_tool_complete" ]]; then
132          for item in fmt clippy; do
133              if [[ -z "$cur" || "$item" == "$cur"* ]]; then
134                  echo "$item"
135              fi
136          done
137          return 0
138      fi
139  
140      return 1
141  }
142  
143  COMPREPLY=()
144  COMP_WORDS=("./bar" "is_cargo_tool_installed" "+nightly" "")
145  COMP_CWORD=3
146  
147  _bar_complete
148  
149  if [[ " ${COMPREPLY[*]} " == *" fmt "* && " ${COMPREPLY[*]} " == *" clippy "* ]]; then
150      echo "✓ Completion offers cargo tools after toolchain"
151  else
152      echo "✗ Completion should offer cargo tools after toolchain"
153      echo "  Got: ${COMPREPLY[*]}"
154      exit 1
155  fi
156  
157  echo ""
158  echo "=== All structural tests passed! ==="
159  echo ""
160  echo "Note: To test actual completion behavior interactively:"
161  echo "  1. Source contrib/bar_complete in your bash shell"
162  echo "  2. Enable bash completion for bar"
163  echo "  3. Try: bar is_cargo_tool_installed <TAB>"
164  echo "  4. You should see toolchain options like +stable, +nightly, etc."
165  echo "  5. After selecting a toolchain, <TAB> again should show cargo tools"