diff --git a/kibot/out_compress.py b/kibot/out_compress.py index 853c7bb8..089abcb4 100644 --- a/kibot/out_compress.py +++ b/kibot/out_compress.py @@ -65,7 +65,7 @@ class CompressOptions(BaseOptions): super().config() if isinstance(self.files, type): self.files = [] - logger.warning(W_EMPTYZIP+' No files provided, creating an empty archive') + logger.warning(W_EMPTYZIP+'No files provided, creating an empty archive') def create_zip(self, output, files): with ZipFile(output, 'w', compression=self.ZIP_ALGORITHMS[self.compression], compresslevel=9) as zip: @@ -90,7 +90,7 @@ class CompressOptions(BaseOptions): except FileNotFoundError: logger.error('Missing `rar` command, install it') exit(MISSING_TOOL) - except CalledProcessError as e: # pragma: no cover + except CalledProcessError as e: logger.error('Failed to invoke rar command, error {}'.format(e.returncode)) if e.output: logger.debug('Output from command: '+e.output.decode()) diff --git a/tests/test_plot/test_gerber.py b/tests/test_plot/test_gerber.py index 72db627b..924063ed 100644 --- a/tests/test_plot/test_gerber.py +++ b/tests/test_plot/test_gerber.py @@ -96,12 +96,14 @@ def test_gerber_2layer(): def test_gerber_inner_ok(): prj = 'good-project' ctx = context.TestContext('Gerber_Inner', prj, 'gerber_inner', GERBER_DIR) + rarfile = prj+'-result.rar' + ctx.create_dummy_out_file(rarfile) ctx.run() files = [prj+'_GND_Cu.gbr', prj+'_Signal1.gbr', 'test-'+prj+'.gbrjob'] files = [os.path.join(GERBER_DIR, f) for f in files] for f in files: ctx.expect_out_file(f) - ctx.test_compress(prj+'-result.rar', files) + ctx.test_compress(rarfile, files) ctx.clean_up() diff --git a/tests/test_plot/test_misc.py b/tests/test_plot/test_misc.py index 6073351f..79a09b8d 100644 --- a/tests/test_plot/test_misc.py +++ b/tests/test_plot/test_misc.py @@ -31,6 +31,9 @@ pytest-3 --log-cli-level debug import os import sys import shutil +import pytest +import coverage +from subprocess import CalledProcessError import logging # Look for the 'utils' module from where the script is running prev_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -42,11 +45,15 @@ 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, NO_PCBNEW_MODULE, NO_YAML_MODULE) + PCBDRAW_ERR, NO_PCBNEW_MODULE, NO_YAML_MODULE, MISSING_TOOL, WRONG_INSTALL) +from kibot.kiplot import load_actions +from kibot.registrable import RegOutput +from kibot.gs import GS POS_DIR = 'positiondir' MK_TARGETS = ['position'] +cov = coverage.Coverage() def test_skip_pre_and_outputs(): @@ -566,3 +573,75 @@ def test_makefile_1(): assert os.path.abspath(targets[targets['position']]) == ctx.board_file logging.debug('- Target `position` OK') ctx.clean_up() + + +def test_empty_zip(): + prj = 'test_v5' + ctx = context.TestContext('test_empty_zip', prj, 'empty_zip', '') + ctx.run() + ctx.expect_out_file(prj+'-result.zip') + ctx.search_err('No files provided, creating an empty archive') + ctx.clean_up() + + +def mocked_check_output(cmd, stderr=None): + raise FileNotFoundError() + + +def run_compress(ctx): + # Load the plug-ins + load_actions() + # Create a compress object with the dummy file as source + out = RegOutput.get_class_for('compress')() + out.set_tree({'options': {'format': 'RAR', 'files': [{'source': ctx.get_out_path('*')}]}}) + out.config() + # Setup the GS output dir, needed for the output path + GS.out_dir = '.' + # Start coverage + cov.load() + cov.start() + # Run the compression and catch the error + with pytest.raises(SystemExit) as pytest_wrapped_e: + out.run('') + # Stop coverage + cov.stop() + cov.save() + return pytest_wrapped_e + + +def test_no_rar(caplog, monkeypatch): + # Create a silly context to get the output path + ctx = context.TestContext('test_no_rar', 'test_v5', 'empty_zip', '') + # The file we pretend to compress + ctx.create_dummy_out_file('Test.txt') + # We will patch subprocess.check_output to make rar fail + with monkeypatch.context() as m: + m.setattr("subprocess.check_output", mocked_check_output) + pytest_wrapped_e = run_compress(ctx) + # Check we exited because rar isn't installed + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == MISSING_TOOL + assert "Missing `rar` command" in caplog.text + + +def mocked_check_output_2(cmd, stderr=None): + e = CalledProcessError(10, 'rar') + e.output = b'THE_ERROR' + raise e + + +def test_rar_fail(caplog, monkeypatch): + # Create a silly context to get the output path + ctx = context.TestContext('test_no_rar', 'test_v5', 'empty_zip', '') + # The file we pretend to compress + ctx.create_dummy_out_file('Test.txt') + # We will patch subprocess.check_output to make rar fail + with monkeypatch.context() as m: + m.setattr("subprocess.check_output", mocked_check_output_2) + pytest_wrapped_e = run_compress(ctx) + # Check we exited because rar isn't installed + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == WRONG_INSTALL + assert "Failed to invoke rar command, error 10" in caplog.text + assert "THE_ERROR" in caplog.text + diff --git a/tests/yaml_samples/empty_zip.kibot.yaml b/tests/yaml_samples/empty_zip.kibot.yaml new file mode 100644 index 00000000..70efa72d --- /dev/null +++ b/tests/yaml_samples/empty_zip.kibot.yaml @@ -0,0 +1,9 @@ +# Example KiBot config file for a basic 2-layer board +kibot: + version: 1 + +outputs: + - name: result + comment: Test rar compress + type: compress +