Skip to content

Commit

Permalink
Use Path objects in tests instead of py.path or plain filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Jan 4, 2024
1 parent 9113cee commit b4fec7c
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 277 deletions.
19 changes: 7 additions & 12 deletions tests/draw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Test the final, drawn results and compare PNG images pixel per pixel."""

import io
import os
from itertools import zip_longest
from pathlib import Path

from PIL import Image

from ..testing_utils import FakeHTML, resource_filename
from ..testing_utils import FakeHTML, resource_path

# NOTE: "r" is not half red on purpose. In the pixel strings it has
# better contrast with "B" than does "R". eg. "rBBBrrBrB" vs "RBBBRRBRB".
Expand Down Expand Up @@ -106,15 +106,13 @@ def assert_pixels_equal(name, width, height, raw, expected_raw, tolerance=0):
f'expected rgba{expected}, got rgba{value}')


def write_png(basename, pixels, width, height): # pragma: no cover
def write_png(name, pixels, width, height): # pragma: no cover
"""Take a pixel matrix and write a PNG file."""
directory = os.path.join(os.path.dirname(__file__), 'results')
if not os.path.isdir(directory):
os.mkdir(directory)
filename = os.path.join(directory, f'{basename}.png')
directory = Path(__file__).parent / 'results'
directory.mkdir(exist_ok=True)
image = Image.new('RGB', (width, height))
image.putdata(pixels)
image.save(filename)
image.save(directory / f'{name}.png')


def html_to_pixels(html):
Expand All @@ -123,10 +121,7 @@ def html_to_pixels(html):
Also return the document to aid debugging.
"""
document = FakeHTML(
string=html,
# Dummy filename, but in the right directory.
base_url=resource_filename('<test>'))
document = FakeHTML(string=html, base_url=resource_path('<dummy>'))
return document_to_pixels(document)


Expand Down
4 changes: 2 additions & 2 deletions tests/draw/svg/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from weasyprint.urls import path2url

from ...testing_utils import assert_no_logs, resource_filename
from ...testing_utils import assert_no_logs, resource_path


@assert_no_logs
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_image_image(assert_pixels):
<svg width="4px" height="4px" xmlns="http://www.w3.org/2000/svg">
<image xlink:href="%s" />
</svg>
''' % path2url(resource_filename('pattern.png')))
''' % path2url(resource_path('pattern.png')))


def test_image_image_wrong(assert_pixels):
Expand Down
4 changes: 2 additions & 2 deletions tests/draw/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from ..testing_utils import (
FakeHTML, assert_no_logs, capture_logs, resource_filename)
FakeHTML, assert_no_logs, capture_logs, resource_path)

centered_image = '''
________
Expand Down Expand Up @@ -589,5 +589,5 @@ def test_image_exif_image_orientation_keep_format():
<style>@page { size: 10px }</style>
<img style="display: block; image-orientation: 180deg"
src="not-optimized-exif.jpg">''',
base_url=resource_filename('<inline HTML>')).write_pdf()
base_url=resource_path('<inline HTML>')).write_pdf()
assert b'DCTDecode' in pdf
4 changes: 2 additions & 2 deletions tests/test_acid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from weasyprint import HTML

from .draw import assert_pixels_equal
from .testing_utils import assert_no_logs, capture_logs, resource_filename
from .testing_utils import assert_no_logs, capture_logs, resource_path


@pytest.mark.xfail
@assert_no_logs
def test_acid2():
# TODO: fails because of Ghostscript rendering
def render(filename):
return HTML(resource_filename(filename)).render()
return HTML(resource_path(filename)).render()

with capture_logs():
# This is a copy of https://www.webstandards.org/files/acid2/test.html
Expand Down
Loading

0 comments on commit b4fec7c

Please sign in to comment.