Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotzbua committed Apr 2, 2024
1 parent a6948e9 commit 6bb07ca
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.py text eol=lf
*.sh text eol=lf
*.svg text eol=lf
3 changes: 3 additions & 0 deletions .github/workflows/precommit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jobs:
main:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [ "3.8", "3.12" ] # Lowest and highest.
steps:
Expand All @@ -13,4 +14,6 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
cache: "pip" # caching pip dependencies
- run: pip install -r requirements.txt
- run: pip install -r requirements_dev.txt
- uses: pre-commit/[email protected]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ documentation/src/generators.inc
*.ipynb_checkpoints
venv/
.vscode/
tracker.log
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ci:
autofix_prs: true
autoupdate_schedule: quarterly
exclude: '\.svg$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand Down Expand Up @@ -99,3 +100,13 @@ repos:
hooks:
- id: codespell
exclude: ^(boxes|locale|po|static)/ # todo folder boxes

- repo: local
hooks:
- id: pytest
name: pytest
entry: pytest
language: system
types: [ python ]
pass_filenames: false
always_run: true
3 changes: 2 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
inkex
lxml
mypy
pre-commit
pytest>=8.1.1
types-Markdown
22 changes: 5 additions & 17 deletions scripts/boxes
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Options:
-h --help Show this screen.
--list List available generators.
"""
from __future__ import annotations

import gettext
import os
import sys
from pathlib import Path
from typing import Type

try:
import boxes
Expand Down Expand Up @@ -48,21 +48,17 @@ def print_grouped_generators() -> None:


def create_example_every_generator() -> None:
print("Generating SVG examples for every possible generator.")
for group in generator_groups():
# print()
defaultStdout = sys.stdout

for boxExample in group.generators:
boxName = boxExample.__name__
notTestGenerator = ('GridfinityTrayLayout', 'TrayLayout', 'TrayLayoutFile', 'TypeTray', 'Edges',)
brokenGenerator = ('WallConsole', 'HolePattern', 'FillTest',) # todo fix?
brokenGenerator = ()
avoidGenerator = notTestGenerator + brokenGenerator
if boxName in avoidGenerator:
print(f"SKIP: {boxName}")
continue
# print(f"generate example for: {boxName}")

# sys.stdout = buffer = StringIO()
print(f"Generate example for: {boxName}")

box = boxExample()
box.translations = get_translation()
Expand All @@ -71,18 +67,10 @@ def create_example_every_generator() -> None:
box.open()
box.render()
boxData = box.close()
#
# sys.stdout = defaultStdout
# output = buffer.getvalue() # Get the captured output
# if 0 < len(output):
# print("ERROR: " + boxExample.__name__)

file = Path('examples') / (boxName + '.svg')
file.write_bytes(boxData.getvalue())

# with os.fdopen(sys.stdout.fileno(), "wb", closefd=False) if box.output == "-" else open(box.output, 'wb') as f:
# f.write(boxData.getvalue())


def get_translation():
try:
Expand Down Expand Up @@ -125,7 +113,7 @@ def group_generators(generators):
return groups


def generators_by_name() -> dict[str, Type[boxes.Boxes]]:
def generators_by_name() -> dict[str, type[boxes.Boxes]]:
all_generators = boxes.generators.getAllBoxGenerators()

return {
Expand Down
Empty file added tests/data/.gitkeep
Empty file.
78 changes: 78 additions & 0 deletions tests/test_svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from __future__ import annotations

import sys
from pathlib import Path

import pytest
from lxml import etree

try:
import boxes
except ImportError:
sys.path.append(Path(__file__).resolve().parent.parent.__str__())
import boxes

import boxes.generators


class TestSVG:
all_generators = boxes.generators.getAllBoxGenerators().values()

notTestGenerator = ('GridfinityTrayLayout', 'TrayLayout', 'TrayLayoutFile', 'TypeTray', 'Edges',)
brokenGenerator = ()
avoidGenerator = notTestGenerator + brokenGenerator

def test_generators_available(self) -> None:
assert len(self.all_generators) != 0

# svgcheck currently do not allow inkscape custom tags.
# @staticmethod
# def is_valid_svg(file_path: str) -> bool:
# result = subprocess.run(['svgcheck', file_path], capture_output=True, text=True)
# return "INFO: File conforms to SVG requirements." in result.stdout

@staticmethod
def is_valid_xml_by_lxml(xml_string: str) -> bool:
try:
etree.fromstring(xml_string)
return True
except etree.XMLSyntaxError:
return False

@staticmethod
def idfunc(val) -> str:
return f"{val.__name__}"

@pytest.mark.parametrize(
"generator",
all_generators,
ids=idfunc.__func__,
)
def test_generator(self, generator: type[boxes.Boxes], capsys) -> None:
boxName = generator.__name__
if boxName in self.avoidGenerator:
pytest.skip("Skipped generator")
box = generator()
box.parseArgs("")
box.metadata["reproducible"] = True
box.open()
box.render()
boxData = box.close()

out, err = capsys.readouterr()

assert 100 < boxData.__sizeof__(), "No data generated."
assert 0 == len(out), "Console output generated."
assert 0 == len(err), "Console error generated."

# Use external library lxml as cross-check.
assert self.is_valid_xml_by_lxml(boxData.getvalue()) is True, "Invalid XML according to library lxml."

file = Path(__file__).resolve().parent / 'data' / (boxName + '.svg')
file.write_bytes(boxData.getvalue())

# Use example data from repository as reference data.
referenceData = Path(__file__).resolve().parent.parent / 'examples' / (boxName + '.svg')
assert referenceData.exists() is True, "Reference file for comparison does not exist."
assert referenceData.is_file() is True, "Reference file for comparison does not exist."
assert referenceData.read_bytes() == boxData.getvalue(), "SVG files are not equal. If change is intended, please update example files."

0 comments on commit 6bb07ca

Please sign in to comment.