You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.7 KiB

  1. #
  2. # This program source code file is part of KiCad, a free EDA CAD application.
  3. #
  4. # Copyright (C) 2023 Mark Roszko <mark.roszko@gmail.com>
  5. # Copyright (C) 2023 KiCad Developers
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21. #
  22. import utils
  23. import cairosvg
  24. import re
  25. from pathlib import Path
  26. import pytest
  27. from typing import List
  28. @pytest.mark.parametrize("test_file,output_dir,compare_fn,cli_args",
  29. [("cli/basic_test/basic_test.kicad_sch", "basic_test", "cli/basic_test/basic_test.png", []),
  30. ("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"])
  31. ])
  32. def test_sch_export_svg( kitest,
  33. test_file: str,
  34. output_dir: str,
  35. compare_fn: str,
  36. cli_args: List[str] ):
  37. input_file = kitest.get_data_file_path( test_file )
  38. output_path = kitest.get_output_path( "cli/{}/".format( output_dir ) )
  39. command = ["kicad-cli", "sch", "export", "svg"]
  40. command.extend( cli_args )
  41. command.append( "-o" )
  42. command.append( str( output_path ) )
  43. command.append( input_file )
  44. stdout, stderr, exitcode = utils.run_and_capture( command )
  45. assert exitcode == 0
  46. assert stderr == ''
  47. assert stdout is not None
  48. stdout_regex = re.match("^Plotted to '(.+)'", stdout)
  49. assert stdout_regex
  50. # now try and manipulate the extracted path
  51. output_svg_path = Path( stdout_regex.group(1) )
  52. assert output_svg_path.exists()
  53. kitest.add_attachment( output_svg_path )
  54. png_converted_from_svg_path = output_svg_path.with_suffix( '.png' )
  55. compare_file_path = kitest.get_data_file_path( compare_fn )
  56. cairosvg.svg2png( url=str( output_svg_path ), write_to=str( png_converted_from_svg_path ), dpi=1200 )
  57. assert utils.images_are_equal( png_converted_from_svg_path, compare_file_path )
  58. @pytest.mark.parametrize("test_file,output_fn,line_skip_count,skip_compare,cli_args",
  59. [("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadsexpr", 5, True, []),
  60. ("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadsexpr", 5, True,["--format=kicadsexpr"]),
  61. ("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.kicadxml", 6, True,["--format=kicadxml"]),
  62. # currently inconsistenly sorts nets between platforms (MSW/Linux)
  63. ("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.cadstar", 3, True, ["--format=cadstar"]),
  64. ("cli/basic_test/basic_test.kicad_sch", "basic_test.netlist.orcadpcb2", 1, False, ["--format=orcadpcb2"])
  65. ])
  66. def test_sch_export_netlist( kitest,
  67. test_file: str,
  68. output_fn: str,
  69. line_skip_count: int,
  70. skip_compare: bool,
  71. cli_args: List[str] ):
  72. input_file = kitest.get_data_file_path( test_file )
  73. compare_filepath = kitest.get_data_file_path( "cli/basic_test/{}".format( output_fn ) )
  74. output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
  75. command = ["kicad-cli", "sch", "export", "netlist"]
  76. command.extend( cli_args )
  77. command.append( "-o" )
  78. command.append( str( output_filepath ) )
  79. command.append( input_file )
  80. stdout, stderr, exitcode = utils.run_and_capture( command )
  81. assert exitcode == 0
  82. assert stderr == ''
  83. # some of our netlist formats are not cross platform so skip for now
  84. if not skip_compare:
  85. assert utils.textdiff_files( compare_filepath, str( output_filepath ), line_skip_count )
  86. kitest.add_attachment( str( output_filepath ) )
  87. @pytest.mark.parametrize("test_file,output_fn,cli_args",
  88. [("cli/basic_test/basic_test.kicad_sch", "basic_test.pdf", []),
  89. ("cli/basic_test/basic_test.kicad_sch", "basic_test.bnw.nods.nobg.pdf", ["--black-and-white","--exclude-drawing-sheet","--no-background-color"]),
  90. ("cli/basic_test/basic_test.kicad_sch", "basic_test.pone.pdf", ["--pages", "1"])
  91. ])
  92. def test_sch_export_pdf( kitest,
  93. test_file: str,
  94. output_fn: str,
  95. cli_args: List[str] ):
  96. input_file = kitest.get_data_file_path( test_file )
  97. output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
  98. command = ["kicad-cli", "sch", "export", "pdf"]
  99. command.extend( cli_args )
  100. command.append( "-o" )
  101. command.append( str( output_filepath ) )
  102. command.append( input_file )
  103. stdout, stderr, exitcode = utils.run_and_capture( command )
  104. assert exitcode == 0
  105. assert stderr == ''
  106. kitest.add_attachment( str( output_filepath ) )
  107. @pytest.mark.parametrize("test_file,output_fn,line_skip_count,cli_args",
  108. [("cli/basic_test/basic_test.kicad_sch", "basic_test.pythonbom", 6, [])
  109. ])
  110. def test_sch_export_pythonbom( kitest,
  111. test_file: str,
  112. output_fn: str,
  113. line_skip_count: int,
  114. cli_args: List[str] ):
  115. input_file = kitest.get_data_file_path( test_file )
  116. compare_filepath = kitest.get_data_file_path( "cli/basic_test/{}".format( output_fn ) )
  117. output_filepath = kitest.get_output_path( "cli/" ).joinpath( output_fn )
  118. command = ["kicad-cli", "sch", "export", "python-bom"]
  119. command.extend( cli_args )
  120. command.append( "-o" )
  121. command.append( str( output_filepath ) )
  122. command.append( input_file )
  123. stdout, stderr, exitcode = utils.run_and_capture( command )
  124. assert exitcode == 0
  125. assert stderr == ''
  126. # pythonbom is not currently crossplatform (platform specific paths) to enable diffs
  127. kitest.add_attachment( str( output_filepath ) )