diff --git a/kibot/out_pcbdraw.py b/kibot/out_pcbdraw.py index c2114fda..a3c4312a 100644 --- a/kibot/out_pcbdraw.py +++ b/kibot/out_pcbdraw.py @@ -5,9 +5,10 @@ # Project: KiBot (formerly KiPlot) import os import re -from shutil import which from tempfile import (NamedTemporaryFile) -from subprocess import (check_output, STDOUT, CalledProcessError) +# Here we import the whole module to make monkeypatch work +import subprocess +import shutil from .misc import PCBDRAW, PCBDRAW_ERR, URL_PCBDRAW from .kiplot import check_script from .error import KiPlotConfigurationError @@ -89,8 +90,8 @@ def _get_tmp_name(ext): def _run_command(cmd, tmp_remap=False, tmp_style=False): logger.debug('Executing: '+str(cmd)) try: - cmd_output = check_output(cmd, stderr=STDOUT) - except CalledProcessError as e: + cmd_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: logger.error('Failed to run %s, error %d', cmd[0], e.returncode) if e.output: logger.debug('Output from command: '+e.output.decode()) @@ -251,11 +252,11 @@ class PcbDrawOptions(BaseOptions): cmd.append(output) else: # PNG and JPG outputs are unreliable - if which(SVG2PNG) is None: + if shutil.which(SVG2PNG) is None: logger.warning('`{}` not installed, using unreliable PNG/JPG conversion'.format(SVG2PNG)) logger.warning('If you experiment problems install `librsvg2-bin` or equivalent') cmd.append(output) - elif which(CONVERT) is None: + elif shutil.which(CONVERT) is None: logger.warning('`{}` not installed, using unreliable PNG/JPG conversion'.format(CONVERT)) logger.warning('If you experiment problems install `imagemagick` or equivalent') cmd.append(output) diff --git a/tests/test_plot/test_pcbdraw.py b/tests/test_plot/test_pcbdraw.py index bc03ada3..c09989b7 100644 --- a/tests/test_plot/test_pcbdraw.py +++ b/tests/test_plot/test_pcbdraw.py @@ -7,15 +7,23 @@ pytest-3 --log-cli-level debug import os import sys +import coverage +from shutil import which # 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: sys.path.insert(0, prev_dir) # Utils import from utils import context - +# One more level for the project +prev_dir = os.path.dirname(prev_dir) +if prev_dir not in sys.path: + sys.path.insert(0, prev_dir) +from kibot.mcpy import activate +from kibot.out_pcbdraw import PcbDrawOptions OUT_DIR = 'PcbDraw' +cov = coverage.Coverage() def test_pcbdraw_3Rs(): @@ -34,3 +42,55 @@ def test_pcbdraw_simple(): ctx.expect_out_file(os.path.join(OUT_DIR, prj+'-top.png')) ctx.expect_out_file(os.path.join(OUT_DIR, prj+'-bottom.jpg')) ctx.clean_up() + + +def no_rsvg_convert(name): + if name == 'rsvg-convert': + return None + return which(name) + + +def no_convert(name): + if name == 'convert': + return None + return which(name) + + +def no_run(cmd, stderr): + return b"" + + +def test_pcbdraw_miss_rsvg(caplog, monkeypatch): + """ Check missing rsvg-convert """ + with monkeypatch.context() as m: + m.setattr("shutil.which", no_rsvg_convert) + m.setattr("subprocess.check_output", no_run) + o = PcbDrawOptions() + o.style='' + o.remap=None + o.format='jpg' + cov.load() + cov.start() + o.run('', None) + cov.stop() + cov.save() + assert 'using unreliable PNG/JPG' in caplog.text, caplog.text + assert 'librsvg2-bin' in caplog.text, caplog.text + + +def test_pcbdraw_miss_convert(caplog, monkeypatch): + """ Check missing convert """ + with monkeypatch.context() as m: + m.setattr("shutil.which", no_convert) + m.setattr("subprocess.check_output", no_run) + o = PcbDrawOptions() + o.style='' + o.remap=None + o.format='jpg' + cov.load() + cov.start() + o.run('', None) + cov.stop() + cov.save() + assert 'using unreliable PNG/JPG' in caplog.text, caplog.text + assert 'imagemagick' in caplog.text, caplog.text