From 263b9c41e4c6a27fb8f7dced179c67d1ec2b0e3e Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 20 Oct 2020 11:41:01 -0300 Subject: [PATCH] Fixed log.init() errors when pcbnew/yaml modules aren't installed. Also added tests for these cases. --- kibot/config_reader.py | 4 ++-- kibot/kiplot.py | 4 ++-- tests/test_plot/force_pcbnew_error.py | 12 ++++++++++++ tests/test_plot/force_xlsx_error.py | 2 +- tests/test_plot/force_yaml_error.py | 12 ++++++++++++ tests/test_plot/test_misc.py | 17 ++++++++++++++++- 6 files changed, 45 insertions(+), 6 deletions(-) create mode 100755 tests/test_plot/force_pcbnew_error.py create mode 100755 tests/test_plot/force_yaml_error.py diff --git a/kibot/config_reader.py b/kibot/config_reader.py index d59bdd92..8a4b703d 100644 --- a/kibot/config_reader.py +++ b/kibot/config_reader.py @@ -27,8 +27,8 @@ logger = log.get_logger(__name__) try: import yaml -except ImportError: # pragma: no cover - log.init(False, False) +except ImportError: + log.init() logger.error('No yaml module for Python, install python3-yaml') exit(NO_YAML_MODULE) diff --git a/kibot/kiplot.py b/kibot/kiplot.py index c0d0457e..8add6418 100644 --- a/kibot/kiplot.py +++ b/kibot/kiplot.py @@ -38,8 +38,8 @@ if os.environ.get('KIAUS_USE_NIGHTLY'): sys_path.insert(0, '/usr/lib/kicad-nightly/lib/python3/dist-packages') try: import pcbnew -except ImportError: # pragma: no cover - log.init(False, False) +except ImportError: + log.init() logger.error("Failed to import pcbnew Python module." " Is KiCad installed?" " Do you need to add it to PYTHONPATH?") diff --git a/tests/test_plot/force_pcbnew_error.py b/tests/test_plot/force_pcbnew_error.py new file mode 100755 index 00000000..6980dda7 --- /dev/null +++ b/tests/test_plot/force_pcbnew_error.py @@ -0,0 +1,12 @@ +#!/usr/bin/python3 +import os +import sys +# Setup the path to load local kibot 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 pcbnew module load to fail +sys.modules['pcbnew'] = None +# Import the module to test +from kibot.kiplot import check_eeschema_do + diff --git a/tests/test_plot/force_xlsx_error.py b/tests/test_plot/force_xlsx_error.py index 3ac95dc2..09587160 100755 --- a/tests/test_plot/force_xlsx_error.py +++ b/tests/test_plot/force_xlsx_error.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 import os import sys # Setup the path to load local kibot module diff --git a/tests/test_plot/force_yaml_error.py b/tests/test_plot/force_yaml_error.py new file mode 100755 index 00000000..4cb3cb85 --- /dev/null +++ b/tests/test_plot/force_yaml_error.py @@ -0,0 +1,12 @@ +#!/usr/bin/python3 +import os +import sys +# Setup the path to load local kibot 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 pcbnew module load to fail +sys.modules['yaml'] = None +# Import the module to test +from kibot.config_reader import CfgYamlReader + diff --git a/tests/test_plot/test_misc.py b/tests/test_plot/test_misc.py index c50707d2..d5088149 100644 --- a/tests/test_plot/test_misc.py +++ b/tests/test_plot/test_misc.py @@ -42,7 +42,7 @@ prev_dir = os.path.dirname(prev_dir) if prev_dir not in sys.path: sys.path.insert(0, prev_dir) from kibot.misc import (EXIT_BAD_ARGS, EXIT_BAD_CONFIG, NO_PCB_FILE, NO_SCH_FILE, EXAMPLE_CFG, WONT_OVERWRITE, CORRUPTED_PCB, - PCBDRAW_ERR) + PCBDRAW_ERR, NO_PCBNEW_MODULE, NO_YAML_MODULE) POS_DIR = 'positiondir' @@ -519,3 +519,18 @@ def test_wrong_global_redef(): ctx.run(EXIT_BAD_ARGS, extra=['--global-redef', 'bogus']) assert ctx.search_err('Malformed global-redef option') ctx.clean_up() + + +def test_no_pcbnew(): + ctx = context.TestContext('test_no_pcbnew', 'bom', 'bom', '') + cmd = [os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/force_pcbnew_error.py')] + ctx.do_run(cmd, NO_PCBNEW_MODULE) + ctx.search_err('Failed to import pcbnew Python module.') + ctx.search_err('PYTHONPATH') + + +def test_no_yaml(): + ctx = context.TestContext('test_no_yaml', 'bom', 'bom', '') + cmd = [os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/force_yaml_error.py')] + ctx.do_run(cmd, NO_YAML_MODULE) + ctx.search_err('No yaml module for Python, install python3-yaml')