diff --git a/Makefile b/Makefile index 19d40059..ad091ac0 100644 --- a/Makefile +++ b/Makefile @@ -106,6 +106,9 @@ gen_ref: cp -a $(REFILL).refill $(REFILL) src/kibot -c tests/yaml_samples/pdf_zone-refill.kibot.yaml -b tests/board_samples/zone-refill.kicad_pcb -d $(REFDIR) src/kibot -c tests/yaml_samples/print_pcb_zone-refill.kibot.yaml -b tests/board_samples/zone-refill.kicad_pcb -d $(REFDIR) + src/kibot -c tests/yaml_samples/sch_no_inductors.kibot.yaml -e tests/board_samples/test_v5.sch -d $(REFDIR) + mv "$(REFDIR)no_inductor/test_v5-schematic_(no_L).pdf" $(REFDIR) + rmdir $(REFDIR)no_inductor/ cp -a $(REFILL).ok $(REFILL) doc: diff --git a/tests/reference/test_v5-schematic_(no_L).pdf b/tests/reference/test_v5-schematic_(no_L).pdf new file mode 100644 index 00000000..3c067242 Binary files /dev/null and b/tests/reference/test_v5-schematic_(no_L).pdf differ diff --git a/tests/test_plot/test_print_sch.py b/tests/test_plot/test_print_sch.py index 08d85aea..41a78e1d 100644 --- a/tests/test_plot/test_print_sch.py +++ b/tests/test_plot/test_print_sch.py @@ -11,17 +11,20 @@ pytest-3 --log-cli-level debug import os import sys +import logging # Look for the 'utils' module from where the script is running prev_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if prev_dir not in sys.path: sys.path.insert(0, prev_dir) from kibot.misc import (PDF_SCH_PRINT, SVG_SCH_PRINT) +from kibot.kicad.v5_sch import Schematic, SchFileError # Utils import from utils import context PDF_DIR = '' PDF_FILE = 'Schematic.pdf' SVG_FILE = 'Schematic.svg' +NI_DIR = 'no_inductor' def test_print_sch_ok(): @@ -54,3 +57,61 @@ def test_print_sch_svg_fail(): ctx = context.TestContext('PrSCHFail_SVG', prj, 'print_sch_svg', PDF_DIR) ctx.run(SVG_SCH_PRINT, no_board_file=True, extra=['-e', os.path.join(ctx.get_board_dir(), 'print_err.sch')]) ctx.clean_up() + + +def check_l1(ctx): + ctx.run() + o_name = os.path.join(NI_DIR, 'test_v5.sch') + ctx.expect_out_file(o_name) + sch = Schematic() + try: + sch.load(ctx.get_out_path(o_name)) + except SchFileError as e: + logging.error('At line {} of `{}`: {}'.format(e.line, e.file, e.msg)) + logging.error('Line content: `{}`'.format(e.code)) + assert False + comps = sch.get_components() + l1 = next(c for c in comps if c.ref == 'L1') + assert l1 + logging.debug('Found L1') + assert l1.lib == 'n' + logging.debug('L1 is crossed') + ctx.clean_up() + + +def test_sch_variant_ni_1(): + """ Using a variant """ + prj = 'test_v5' # Is the most complete, contains every KiCad object I know + ctx = context.TestContextSCH('test_sch_variant_ni_1', prj, 'sch_no_inductors_1', PDF_DIR) + check_l1(ctx) + + +def test_sch_variant_ni_2(): + """ Using a filter """ + prj = 'test_v5' # Is the most complete, contains every KiCad object I know + ctx = context.TestContextSCH('test_sch_variant_ni_2', prj, 'sch_no_inductors_2', PDF_DIR) + check_l1(ctx) + + +def test_print_sch_variant_ni_1(): + """ Using a variant """ + prj = 'test_v5' # Is the most complete, contains every KiCad object I know + ctx = context.TestContextSCH('test_print_sch_variant_ni_1', prj, 'print_pdf_no_inductors_1', PDF_DIR) + ctx.run() + r_name = 'test_v5-schematic_(no_L).pdf' + o_name = os.path.join(NI_DIR, r_name) + ctx.expect_out_file(o_name) + ctx.compare_pdf(o_name, r_name) + ctx.clean_up() + + +def test_print_sch_variant_ni_2(): + """ Using a filter """ + prj = 'test_v5' # Is the most complete, contains every KiCad object I know + ctx = context.TestContextSCH('test_print_sch_variant_ni_2', prj, 'print_pdf_no_inductors_2', PDF_DIR) + ctx.run() + r_name = 'test_v5-schematic_(no_L).pdf' + o_name = os.path.join(NI_DIR, 'test_v5-schematic.pdf') + ctx.expect_out_file(o_name) + ctx.compare_pdf(o_name, r_name) + ctx.clean_up() diff --git a/tests/utils/context.py b/tests/utils/context.py index 7a126a89..8bfffc8d 100644 --- a/tests/utils/context.py +++ b/tests/utils/context.py @@ -20,6 +20,10 @@ MODE_SCH = 1 MODE_PCB = 0 +def quote(s): + return '"'+s+'"' + + class TestContext(object): def __init__(self, test_name, board_name, yaml_name, sub_dir, yaml_compressed=False): @@ -296,17 +300,21 @@ class TestContext(object): m = re.search(t, txt, re.MULTILINE) assert m is None - def compare_image(self, image, reference=None, diff='diff.png'): + def compare_image(self, image, reference=None, diff='diff.png', ref_out_dir=False): """ For images and single page PDFs """ if reference is None: reference = image + if ref_out_dir: + reference = self.get_out_path(reference) + else: + reference = os.path.join(REF_DIR, reference) cmd = ['compare', # Tolerate 5 % error in color '-fuzz', '5%', # Count how many pixels differ '-metric', 'AE', self.get_out_path(image), - os.path.join(REF_DIR, reference), + reference, # Avoid the part where KiCad version is printed '-crop', '100%x92%+0+0', '+repage', '-colorspace', 'RGB', @@ -344,16 +352,7 @@ class TestContext(object): assert len(ref_pages) == len(gen_pages) # Compare each page for page in range(len(ref_pages)): - cmd = ['compare', '-metric', 'MSE', - self.get_out_path('ref-'+str(page)+'.png'), - self.get_out_path('gen-'+str(page)+'.png'), - self.get_out_path(diff.format(page))] - logging.debug('Comparing images with: '+str(cmd)) - res = subprocess.check_output(cmd, stderr=subprocess.STDOUT) - m = re.match(r'([\d\.]+) \(([\d\.]+)\)', res.decode()) - assert m - logging.debug('MSE={} ({})'.format(m.group(1), m.group(2))) - assert float(m.group(2)) == 0.0 + self.compare_image('gen-'+str(page)+'.png', 'ref-'+str(page)+'.png', diff.format(page), ref_out_dir=True) def compare_txt(self, text, reference=None, diff='diff.txt'): if reference is None: diff --git a/tests/yaml_samples/print_pdf_no_inductors_1.kibot.yaml b/tests/yaml_samples/print_pdf_no_inductors_1.kibot.yaml new file mode 100644 index 00000000..4ae89156 --- /dev/null +++ b/tests/yaml_samples/print_pdf_no_inductors_1.kibot.yaml @@ -0,0 +1,25 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'no_inductor' + comment: 'Inductors removed' + type: generic + exclude_refs: + - L* + +variants: + - name: 'no_inductor' + comment: 'Inductors removed' + type: kibom + file_id: '_(no_L)' + dnf_filter: 'no_inductor' + +outputs: + - name: 'no_inductor' + comment: "Inductors removed" + type: pdf_sch_print + dir: no_inductor + options: + variant: 'no_inductor' diff --git a/tests/yaml_samples/print_pdf_no_inductors_2.kibot.yaml b/tests/yaml_samples/print_pdf_no_inductors_2.kibot.yaml new file mode 100644 index 00000000..e83133a4 --- /dev/null +++ b/tests/yaml_samples/print_pdf_no_inductors_2.kibot.yaml @@ -0,0 +1,18 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'no_inductor' + comment: 'Inductors removed' + type: generic + exclude_refs: + - L* + +outputs: + - name: 'no_inductor' + comment: "Inductors removed" + type: pdf_sch_print + dir: no_inductor + options: + dnf_filter: 'no_inductor' diff --git a/tests/yaml_samples/sch_no_inductors_1.kibot.yaml b/tests/yaml_samples/sch_no_inductors_1.kibot.yaml new file mode 100644 index 00000000..0f31165b --- /dev/null +++ b/tests/yaml_samples/sch_no_inductors_1.kibot.yaml @@ -0,0 +1,25 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'no_inductor' + comment: 'Inductors removed' + type: generic + exclude_refs: + - L* + +variants: + - name: 'no_inductor' + comment: 'Inductors removed' + type: kibom + file_id: '_(no_L)' + dnf_filter: 'no_inductor' + +outputs: + - name: 'no_inductor' + comment: "Inductors removed" + type: sch_variant + dir: no_inductor + options: + variant: 'no_inductor' diff --git a/tests/yaml_samples/sch_no_inductors_2.kibot.yaml b/tests/yaml_samples/sch_no_inductors_2.kibot.yaml new file mode 100644 index 00000000..6c9e16fd --- /dev/null +++ b/tests/yaml_samples/sch_no_inductors_2.kibot.yaml @@ -0,0 +1,18 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'no_inductor' + comment: 'Inductors removed' + type: generic + exclude_refs: + - L* + +outputs: + - name: 'no_inductor' + comment: "Inductors removed" + type: sch_variant + dir: no_inductor + options: + dnf_filter: 'no_inductor'