Skip to content

Commit d72e326

Browse files
committed
Test viz in CI
1 parent d4b18cd commit d72e326

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

.github/workflows/test_viz.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Test viz
2+
3+
on:
4+
push:
5+
paths:
6+
- 'examples/**/app.py' # If an example visualisation is modified
7+
- 'examples/**/viz.py' # If an example visualisation is modified
8+
- 'viz.py' # If the test script is modified
9+
- '.github/workflows/test_viz.yml' # If this workflow is modified
10+
pull_request:
11+
paths:
12+
- 'examples/**/*.py'
13+
- 'test_examples.py'
14+
- '.github/workflows/test_examples.yml'
15+
workflow_dispatch:
16+
schedule:
17+
- cron: '0 6 * * 1' # Monday at 6:00 UTC
18+
19+
jobs:
20+
# build-stable:
21+
# runs-on: ubuntu-latest
22+
# steps:
23+
# - uses: actions/checkout@v4
24+
# - name: Set up Python
25+
# uses: actions/setup-python@v5
26+
# with:
27+
# python-version: "3.12"
28+
# - name: Install dependencies
29+
# run: pip install mesa pytest
30+
# - name: Test with pytest
31+
# run: pytest -rA -Werror test_examples.py
32+
33+
build-pre:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
- name: Set up Python
38+
uses: actions/setup-python@v5
39+
with:
40+
python-version: "3.12"
41+
- name: Install dependencies
42+
run: |
43+
pip install --pre mesa[viz]
44+
pip install .[test]
45+
- name: Test with pytest
46+
run: pytest -rA -Werror -Wdefault::FutureWarning viz.py
47+
48+
# build-main:
49+
# runs-on: ubuntu-latest
50+
# steps:
51+
# - uses: actions/checkout@v4
52+
# - name: Set up Python
53+
# uses: actions/setup-python@v5
54+
# with:
55+
# python-version: "3.12"
56+
# - name: Install dependencies
57+
# run: |
58+
# pip install .[test]
59+
# pip install -U git+https://github.com/projectmesa/mesa@main#egg=mesa
60+
# - name: Test with pytest
61+
# run: pytest -rA -Werror -Wdefault::FutureWarning test_examples.py

viz.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import subprocess
3+
import time
4+
import pytest
5+
6+
7+
def find_viz_files(directory):
8+
viz_files = []
9+
for root, _, files in os.walk(directory):
10+
for file in files:
11+
if file in ['app.py', 'viz.py']:
12+
viz_files.append(os.path.join(root, file))
13+
return viz_files
14+
15+
16+
def run_solara_app(file_path):
17+
process = subprocess.Popen(['solara', 'run', file_path])
18+
time.sleep(5) # Wait for the app to start
19+
return process
20+
21+
22+
def send_step_command(times=1):
23+
for _ in range(times):
24+
subprocess.run(['xdotool', 'key', 'space'])
25+
time.sleep(0.5)
26+
27+
28+
def send_pause_command():
29+
subprocess.run(['xdotool', 'key', 'p'])
30+
time.sleep(0.5)
31+
32+
33+
def send_reset_command():
34+
subprocess.run(['xdotool', 'key', 'r'])
35+
time.sleep(0.5)
36+
37+
38+
@pytest.mark.parametrize('viz_file', find_viz_files('examples'))
39+
def test_viz_app(viz_file):
40+
process = run_solara_app(viz_file)
41+
42+
try:
43+
# Do 3 steps
44+
send_step_command(3)
45+
46+
# Pause
47+
send_pause_command()
48+
49+
# 3 steps
50+
send_step_command(3)
51+
52+
# Reset
53+
send_reset_command()
54+
55+
# Run steps for 5 seconds
56+
start_time = time.time()
57+
while time.time() - start_time < 5:
58+
send_step_command()
59+
60+
# Pause
61+
send_pause_command()
62+
63+
finally:
64+
# Clean up: terminate the Solara app process
65+
process.terminate()
66+
process.wait()
67+
68+
69+
if __name__ == '__main__':
70+
pytest.main([__file__])

0 commit comments

Comments
 (0)