Commit 39308b80 authored by Cyril Roelandt's avatar Cyril Roelandt
Browse files

Show valid frontends/backends in argparse error message.

parent b760cf5f
......@@ -111,7 +111,7 @@ class TestUtils(unittest.TestCase):
class TestParser(unittest.TestCase):
def setUp(self):
self.parser = upt.upt.create_parser()
self.parser = upt.upt.create_parser(['pypi'], ['guix'])
def test_package_missing_frontend(self):
args = 'package -b guix requests'.split()
......@@ -186,24 +186,43 @@ class TestCommandLine(unittest.TestCase):
@mock.patch('upt.upt._get_installed_frontends', return_value={})
@mock.patch('upt.upt._get_installed_backends',
return_value={'valid': mock.Mock()})
def test_package_invalid_frontend(self, m_backends, m_frontends):
sys.argv.extend('package -f invalid -b valid pkgname'.split())
with self.assertRaises(SystemExit) as exit:
def test_package_no_frontends(self, m_backends, m_frontends):
sys.argv.extend('package'.split())
with self.assertRaises(SystemExit):
upt.upt.main()
self.assertEqual(exit.exception.code, 1)
@mock.patch('upt.upt._get_installed_frontends',
return_value={'valid': mock.Mock()})
@mock.patch('upt.upt._get_installed_backends', return_value={})
def test_package_no_backends(self, m_backends, m_frontends):
sys.argv.extend('package'.split())
with self.assertRaises(SystemExit):
upt.upt.main()
@mock.patch('upt.upt._get_installed_frontends',
return_value={'valid-frontend': mock.Mock()})
@mock.patch('upt.upt._get_installed_backends',
return_value={'valid-backend': mock.Mock()})
def test_package_invalid_frontend(self, m_backends, m_frontends):
sys.argv.extend('package -f invalid -b valid-backend pkgname'.split())
with self.assertRaises(SystemExit) as exit:
upt.upt.main()
self.assertEqual(exit.exception.code, 2)
@mock.patch('upt.upt._get_installed_frontends',
return_value={'valid-frontend': mock.Mock()})
@mock.patch('upt.upt._get_installed_backends',
return_value={'valid-backend': mock.Mock()})
def test_package_invalid_backend(self, m_backends, m_frontends):
sys.argv.extend('package -f valid -b invalid pkgname'.split())
sys.argv.extend('package -f valid-frontend -b invalid pkgname'.split())
with self.assertRaises(SystemExit) as exit:
upt.upt.main()
self.assertEqual(exit.exception.code, 1)
self.assertEqual(exit.exception.code, 2)
@mock.patch('upt.upt._get_installed_frontends',
return_value={'valid-frontend': mock.Mock()})
@mock.patch('upt.upt._get_installed_backends')
@mock.patch('upt.upt._get_installed_backends',
return_value={'valid-backend': mock.Mock()})
def test_package_unhandled_frontend(self, m_backends, m_frontends):
cmdline = 'package -f valid-frontend -b valid-backend pkgname'
sys.argv.extend(cmdline.split())
......@@ -222,7 +241,8 @@ class TestCommandLine(unittest.TestCase):
@mock.patch('upt.upt._get_installed_frontends',
return_value={'valid-frontend': mock.Mock()})
@mock.patch('upt.upt._get_installed_backends')
@mock.patch('upt.upt._get_installed_backends',
return_value={'valid-backend': mock.Mock()})
def test_package_invalid_pkgname(self, m_backends, m_frontends):
cmdline = 'package -f valid-frontend -b valid-backend pkgname'
sys.argv.extend(cmdline.split())
......
......@@ -191,7 +191,7 @@ class Package(object):
os.remove(archive._filepath)
def create_parser():
def create_parser(frontends, backends):
parser = argparse.ArgumentParser(prog='upt')
subparsers = parser.add_subparsers(title='Commands', dest='cmd')
......@@ -209,8 +209,10 @@ def create_parser():
help='Package a piece of software')
required_args = parser_package.add_argument_group('Required arguments')
required_args.add_argument('-f', '--frontend', required=True,
choices=frontends,
help='Frontend to use')
required_args.add_argument('-b', '--backend', required=True,
choices=backends,
help='Backend to use')
logger_group = parser_package.add_mutually_exclusive_group()
logger_group.add_argument('--debug', action='store_const',
......@@ -246,39 +248,44 @@ def _get_installed_backends():
def main():
parser = create_parser()
frontends = _get_installed_frontends()
backends = _get_installed_backends()
parser = create_parser(list(frontends.keys()), list(backends.keys()))
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
# We need to handle this here, because if we let the parser run in an
# environment without frontends/backends, it will return a useless error
# message, such as "choose a frontend in {}".
if sys.argv[1] == 'package':
if not frontends:
sys.exit('You need to install at least one frontend, for instance '
'upt-cpan, upt-pypi or upt-rubygems')
if not backends:
sys.exit('You need to install at least one backend, for instance '
'upt-fedora, upt-freebsd, upt-guix, upt-nix or '
'upt-openbsd')
args = parser.parse_args()
if args.cmd == 'list-backends':
for backend in _get_installed_backends().keys():
for backend in backends.keys():
print(backend)
sys.exit(0)
if args.cmd == 'list-frontends':
for frontend in _get_installed_frontends().keys():
for frontend in frontends.keys():
print(frontend)
sys.exit(0)
if args.cmd == 'package':
frontends = _get_installed_frontends()
backends = _get_installed_backends()
try:
frontend = frontends[args.frontend]()
except KeyError:
print(f'No frontend named {args.frontend}.')
sys.exit(1)
try:
backend = backends[args.backend]()
except KeyError:
print(f'No backend named {args.backend}.')
sys.exit(1)
# There will not be a KeyError here, since argparse will catch "wrong"
# values for us.
frontend = frontends[args.frontend]()
backend = backends[args.backend]()
logger = upt.log.create_logger(args.log_level)
try:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment