From d43eff8b49022719b2933e8429a05e3a35b8762f Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 23 Feb 2023 23:52:29 +0100 Subject: Extend CLI tests --- gerbonara/tests/test_cli.py | 93 +++++++++++++++++++++++++++++++++++++++++- gerbonara/tests/test_rs274x.py | 4 +- 2 files changed, 93 insertions(+), 4 deletions(-) (limited to 'gerbonara/tests') diff --git a/gerbonara/tests/test_cli.py b/gerbonara/tests/test_cli.py index e085894..d5a0ad4 100644 --- a/gerbonara/tests/test_cli.py +++ b/gerbonara/tests/test_cli.py @@ -21,18 +21,34 @@ from pathlib import Path import re import tempfile import json +from unittest import mock import pytest from click.testing import CliRunner from bs4 import BeautifulSoup from .utils import * -from ..cli import render, rewrite, transform, merge, bounding_box, layers, meta +from .. import cli +from ..utils import MM + + +@pytest.fixture() +def file_mock(): + old = cli.GerberFile + c_obj = cli.GerberFile = mock.Mock() + i_obj = c_obj.open.return_value = mock.Mock() + i_obj.bounding_box.return_value = (0, 0), (50, 100) + yield i_obj + cli.GerberFile = old + class TestRender: def invoke(self, *args): runner = CliRunner() - res = runner.invoke(render, list(map(str, args))) + res = runner.invoke(cli.render, list(map(str, args))) + print(res.output) + if res.exception: + raise res.exception assert res.exit_code == 0 return res.output @@ -115,3 +131,76 @@ class TestRender: assert len(colors_without) == len(colors_with) assert colors_with - {'#67890a'} == set(test_colorscheme.values()) - {'#67890abc'} + +class TestRewrite: + def invoke(self, *args): + runner = CliRunner() + res = runner.invoke(cli.rewrite, list(map(str, args))) + print(res.output) + if res.exception: + raise res.exception + assert res.exit_code == 0 + return res.output + + def test_basic(self): + assert self.invoke('--version').startswith('Version ') + + @pytest.mark.parametrize('reference', ['example_flash_obround.gbr'], indirect=True) + def test_transforms(self, reference, file_mock): + with tempfile.NamedTemporaryFile() as tmpout: + self.invoke(reference, tmpout.name, '--transform', 'rotate(90); translate(10, 10); rotate(-45.5); scale(2)') + file_mock.rotate.assert_has_calls([ + mock.call(math.radians(90), 0, 0, MM), + mock.call(math.radians(-45.5), 0, 0, MM)]) + file_mock.offset.assert_called_with(10, 10, MM) + file_mock.scale.assert_called_with(2) + assert file_mock.save.called + assert file_mock.save.call_args[0][0] == tmpout.name + + @pytest.mark.parametrize('reference', ['example_flash_obround.gbr'], indirect=True) + def test_real_invocation(self, reference): + with tempfile.NamedTemporaryFile() as tmpout: + self.invoke(reference, tmpout.name, '--transform', 'rotate(45); translate(10, 0)') + assert tmpout.read() + + +class TestMerge: + def invoke(self, *args): + runner = CliRunner() + res = runner.invoke(cli.merge, list(map(str, args))) + if res.exception: + raise res.exception + assert res.exit_code == 0 + return res.output + + def test_basic(self): + assert self.invoke('--version').startswith('Version ') + + @pytest.mark.parametrize('file_a', ['kicad-older']) + @pytest.mark.parametrize('file_b', ['eagle-newer']) + def test_real_invocation(self, file_a, file_b): + with tempfile.TemporaryDirectory() as outdir: + self.invoke(reference_path(file_a), '--rotation', '90', '--offset', '0,0', + reference_path(file_b), '--offset', '100,100', '--rotation', '0', + outdir, '--output-naming-scheme', 'kicad', '--output-board-name', 'foobar', + '--warnings', 'ignore') + assert (Path(outdir) / 'foobar-F.Cu.gbr').exists() + + +class TestMeta: + def invoke(self, *args): + runner = CliRunner() + res = runner.invoke(cli.meta, list(map(str, args))) + print(res.output) + if res.exception: + raise res.exception + assert res.exit_code == 0 + return res.output + + def test_basic(self): + assert self.invoke('--version').startswith('Version ') + + @pytest.mark.parametrize('reference', ['example_flash_obround.gbr'], indirect=True) + def test_real_invocation(self, reference): + j = json.loads(self.invoke(reference, '--warnings', 'ignore')) + diff --git a/gerbonara/tests/test_rs274x.py b/gerbonara/tests/test_rs274x.py index 0807bfd..2fb51d9 100644 --- a/gerbonara/tests/test_rs274x.py +++ b/gerbonara/tests/test_rs274x.py @@ -336,7 +336,7 @@ def test_rotation_center(reference, angle, center, tmpfile): tmp_gbr = tmpfile('Output gerber', '.gbr') f = GerberFile.open(reference) - f.rotate(math.radians(angle), center=center) + f.rotate(math.radians(angle), *center) f.save(tmp_gbr) # calculate circle center in SVG coordinates @@ -379,7 +379,7 @@ def test_combined(reference, angle, center, offset, tmpfile): tmp_gbr = tmpfile('Output gerber', '.gbr') f = GerberFile.open(reference) - f.rotate(math.radians(angle), center=center) + f.rotate(math.radians(angle), *center) f.offset(*offset) f.save(tmp_gbr, settings=FileSettings(unit=f.unit, number_format=(4,7))) -- cgit