From 3f08e86827701951ed8185fa79ebd9796f3f9088 Mon Sep 17 00:00:00 2001 From: cdce8p <30130371+cdce8p@users.noreply.github.com> Date: Tue, 19 Jan 2021 19:50:15 +0100 Subject: [PATCH] Add arg verify --- piplicenses.py | 32 ++++++++++++++--------- test_piplicenses.py | 63 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/piplicenses.py b/piplicenses.py index 862a65b..aff4fc9 100644 --- a/piplicenses.py +++ b/piplicenses.py @@ -584,22 +584,30 @@ def _split_lines(self, text: Text, width: int) -> List[str]: class CompatibleArgumentParser(argparse.ArgumentParser): def parse_args(self, args=None, namespace=None): - args = super(CompatibleArgumentParser, self).parse_args(args, - namespace) - self._check_code_page(args.filter_code_page) - + args = super().parse_args(args, namespace) + self._verify_args(args) return args - @staticmethod - def _check_code_page(code_page): + def _verify_args(self, args): + if args.with_license_file is False and ( + args.no_license_path is True or + args.with_notice_file is True): + self.error( + "'--no-license-path' and '--with-notice-file' require " + "the '--with-license-file' option to be set") + if args.filter_strings is False and \ + args.filter_code_page != 'latin1': + self.error( + "'--filter-code-page' requires the '--filter-strings' " + "option to be set") try: - codecs.lookup(code_page) + codecs.lookup(args.filter_code_page) except LookupError: - print(("error: invalid code page '%s' given for " - "--filter-code-page;\n" - " check https://docs.python.org/3/library/" - "codecs.html for valid code pages") % code_page) - sys.exit(1) + self.error( + "invalid code page '%s' given for '--filter-code-page, " + "check https://docs.python.org/3/library/codecs.html" + "#standard-encodings for valid code pages" + % args.filter_code_page) class NoValueEnum(Enum): diff --git a/test_piplicenses.py b/test_piplicenses.py index ec8d17f..96ddb4a 100644 --- a/test_piplicenses.py +++ b/test_piplicenses.py @@ -5,10 +5,13 @@ import sys import unittest from email import message_from_string +from enum import Enum, auto import docutils.parsers.rst import docutils.utils import docutils.frontend +import pytest +from _pytest.capture import CaptureFixture import piplicenses from piplicenses import (FromArg, __pkgname__, create_parser, output_colored, @@ -18,7 +21,9 @@ select_license_by_source, save_if_needs, RULE_ALL, RULE_FRAME, RULE_HEADER, RULE_NONE, DEFAULT_OUTPUT_FIELDS, SYSTEM_PACKAGES, - LICENSE_UNKNOWN) + LICENSE_UNKNOWN, + CompatibleArgumentParser, value_to_enum_key, + enum_key_to_value) UNICODE_APPENDIX = "" @@ -547,11 +552,6 @@ def test_with_specified_filter(self): packages = list(piplicenses.get_packages(args)) self.assertNotIn(UNICODE_APPENDIX, packages[-1]["summary"]) - def test_invalid_code_page(self): - with self.assertRaises(SystemExit): - self.parser.parse_args(["--filter-strings", - "--filter-code-page=XXX"]) - class MockStdStream(object): @@ -649,3 +649,54 @@ def test_fail_on(monkeypatch): assert '' == mocked_stdout.printed assert 'fail-on license MIT License was found for ' \ 'package' in mocked_stderr.printed + + +def test_enums(): + class TestEnum(Enum): + PLAIN = P = auto() + JSON_LICENSE_FINDER = JLF = auto() + + assert TestEnum.PLAIN == TestEnum.P + assert getattr(TestEnum, value_to_enum_key('jlf')) == \ + TestEnum.JSON_LICENSE_FINDER + assert value_to_enum_key('jlf') == 'JLF' + assert value_to_enum_key('json-license-finder') == \ + 'JSON_LICENSE_FINDER' + assert enum_key_to_value(TestEnum.JSON_LICENSE_FINDER) == \ + 'json-license-finder' + assert enum_key_to_value(TestEnum.PLAIN) == 'plain' + + +@pytest.fixture(scope='package') +def parser(): + return create_parser() + + +def test_verify_args( + parser: CompatibleArgumentParser, capsys: CaptureFixture): + # --with-license-file missing + with pytest.raises(SystemExit) as ex: + parser.parse_args(['--no-license-path']) + capture = capsys.readouterr().err + for arg in ('--no-license-path', '--with-license-file'): + assert arg in capture + + with pytest.raises(SystemExit) as ex: + parser.parse_args(['--with-notice-file']) + capture = capsys.readouterr().err + for arg in ('--with-notice-file', '--with-license-file'): + assert arg in capture + + # --filter-strings missing + with pytest.raises(SystemExit) as ex: + parser.parse_args(['--filter-code-page=utf8']) + capture = capsys.readouterr().err + for arg in ('--filter-code-page', '--filter-strings'): + assert arg in capture + + # invalid code-page + with pytest.raises(SystemExit) as ex: + parser.parse_args(['--filter-strings', '--filter-code-page=XX']) + capture = capsys.readouterr().err + for arg in ('invalid code', '--filter-code-page'): + assert arg in capture