Fixed log.init() errors when pcbnew/yaml modules aren't installed.

Also added tests for these cases.
This commit is contained in:
Salvador E. Tropea 2020-10-20 11:41:01 -03:00
parent 6bbd15eaf2
commit 263b9c41e4
6 changed files with 45 additions and 6 deletions

View File

@ -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)

View File

@ -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?")

View File

@ -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

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
import os
import sys
# Setup the path to load local kibot module

View File

@ -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

View File

@ -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')