/ tests / test_prototype_scoping.sh
test_prototype_scoping.sh
  1  #!/bin/bash
  2  # Test prototype scoping to ensure module prototypes don't override globals
  3  
  4  set -euo pipefail
  5  
  6  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  7  REPO_ROOT="$(dirname "$SCRIPT_DIR")"
  8  
  9  # Source the completion script
 10  # shellcheck source=../contrib/bar_complete
 11  source "$REPO_ROOT/contrib/bar_complete"
 12  
 13  echo "==================================="
 14  echo "Prototype Scoping Tests"
 15  echo "==================================="
 16  echo ""
 17  
 18  # Test 1: Module prototypes should be scoped
 19  echo "Test 1: Module prototypes are scoped to module@prototype"
 20  __bar_init_completion_registry
 21  __bar_parse_file "$REPO_ROOT/Bar.d/help"
 22  
 23  if [[ "${__bar_protoregistry[help@rule]}" == "help" ]]; then
 24      echo "  ✓ PASS: help@rule is registered as 'help'"
 25  else
 26      echo "  ✗ FAIL: help@rule should be 'help' but is: ${__bar_protoregistry[help@rule]}"
 27      exit 1
 28  fi
 29  
 30  if [[ "${__bar_protoregistry[rule]}" == "rule_or_function" ]]; then
 31      echo "  ✓ PASS: Global 'rule' remains 'rule_or_function'"
 32  else
 33      echo "  ✗ FAIL: Global 'rule' should be 'rule_or_function' but is: ${__bar_protoregistry[rule]}"
 34      exit 1
 35  fi
 36  
 37  echo ""
 38  
 39  # Test 2: Non-module files should NOT register global prototypes
 40  echo "Test 2: Prototype definitions in non-module files are ignored"
 41  cat > /tmp/test_barf << 'EOF'
 42  # prototype: "testproto" = "help"
 43  
 44  rule test:
 45      echo "test"
 46  EOF
 47  
 48  # Clear and reinit
 49  unset __bar_protoregistry
 50  declare -gA __bar_protoregistry=()
 51  __bar_init_completion_registry
 52  
 53  # Parse the non-module file (no module context)
 54  __bar_parse_file --public /tmp/test_barf
 55  
 56  if [[ -z "${__bar_protoregistry[testproto]:-}" ]]; then
 57      echo "  ✓ PASS: testproto was not registered globally (correctly ignored)"
 58  else
 59      echo "  ✗ FAIL: testproto should not be registered globally, but is: ${__bar_protoregistry[testproto]}"
 60      rm /tmp/test_barf
 61      exit 1
 62  fi
 63  
 64  rm /tmp/test_barf
 65  echo ""
 66  
 67  # Test 3: Verify help@rule only used in help module context
 68  echo "Test 3: help@rule completer is only used in help module context"
 69  
 70  # Clear registries
 71  unset __bar_protoregistry
 72  unset __bar_func_module
 73  declare -gA __bar_protoregistry=()
 74  declare -gA __bar_func_module=()
 75  
 76  __bar_init_completion_registry
 77  __bar_scan_files "./bar"
 78  
 79  # Get completer for 'rule' without any function context
 80  completer=$(__bar_get_completer "" "rule")
 81  if [[ "$completer" == "__bar_comp_rule_or_function" ]]; then
 82      echo "  ✓ PASS: 'rule' without context uses rule_or_function completer"
 83  else
 84      echo "  ✗ FAIL: 'rule' without context should use rule_or_function but got: $completer"
 85      exit 1
 86  fi
 87  
 88  # Get completer for 'rule' in context of help function
 89  completer=$(__bar_get_completer "help" "rule")
 90  if [[ "$completer" == "__bar_comp_help" ]]; then
 91      echo "  ✓ PASS: 'rule' in help context uses help completer"
 92  else
 93      echo "  ✗ FAIL: 'rule' in help context should use help completer but got: $completer"
 94      exit 1
 95  fi
 96  
 97  # Get completer for 'rule' in context of some other function
 98  __bar_functions+=("other_func")
 99  completer=$(__bar_get_completer "other_func" "rule")
100  if [[ "$completer" == "__bar_comp_rule_or_function" ]]; then
101      echo "  ✓ PASS: 'rule' in other_func context uses rule_or_function completer"
102  else
103      echo "  ✗ FAIL: 'rule' in other_func context should use rule_or_function but got: $completer"
104      exit 1
105  fi
106  
107  echo ""
108  
109  # Test 4: Verify cargo module prototypes are also scoped
110  echo "Test 4: Cargo module prototypes are properly scoped"
111  
112  if [[ "${__bar_protoregistry[cargo@toolchain]}" == "extcomp cargo" ]]; then
113      echo "  ✓ PASS: cargo@toolchain is registered"
114  else
115      echo "  ✗ FAIL: cargo@toolchain should be registered but is: ${__bar_protoregistry[cargo@toolchain]:-NOT SET}"
116      exit 1
117  fi
118  
119  # Global 'toolchain' should not exist
120  if [[ -z "${__bar_protoregistry[toolchain]:-}" ]]; then
121      echo "  ✓ PASS: Global 'toolchain' is not registered (module-scoped only)"
122  else
123      echo "  ✗ FAIL: Global 'toolchain' should not exist but is: ${__bar_protoregistry[toolchain]}"
124      exit 1
125  fi
126  
127  echo ""
128  echo "==================================="
129  echo "All prototype scoping tests passed!"
130  echo "==================================="