Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 67 additions & 63 deletions .github/workflows/test_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ name: Test and Release

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
python-version: "3.9"
version: "0.11.24"
enable-cache: true
- name: Install the project
run: uv sync --all-extras --dev

# Install and run pre-commit
- run: |
pip install pre-commit
pre-commit install
pre-commit run --all-files
uv run pre-commit install
uv run pre-commit install --hook-type commit-msg
uv run pre-commit run --all-files

pytest:
name: Pytest ${{ matrix.config.name }}
Expand All @@ -30,10 +33,7 @@ jobs:
matrix:
python-version: ["3.10"]
config:
- {
name: "Linux",
os: ubuntu-latest
}
- { name: "Linux", os: ubuntu-latest }
# - {
# name: "MacOSX",
# os: macos-latest
Expand All @@ -48,57 +48,65 @@ jobs:
shell: bash

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install OSMesa for Linux
if: matrix.config.os == 'ubuntu-latest'
run: |
sudo apt update
sudo apt-get install -y libosmesa6-dev

- name: Install dependencies
run: |
pip install .[vtk]
pip install -r tests/requirements.txt
pip install coverage
playwright install

- name: Run Tests
run: |
# Run the tests with coverage so we get a coverage report too
coverage run --source . -m pytest ./tests
# Print the coverage report
coverage report -m

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3

- name: Upload pytest test results
uses: actions/upload-artifact@v4
if: always()
with:
name: pytest-results-${{ matrix.config.name }}
path: |
tests/refs/*.yml
tests/refs/**/*.png
assets/**
retention-days: 1
- name: Checkout
uses: actions/checkout@v6

- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.11.24"
enable-cache: true

- name: Set Up Node
uses: actions/setup-node@v6
with:
node-version: 24

- name: Build Vue Components
run: |
cd vue-components
npm i
npm run build

- name: Install the project
run: uv sync --all-extras --dev

- name: Install OSMesa for Linux
if: matrix.config.os == 'ubuntu-latest'
run: |
sudo apt update
sudo apt-get install -y libosmesa6-dev

- name: Install dependencies
run: |
uv sync --all-extras --dev
source .venv/bin/activate
playwright install
pytest -s ./tests --cov=src --cov-report=xml

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3

- name: Upload pytest test results
uses: actions/upload-artifact@v4
if: always()
with:
name: trame-vtklocal-tests-${{ matrix.config.name }}
path: |
tests/results/**/*.png
tests/results/**/*.txt

retention-days: 1

test-npm-build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6

- name: Set Up Node
uses: actions/setup-node@v4
uses: actions/setup-node@v6
with:
node-version: 24

Expand All @@ -109,11 +117,7 @@ jobs:
npm run build

release:
needs: [
pre-commit,
pytest,
test-npm-build,
]
needs: [pre-commit, pytest, test-npm-build]
runs-on: ubuntu-latest
if: github.event_name == 'push'
environment:
Expand All @@ -125,12 +129,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0

- name: Set Up Node
uses: actions/setup-node@v4
uses: actions/setup-node@v6
with:
node-version: 24

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ dev = [
"ruff",
"pytest",
"pytest-asyncio",
"pytest-cov >=3",
"coverage",
"pytest-playwright",
"trame-client[test]",
]

Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
results
Binary file added tests/assets/cone/00_startup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/00_startup_github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/01_update_resolution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/01_update_resolution_github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/02_unmount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/02_unmount_github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/03_remount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cone/03_remount_github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 117 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,116 @@
import pytest
from pathlib import Path
from trame_client.utils.testing import FixtureHelper

import pytest
import vtk
from PIL import Image
from pixelmatch.contrib.PIL import pixelmatch
from trame.app import TrameApp
from trame.decorators import change
from trame.ui.html import DivLayout
from trame_client.utils.testing import FixtureHelper, enable_testing

from trame.widgets import client, html, vtklocal
from trame_vtklocal.utils import ui

ROOT_PATH = Path(__file__).parent.parent.absolute()
HELPER = FixtureHelper(ROOT_PATH)


class Utils:
@staticmethod
async def compare_screenshot(page, baseline_image, result_directory, threshold=0.1):
test_image = result_directory / baseline_image.with_suffix(".png").name
await page.screenshot(path=test_image)

img_test = Image.open(test_image)
img_diff = Image.new("RGBA", img_test.size)
mismatches = []

for ref_file in baseline_image.parent.glob(f"{baseline_image.name}*.png"):
img_ref = Image.open(ref_file)

file_diff = (test_image.parent / ref_file.name).with_suffix(".diff.png")
mismatch = pixelmatch(img_ref, img_test, img_diff, threshold=threshold)
img_diff.save(file_diff)
file_diff.with_suffix(".txt").write_text(f"{mismatch}")
mismatches.append(mismatch)

return min(mismatches) < threshold


def create_vtk_pipeline():
renderer = vtk.vtkRenderer()
rw = vtk.vtkRenderWindow()
rw.AddRenderer(renderer)
rwi = vtk.vtkRenderWindowInteractor(render_window=rw)
rwi.interactor_style.SetCurrentStyleToTrackballCamera()

cone = vtk.vtkConeSource()

mapper = vtk.vtkPolyDataMapper(input_connection=cone.output_port)
actor = vtk.vtkActor(mapper=mapper)

renderer.AddActor(actor)
renderer.background = (0.1, 0.2, 0.4)
renderer.ResetCamera()

return rw, cone


class Cone(TrameApp):
def __init__(self, server=None, mode="wasm32", exec="sync", rendering="webgl"):
super().__init__(server)
self.state.wasm_conf = {
"mode": mode,
"exec": exec,
"rendering": rendering,
}
enable_testing(self.server, "local_rendering_ready")
self.render_window, self.cone = create_vtk_pipeline()
self._build_ui()

@property
def resolution(self):
return self.state.resolution

@resolution.setter
def resolution(self, v):
with self.state:
self.state.resolution = int(v)

@change("resolution")
def _on_resolution(self, resolution, **_):
self.cone.resolution = int(resolution)
self.ctx.view.update()

@property
def mounted(self):
return self.state.mounted

@mounted.setter
def mounted(self, v):
with self.state:
self.state.mounted = bool(v)

def _build_ui(self):
self.state.local_rendering_ready = 0
with DivLayout(self.server) as self.ui:
html.Div("{{ local_rendering_ready }}", classes="readyCount")
client.Style(
"body { margin: 0; } .readyCount { z-index: 10; position: absolute; left: 0; top: 0; }"
)
with html.Div(
style=ui.FULL_SCREEN,
v_if=("mounted", True),
):
vtklocal.LocalView(
self.render_window,
ctx_name="view",
config=["wasm_conf"],
updated="local_rendering_ready++",
)


@pytest.fixture
def ref_dir() -> Path:
return Path(__file__).parent / "refs"
Expand All @@ -21,3 +126,13 @@ def server(xprocess, server_path):

# clean up whole process tree afterwards
xprocess.getinfo(name).terminate()


@pytest.fixture
def utils():
return Utils


@pytest.fixture
def ConeApp():
return Cone
4 changes: 0 additions & 4 deletions tests/requirements.txt

This file was deleted.

Loading
Loading