launcher-detection.test.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env bash
  2. # Tests for bin/qmd runtime detection logic.
  3. # Simulates lockfile combinations in a temp directory and verifies which
  4. # runtime the launcher would choose.
  5. #
  6. # Usage: bash test/launcher-detection.test.sh
  7. set -euo pipefail
  8. PASS=0
  9. FAIL=0
  10. TMPDIR_BASE=$(mktemp -d)
  11. cleanup() { rm -rf "$TMPDIR_BASE"; }
  12. trap cleanup EXIT
  13. ok() { printf " %-60s OK\n" "$1"; PASS=$((PASS + 1)); }
  14. fail() { printf " %-60s FAIL\n" "$1 (got: $2, expected: $3)"; FAIL=$((FAIL + 1)); }
  15. # Extract the detection logic from bin/qmd into a testable function.
  16. # Instead of exec-ing a runtime, we echo which one would be chosen.
  17. detect_runtime() {
  18. local DIR="$1"
  19. if [ -f "$DIR/package-lock.json" ]; then
  20. echo "node"
  21. elif [ -f "$DIR/bun.lock" ] || [ -f "$DIR/bun.lockb" ]; then
  22. echo "bun"
  23. else
  24. echo "node"
  25. fi
  26. }
  27. # Verify detect_runtime matches the actual bin/qmd logic
  28. assert_runtime() {
  29. local label="$1" dir="$2" expected="$3"
  30. local got
  31. got=$(detect_runtime "$dir")
  32. if [[ "$got" == "$expected" ]]; then
  33. ok "$label"
  34. else
  35. fail "$label" "$got" "$expected"
  36. fi
  37. }
  38. echo "=== bin/qmd runtime detection tests ==="
  39. # --- Test cases ---
  40. # 1. No lockfiles → default to node
  41. d="$TMPDIR_BASE/no-lockfiles"
  42. mkdir -p "$d"
  43. assert_runtime "no lockfiles → node" "$d" "node"
  44. # 2. Only bun.lock → bun
  45. d="$TMPDIR_BASE/bun-lock-only"
  46. mkdir -p "$d"
  47. touch "$d/bun.lock"
  48. assert_runtime "bun.lock only → bun" "$d" "bun"
  49. # 3. Only bun.lockb → bun
  50. d="$TMPDIR_BASE/bun-lockb-only"
  51. mkdir -p "$d"
  52. touch "$d/bun.lockb"
  53. assert_runtime "bun.lockb only → bun" "$d" "bun"
  54. # 4. Only package-lock.json → node
  55. d="$TMPDIR_BASE/npm-only"
  56. mkdir -p "$d"
  57. touch "$d/package-lock.json"
  58. assert_runtime "package-lock.json only → node" "$d" "node"
  59. # 5. Both package-lock.json AND bun.lock → node (npm takes priority)
  60. # This is the key fix for #381: source checkouts have bun.lock committed,
  61. # and contributors who run npm install also create package-lock.json.
  62. d="$TMPDIR_BASE/both-lockfiles"
  63. mkdir -p "$d"
  64. touch "$d/package-lock.json"
  65. touch "$d/bun.lock"
  66. assert_runtime "package-lock.json + bun.lock → node (npm priority)" "$d" "node"
  67. # 6. Both package-lock.json AND bun.lockb → node (npm takes priority)
  68. d="$TMPDIR_BASE/both-lockfiles-b"
  69. mkdir -p "$d"
  70. touch "$d/package-lock.json"
  71. touch "$d/bun.lockb"
  72. assert_runtime "package-lock.json + bun.lockb → node (npm priority)" "$d" "node"
  73. # 7. All three lockfiles → node (npm takes priority)
  74. d="$TMPDIR_BASE/all-lockfiles"
  75. mkdir -p "$d"
  76. touch "$d/package-lock.json"
  77. touch "$d/bun.lock"
  78. touch "$d/bun.lockb"
  79. assert_runtime "all three lockfiles → node (npm priority)" "$d" "node"
  80. echo ""
  81. echo "=== Results: $PASS passed, $FAIL failed ==="
  82. [[ $FAIL -eq 0 ]]