Skip to content

Commit

Permalink
Add arg verify
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jan 19, 2021
1 parent 2b885a1 commit 3f08e86
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
32 changes: 20 additions & 12 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
63 changes: 57 additions & 6 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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

0 comments on commit 3f08e86

Please sign in to comment.