From d904dc516f7e76155d19ee78e2bf40ad1c9aed73 Mon Sep 17 00:00:00 2001 From: SET Date: Thu, 13 Aug 2020 19:17:17 -0300 Subject: [PATCH] Added a test for missing xlsxwriter module. Is reported as an error, not fatal, but error. --- tests/test_plot/force_xlsx_error.py | 19 ++++++++ tests/test_plot/test_int_bom.py | 15 ++++-- tests/utils/context.py | 75 +++++++++++++++-------------- 3 files changed, 71 insertions(+), 38 deletions(-) create mode 100755 tests/test_plot/force_xlsx_error.py diff --git a/tests/test_plot/force_xlsx_error.py b/tests/test_plot/force_xlsx_error.py new file mode 100755 index 00000000..2fc0fe77 --- /dev/null +++ b/tests/test_plot/force_xlsx_error.py @@ -0,0 +1,19 @@ +#!/usr/bin/python +import os +import sys +# Setup the path to load local kiplot module +prev_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +if prev_dir not in sys.path: + sys.path.insert(0, prev_dir) +# Force the xlsxwriter module load to fail +sys.modules['xlsxwriter'] = None +# Initialize the logger +from kiplot import log +log.set_domain('kiplot') +logger = log.init(True, False) +logger.debug("Testing bom_writer without xlsxwriter") + +# Import the module to test +from kiplot.bom.bom_writer import write_bom +# Run it +write_bom('bogus', 'xlsx', [], [], {}) diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index 8d052b48..4c10a19e 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -47,6 +47,7 @@ pytest-3 --log-cli-level debug import os import sys import logging +import subprocess # 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: @@ -355,9 +356,9 @@ def test_int_bom_sort_1(): check_kibom_test_netlist(rows, ref_column, 23, None, exp) # Check the sorting assert get_column(rows, ref_column) == exp - ctx.search_err('Malformed value: .?10Q.?') - ctx.search_err('Malformed value: .?\.G.?') - ctx.search_err('Malformed value: .?2\.2k2.?') + ctx.search_err(r'Malformed value: .?10Q.?') + ctx.search_err(r'Malformed value: .?\.G.?') + ctx.search_err(r'Malformed value: .?2\.2k2.?') ctx.clean_up() @@ -1017,3 +1018,11 @@ def test_int_bom_digikey_links_xlsx(): assert 'digikey' in c logging.debug(c + ' OK') ctx.clean_up() + + +def test_int_bom_no_xlsx_support(): + ctx = context.TestContextSCH('test_int_bom_no_xlsx_support', 'links', 'int_bom_digikey_links_xlsx', BOM_DIR) + cmd = [os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/force_xlsx_error.py')] + ctx.do_run(cmd) + ctx.search_err('Python xlsxwriter module not installed') + ctx.search_err('writing XLSX output') diff --git a/tests/utils/context.py b/tests/utils/context.py index 2208e73b..7e174b85 100644 --- a/tests/utils/context.py +++ b/tests/utils/context.py @@ -153,43 +153,12 @@ class TestContext(object): with open(file, 'w') as f: f.write('Dummy file\n') - def run(self, ret_val=None, extra=None, use_a_tty=False, filename=None, no_out_dir=False, no_board_file=False, - no_yaml_file=False, chdir_out=False, no_verbose=False, extra_debug=False, do_locale=False): - logging.debug('Running '+self.test_name) - # Change the command to be local and add the board and output arguments - cmd = [COVERAGE_SCRIPT, 'run', '-a'] + def do_run(self, cmd, ret_val=None, use_a_tty=False, chdir_out=False): + cmd_base = [COVERAGE_SCRIPT, 'run', '-a'] if chdir_out: - cmd.append('--rcfile=../../.coveragerc') + cmd_base.append('--rcfile=../../.coveragerc') os.environ['COVERAGE_FILE'] = os.path.join(os.getcwd(), '.coverage') - cmd.append(os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/../../src/kiplot')) - if not no_verbose: - # One is enough, 2 can generate tons of data when loading libs - cmd.append('-v') - if extra_debug: - cmd.append('-v') - if not no_board_file: - if self.mode == MODE_PCB: - cmd = cmd+['-b', filename if filename else self.board_file] - else: - cmd = cmd+['-e', filename if filename else self.sch_file] - if not no_yaml_file: - cmd = cmd+['-c', self.yaml_file] - if not no_out_dir: - cmd = cmd+['-d', self.output_dir] - if extra is not None: - cmd = cmd+extra - # Do we need a custom locale? - old_LOCPATH = None - old_LANG = None - if do_locale: - # Setup an Spanish for Argentina using UTF-8 locale - old_LOCPATH = os.environ.get('LOCPATH') - old_LANG = os.environ.get('LANG') - os.environ['LOCPATH'] = os.path.abspath('tests/data') - os.environ['LANG'] = 'es_AR.UTF-8' - #os.environ['LANG'] = 'en_US.UTF-8' - logging.debug('LOCPATH='+os.environ['LOCPATH']) - logging.debug('LANG='+os.environ['LANG']) + cmd = cmd_base+cmd logging.debug(cmd) out_filename = self.get_out_path('output.txt') err_filename = self.get_out_path('error.txt') @@ -236,6 +205,42 @@ class TestContext(object): self.err = os.read(f_err, 1000000) os.close(f_err) self.err = self.err.decode() + + + def run(self, ret_val=None, extra=None, use_a_tty=False, filename=None, no_out_dir=False, no_board_file=False, + no_yaml_file=False, chdir_out=False, no_verbose=False, extra_debug=False, do_locale=False): + logging.debug('Running '+self.test_name) + # Change the command to be local and add the board and output arguments + cmd = [os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/../../src/kiplot')] + if not no_verbose: + # One is enough, 2 can generate tons of data when loading libs + cmd.append('-v') + if extra_debug: + cmd.append('-v') + if not no_board_file: + if self.mode == MODE_PCB: + cmd = cmd+['-b', filename if filename else self.board_file] + else: + cmd = cmd+['-e', filename if filename else self.sch_file] + if not no_yaml_file: + cmd = cmd+['-c', self.yaml_file] + if not no_out_dir: + cmd = cmd+['-d', self.output_dir] + if extra is not None: + cmd = cmd+extra + # Do we need a custom locale? + old_LOCPATH = None + old_LANG = None + if do_locale: + # Setup an Spanish for Argentina using UTF-8 locale + old_LOCPATH = os.environ.get('LOCPATH') + old_LANG = os.environ.get('LANG') + os.environ['LOCPATH'] = os.path.abspath('tests/data') + os.environ['LANG'] = 'es_AR.UTF-8' + #os.environ['LANG'] = 'en_US.UTF-8' + logging.debug('LOCPATH='+os.environ['LOCPATH']) + logging.debug('LANG='+os.environ['LANG']) + self.do_run(cmd, use_a_tty, chdir_out) # Do we need to restore the locale? if do_locale: if old_LOCPATH: