| 1 | #!/bin/bash |
| 2 | # Copyright 2026 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | set -e |
| 17 | |
| 18 | # Integration Test: Plot Redirection |
| 19 | # Verifies that 'colab exec --output-image' correctly intercepts and saves plots. |
| 20 | |
| 21 | SESSION_NAME="repro-plot-$(date +%s)" |
| 22 | OUTPUT_FILE="intercepted_plot.png" |
| 23 | SCRIPT_FILE="plot_gen.py" |
| 24 | |
| 25 | # Cleanup on exit |
| 26 | cleanup() { |
| 27 | echo "[*] Cleaning up..." |
| 28 | colab stop -s "$SESSION_NAME" || true |
| 29 | rm -f "$SCRIPT_FILE" "$OUTPUT_FILE" |
| 30 | } |
| 31 | trap cleanup EXIT |
| 32 | |
| 33 | echo "[*] Creating script..." |
| 34 | cat <<EOF > "$SCRIPT_FILE" |
| 35 | import matplotlib.pyplot as plt |
| 36 | import numpy as np |
| 37 | |
| 38 | x = np.linspace(0, 10, 100) |
| 39 | y = np.sin(x) |
| 40 | |
| 41 | plt.figure(figsize=(8, 4)) |
| 42 | plt.plot(x, y) |
| 43 | plt.title("Repro Plot") |
| 44 | plt.show() |
| 45 | EOF |
| 46 | |
| 47 | echo "[*] Starting session..." |
| 48 | colab new -s "$SESSION_NAME" |
| 49 | |
| 50 | echo "[*] Running execution with plot redirection..." |
| 51 | # Note: On a fresh VM, this may trigger the retry logic. |
| 52 | colab exec -s "$SESSION_NAME" -f "$SCRIPT_FILE" --output-image "$OUTPUT_FILE" |
| 53 | |
| 54 | if [ -f "$OUTPUT_FILE" ]; then |
| 55 | echo "[SUCCESS] Plot intercepted and saved to $OUTPUT_FILE" |
| 56 | ls -l "$OUTPUT_FILE" |
| 57 | else |
| 58 | echo "[FAILURE] Plot file not found!" |
| 59 | exit 1 |
| 60 | fi |