From 7583402643b4b333590ec9b4073b45b51a03687c Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Apr 17 2019 09:39:14 +0000 Subject: Output meaningful message if tag config file does not exist Instead of outputing a traceback to user, this patch prints meaningful message to user. Signed-off-by: Chenxiong Qi --- diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..dbc606f --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Red Hat, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# Written by Chenxiong Qi + +import io +import os +import tempfile +import ursa_major.cli +import pytest + +from mock import patch + + +class TestTagConfigFileCLIOption: + """Test --tag-config-file""" + + @classmethod + def setup_class(cls): + # Create a real file to make json.load work + fd, cls.tag_config_file = tempfile.mkstemp() + with io.open(fd, 'w', encoding='utf-8', closefd=True) as f: + f.write(u'{}') + + @classmethod + def teardown_class(cls): + os.unlink(cls.tag_config_file) + + @patch.object(ursa_major.cli, 'AddModuleHandler') + @patch('ursa_major.cli.ConfigParser') + @patch('os.path.exists', return_value=True) + def test_use_default_file(self, exists, ConfigParser, AddModuleHandler): + cli_cmd = [ + 'ursa-major', 'add-module', '--name', 'nodejs', '--stream', '10', + '--priority', '10', '--tag', 'f30-candidate' + ] + + with patch('sys.argv', new=cli_cmd): + with patch.object(ursa_major.cli, 'DEFAULT_TAG_CONFIG_FILE', + new=self.tag_config_file): + ursa_major.cli.main() + + handler = AddModuleHandler.return_value + pos_args, _ = handler.set_args.call_args + args = pos_args[0] + assert args.tag_config_file == self.tag_config_file + + @patch.object(ursa_major.cli, 'AddModuleHandler') + @patch('ursa_major.cli.ConfigParser') + @patch('os.path.exists', return_value=True) + def test_default_file_does_not_exist(self, exists, ConfigParser, AddModuleHandler): + cli_cmd = [ + 'ursa-major', 'add-module', '--name', 'nodejs', '--stream', '10', + '--priority', '10', '--tag', 'f30-candidate' + ] + + with patch('sys.argv', new=cli_cmd): + with patch.object(ursa_major.cli, 'DEFAULT_TAG_CONFIG_FILE', new='/tmp/xxxx'): + with pytest.raises(SystemExit): + ursa_major.cli.main() + + @patch.object(ursa_major.cli, 'AddModuleHandler') + @patch('ursa_major.cli.ConfigParser') + @patch('os.path.exists', return_value=True) + def test_specify_a_file(self, exists, ConfigParser, AddModuleHandler): + cli_cmd = [ + 'ursa-major', 'add-module', '--name', 'nodejs', '--stream', '10', + '--priority', '10', '--tag', 'f30-candidate', + '--tag-config-file', self.tag_config_file + ] + + with patch('sys.argv', new=cli_cmd): + ursa_major.cli.main() + + handler = AddModuleHandler.return_value + pos_args, _ = handler.set_args.call_args + args = pos_args[0] + assert args.tag_config_file == self.tag_config_file + + @patch.object(ursa_major.cli, 'AddModuleHandler') + @patch('ursa_major.cli.ConfigParser') + @patch('os.path.exists', return_value=True) + def test_specified_file_does_not_exist(self, exists, ConfigParser, AddModuleHandler): + cli_cmd = [ + 'ursa-major', 'add-module', '--name', 'nodejs', '--stream', '10', + '--priority', '10', '--tag', 'f30-candidate', + '--tag-config-file', '/tmp/xxxxx' + ] + + with patch('sys.argv', new=cli_cmd): + with pytest.raises(SystemExit): + ursa_major.cli.main() diff --git a/ursa_major/cli.py b/ursa_major/cli.py index 215b81b..39b088d 100644 --- a/ursa_major/cli.py +++ b/ursa_major/cli.py @@ -54,11 +54,22 @@ DEFAULT_TAG_CONFIG_FILE = os.path.join(cwd, 'ursa-major.json') def validate_json_file(value): + if not os.path.exists(value): + if value == DEFAULT_TAG_CONFIG_FILE: + raise argparse.ArgumentTypeError( + 'Default tag config file ursa-major.json is not found from ' + 'current directory.\n' + 'Make sure to run ursa-major alongside it, otherwise specify ' + 'the file by option --tag-config-file.') + else: + raise argparse.ArgumentTypeError( + 'Specified tag config file does not exist: {}'.format(value)) + try: with open(value, 'r') as f: json.load(f) except IOError as e: - raise RuntimeError(e) + raise argparse.ArgumentTypeError('Cannot read tag config file: {}'.format(str(e))) except Exception as e: raise argparse.ArgumentTypeError("Invalid json file {}: {}.".format(value, str(e))) return value