|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd) |
| 5 | +script="$repo_root/sandboxes/code-interpreter/scripts/code-interpreter.sh" |
| 6 | +sandbox=$(mktemp -d) |
| 7 | +trap 'rm -rf "$sandbox"' EXIT |
| 8 | + |
| 9 | +write_tslab_stub() { |
| 10 | + cat >"$sandbox/tslab" <<'STUB' |
| 11 | +#!/usr/bin/env bash |
| 12 | +echo "tslab $*" >>"$CALL_LOG" |
| 13 | +STUB |
| 14 | + chmod +x "$sandbox/tslab" |
| 15 | +} |
| 16 | + |
| 17 | +cat >"$sandbox/npm" <<'STUB' |
| 18 | +#!/usr/bin/env bash |
| 19 | +echo "npm $*" >>"$CALL_LOG" |
| 20 | +if [ "$1 $2 $3" = "install -g tslab" ]; then |
| 21 | + cat >"$TEST_BIN_DIR/tslab" <<'TSLAB' |
| 22 | +#!/usr/bin/env bash |
| 23 | +echo "tslab $*" >>"$CALL_LOG" |
| 24 | +TSLAB |
| 25 | + chmod +x "$TEST_BIN_DIR/tslab" |
| 26 | +fi |
| 27 | +STUB |
| 28 | +write_tslab_stub |
| 29 | +cat >"$sandbox/jupyter" <<'STUB' |
| 30 | +#!/usr/bin/env bash |
| 31 | +if [ "$1" = "kernelspec" ] && [ "$2" = "list" ]; then |
| 32 | + cat "$JUPYTER_KERNELS" |
| 33 | +fi |
| 34 | +STUB |
| 35 | +chmod +x "$sandbox/npm" "$sandbox/jupyter" |
| 36 | + |
| 37 | +extract_node_setup() { |
| 38 | + awk ' |
| 39 | + /^tslab_kernels_installed\(\)/ {emit=1} |
| 40 | + emit {print} |
| 41 | + /^setup_node\(\)/ {in_setup=1} |
| 42 | + in_setup && /^}/ {exit} |
| 43 | + ' "$script" |
| 44 | +} |
| 45 | + |
| 46 | +run_setup_node() { |
| 47 | + CALL_LOG=$1 JUPYTER_KERNELS=$2 TEST_BIN_DIR="$sandbox" PATH="$sandbox:$PATH" bash -c "$(extract_node_setup); setup_node" |
| 48 | +} |
| 49 | + |
| 50 | +call_log="$sandbox/calls-installed.log" |
| 51 | +kernels="$sandbox/kernels-installed.txt" |
| 52 | +cat >"$kernels" <<'KERNELS' |
| 53 | +Available kernels: |
| 54 | + python /usr/local/share/jupyter/kernels/python |
| 55 | + tslab /usr/local/share/jupyter/kernels/tslab |
| 56 | + jslab /usr/local/share/jupyter/kernels/jslab |
| 57 | +KERNELS |
| 58 | +run_setup_node "$call_log" "$kernels" |
| 59 | +if [ -s "$call_log" ]; then |
| 60 | + echo "expected no npm or tslab calls when kernels are already installed" >&2 |
| 61 | + cat "$call_log" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +call_log="$sandbox/calls-missing-kernels.log" |
| 66 | +kernels="$sandbox/kernels-missing.txt" |
| 67 | +cat >"$kernels" <<'KERNELS' |
| 68 | +Available kernels: |
| 69 | + python /usr/local/share/jupyter/kernels/python |
| 70 | +KERNELS |
| 71 | +run_setup_node "$call_log" "$kernels" |
| 72 | +if grep -q '^npm install -g tslab$' "$call_log"; then |
| 73 | + echo "did not expect npm install when tslab command exists" >&2 |
| 74 | + cat "$call_log" >&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | +if ! grep -q '^tslab install$' "$call_log"; then |
| 78 | + echo "expected tslab install when kernels are missing" >&2 |
| 79 | + cat "$call_log" >&2 |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +call_log="$sandbox/calls-partial-kernels.log" |
| 84 | +kernels="$sandbox/kernels-partial.txt" |
| 85 | +cat >"$kernels" <<'KERNELS' |
| 86 | +Available kernels: |
| 87 | + python /usr/local/share/jupyter/kernels/python |
| 88 | + tslab /usr/local/share/jupyter/kernels/tslab |
| 89 | +KERNELS |
| 90 | +run_setup_node "$call_log" "$kernels" |
| 91 | +if grep -q '^npm install -g tslab$' "$call_log"; then |
| 92 | + echo "did not expect npm install when tslab command exists" >&2 |
| 93 | + cat "$call_log" >&2 |
| 94 | + exit 1 |
| 95 | +fi |
| 96 | +if ! grep -q '^tslab install$' "$call_log"; then |
| 97 | + echo "expected tslab install when one kernel is missing" >&2 |
| 98 | + cat "$call_log" >&2 |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +rm "$sandbox/tslab" |
| 103 | +call_log="$sandbox/calls-missing-command.log" |
| 104 | +run_setup_node "$call_log" "$kernels" |
| 105 | +if ! grep -q '^npm install -g tslab$' "$call_log"; then |
| 106 | + echo "expected npm install when tslab command is missing" >&2 |
| 107 | + cat "$call_log" >&2 |
| 108 | + exit 1 |
| 109 | +fi |
0 commit comments