From d2ef9d19ce29aeba9740011f303db9de89b397d2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 23 Sep 2024 22:46:04 +0200 Subject: Add regression test for github issue #48 This bug caused a hang when deholing certain concave polygons --- svg-flatten/src/test/svg_tests.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'svg-flatten/src/test') diff --git a/svg-flatten/src/test/svg_tests.py b/svg-flatten/src/test/svg_tests.py index 6afefda..36cf9f1 100644 --- a/svg-flatten/src/test/svg_tests.py +++ b/svg-flatten/src/test/svg_tests.py @@ -6,13 +6,14 @@ import unittest from pathlib import Path import subprocess import itertools +import textwrap import os import sys from PIL import Image import numpy as np -def run_svg_flatten(input_file, output_file, *args, **kwargs): +def run_svg_flatten(input_file, output_file, *args, timeout=None, **kwargs): if 'SVG_FLATTEN' in os.environ: svg_flatten = os.environ.get('SVG_FLATTEN') if not hasattr(run_svg_flatten, 'custom_svg_flatten_warned'): @@ -36,7 +37,7 @@ def run_svg_flatten(input_file, output_file, *args, **kwargs): args.append(str(output_file)) try: - proc = subprocess.run(args, capture_output=True, check=True, text=True) + proc = subprocess.run(args, capture_output=True, check=True, text=True, timeout=timeout) except subprocess.CalledProcessError as e: print('Subprocess stdout:') print(e.stdout) @@ -225,6 +226,25 @@ class StrokeMappingTests(unittest.TestCase): num_strokes = sum(1 for l in f.readlines() if 'stroke=' in l) self.assertEqual(num_strokes, 84) +class RegressionTests(unittest.TestCase): + def test_regression_dehole_concave_infinite_loop(self): + test_svg = textwrap.dedent(''' + + + + + + + ''') + + with tempfile.NamedTemporaryFile(suffix='.svg') as tmp_svg,\ + tempfile.NamedTemporaryFile(suffix='.gbr') as tmp_gbr: + tmp_svg.write(test_svg.encode()) + tmp_svg.flush() + + # This will raise subprocess.TimeoutExpired if the test fails. + run_svg_flatten(tmp_svg.name, tmp_gbr.name, format='svg', timeout=15) + for test_in_svg in Path('testdata/svg').glob('*.svg'): # We need to make sure we capture the loop variable's current value here. -- cgit