kicad/qa/tests/cli/test_sch.py

185 lines
7.2 KiB
Python
Raw Normal View History

2023-04-26 11:16:14 +00:00
#
# This program source code file is part of KiCad, a free EDA CAD application.
#
# Copyright (C) 2023 Mark Roszko <mark.roszko@gmail.com>
# Copyright (C) 2023 KiCad Developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import utils
import cairosvg
import re
import os
from pathlib import Path
import pytest
2023-05-16 00:05:31 +00:00
from typing import List
import platform
from PIL import Image
import numpy as np
2023-04-26 11:16:14 +00:00
def images_are_equal( image1: str, image2: str ):
image1 = Image.open( image1 )
image2 = Image.open( image2 )
if image1.size != image2.size:
return False
if image1.mode != image2.mode:
return False
sum = np.sum( ( np.asarray ( image1 ).astype( np.float32 ) - np.asarray( image2 ).astype( np.float32 ) ) ** 2.0 )
if sum == 0:
return True
else:
norm_sum = sum / np.sqrt( sum )
return norm_sum < 0.001
@pytest.mark.parametrize("test_file,output_dir,compare_fn,cli_args",
[("cli/basic_test/basic_test.kicad_sch", "basic_test", "cli/basic_test/basic_test.png", []),
("cli/basic_test/basic_test.kicad_sch", "basic_test_nobg_bnw_nods", "cli/basic_test/basic_test_nobg_bnw_nods.png", ["--no-background-color", "--exclude-drawing-sheet", "--black-and-white"])
2023-04-26 11:16:14 +00:00
])
def test_sch_export_svg( kitest,
test_file: str,
output_dir: str,
compare_fn: str,
2023-05-16 00:05:31 +00:00
cli_args: List[str] ):
2023-04-26 11:16:14 +00:00
input_file = kitest.get_data_file_path( test_file )
output_path = kitest.get_output_path( "cli/{}/".format( output_dir ) )
2023-04-26 11:16:14 +00:00
command = ["kicad-cli", "sch", "export", "svg"]
command.extend( cli_args )
command.append( "-o" )
command.append( str( output_path ) )
command.append( input_file )
stdout, stderr, exitcode = utils.run_and_capture( command )
2023-04-26 11:16:14 +00:00
assert exitcode == 0
assert stderr == ''
assert stdout is not None
2023-04-26 11:16:14 +00:00
stdout_regex = re.match("^Plotted to '(.+)'", stdout)
assert stdout_regex
2023-04-26 11:16:14 +00:00
# now try and manipulate the extracted path
output_svg_path = Path( stdout_regex.group(1) )
assert output_svg_path.exists()
2023-04-26 11:16:14 +00:00
kitest.add_attachment( output_svg_path )
2023-04-26 11:16:14 +00:00
png_converted_from_svg_path = output_svg_path.with_suffix( '.png' )
2023-04-26 11:16:14 +00:00
compare_file_path = kitest.get_data_file_path( compare_fn )
cairosvg.svg2png( url=str( output_svg_path ), write_to=str( png_converted_from_svg_path ), dpi=300 )
assert images_are_equal( png_converted_from_svg_path, compare_file_path )
2023-04-26 11:16:14 +00:00
@pytest.mark.parametrize("test_file,output_fn,line_skip_count,skip_compare,cli_args",
[("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadsexpr", 5, True, []),
("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadsexpr", 5, True,["--format=kicadsexpr"]),
("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadxml", 6, True,["--format=kicadxml"]),
2023-05-06 00:15:15 +00:00
# currently inconsistenly sorts nets between platforms (MSW/Linux)
("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.cadstar", 3, True, ["--format=cadstar"]),
("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.orcadpcb2", 1, False, ["--format=orcadpcb2"])
2023-04-26 11:16:14 +00:00
])
def test_sch_export_netlist( kitest,
test_file: str,
output_fn: str,
2023-04-26 11:16:14 +00:00
line_skip_count: int,
skip_compare: bool,
2023-05-16 00:05:31 +00:00
cli_args: List[str] ):
2023-04-26 11:16:14 +00:00
input_file = kitest.get_data_file_path( test_file )
compare_filepath = kitest.get_data_file_path( "cli/basic_test/{}".format( output_fn ) )
output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
2023-04-26 11:16:14 +00:00
command = ["kicad-cli", "sch", "export", "netlist"]
command.extend( cli_args )
command.append( "-o" )
command.append( str( output_filepath ) )
command.append( input_file )
stdout, stderr, exitcode = utils.run_and_capture( command )
2023-04-26 11:16:14 +00:00
assert exitcode == 0
assert stderr == ''
# some of our netlist formats are not cross platform so skip for now
if not skip_compare:
assert utils.textdiff_files( compare_filepath, str( output_filepath ), line_skip_count )
2023-04-26 11:16:14 +00:00
kitest.add_attachment( str( output_filepath ) )
@pytest.mark.parametrize("test_file,output_fn,cli_args",
[("cli/basic_test/basic_test.kicad_sch", "basic_test.pdf", []),
("cli/basic_test/basic_test.kicad_sch", "basic_test.bnw.nods.nobg.pdf", ["--black-and-white","--exclude-drawing-sheet","--no-background-color"]),
("cli/basic_test/basic_test.kicad_sch", "basic_test.pone.pdf", ["--plot-one"])
2023-04-26 11:16:14 +00:00
])
def test_sch_export_pdf( kitest,
test_file: str,
output_fn: str,
2023-05-16 00:05:31 +00:00
cli_args: List[str] ):
2023-04-26 11:16:14 +00:00
input_file = kitest.get_data_file_path( test_file )
output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
2023-04-26 11:16:14 +00:00
command = ["kicad-cli", "sch", "export", "pdf"]
command.extend( cli_args )
command.append( "-o" )
command.append( str( output_filepath ) )
command.append( input_file )
stdout, stderr, exitcode = utils.run_and_capture( command )
2023-04-26 11:16:14 +00:00
assert exitcode == 0
assert stderr == ''
kitest.add_attachment( str( output_filepath ) )
@pytest.mark.parametrize("test_file,output_fn,line_skip_count,cli_args",
[("cli/basic_test/basic_test.kicad_sch", "basic_test.pythonbom", 6, [])
2023-04-26 11:16:14 +00:00
])
def test_sch_export_pythonbom( kitest,
test_file: str,
output_fn: str,
2023-04-26 11:16:14 +00:00
line_skip_count: int,
2023-05-16 00:05:31 +00:00
cli_args: List[str] ):
2023-04-26 11:16:14 +00:00
input_file = kitest.get_data_file_path( test_file )
compare_filepath = kitest.get_data_file_path( "cli/basic_test/{}".format( output_fn ) )
output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
2023-04-26 11:16:14 +00:00
command = ["kicad-cli", "sch", "export", "python-bom"]
command.extend( cli_args )
command.append( "-o" )
command.append( str( output_filepath ) )
command.append( input_file )
stdout, stderr, exitcode = utils.run_and_capture( command )
2023-04-26 11:16:14 +00:00
assert exitcode == 0
assert stderr == ''
# pythonbom is not currently crossplatform (platform specific paths) to enable diffs
2023-04-26 11:16:14 +00:00
kitest.add_attachment( str( output_filepath ) )