def test_unexisting_command(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.commands_json) temp_file.flush() with self.assertRaisesRegex(PearunException, r'Unrecognized command.*'): pearunfile = Pearunfile(temp_file.name) pearunfile.resolve_command('command_c')
def test_known_command(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.commands_json) temp_file.flush() command_name = 'command_a' pearunfile = Pearunfile(temp_file.name) command = pearunfile.resolve_command(command_name) self.assertEqual(command, pearunfile[command_name]) self.assertEqual(command, self.commands[command_name])
def test_command_with_args(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.commands_json) temp_file.flush() command_name = 'command_b' command_args = ['production', '--fast'] pearunfile = Pearunfile(temp_file.name) expected_command = ' '.join([self.commands[command_name]] + command_args) command = pearunfile.resolve_command(command_name, *command_args) self.assertEqual(command, expected_command)
def test_complex_json(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.complex_json) temp_file.flush() with self.assertRaisesRegex(PearunfileException, r'.*commands are not string values$'): Pearunfile(temp_file.name)
def test_non_string_commands(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.non_string_json) temp_file.flush() with self.assertRaisesRegex(PearunfileException, r'Pearunfile validation failed.*'): Pearunfile(temp_file.name)
def test_invalid_json(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.invalid_json) temp_file.flush() with self.assertRaisesRegex(PearunfileException, r'Pearunfile parsing failed.*'): Pearunfile(temp_file.name)
def test_valid_json(self): with NamedTemporaryFile(mode='w') as temp_file: temp_file.write(self.valid_json) temp_file.flush() pearunfile = Pearunfile(temp_file.name) self.assertDictEqual(pearunfile.commands, { 'aaa': 'bb', 'foo': 'bar', })
def main(**kwargs): if kwargs.get('help', False): _show_help() sys.exit(0) file_path = kwargs.get('file') try: pearunfile = Pearunfile(file_path) except PearunException as e: print(e, end='\n\n') _show_help() sys.exit(1) except PearunfileException as e: print(e) sys.exit(1) if kwargs.get('list', False): _list_commands(pearunfile) sys.exit(0) command_name = kwargs.get('command') args = kwargs.get('args', []) try: command = pearunfile.resolve_command(command_name, *args) except UnspecifiedCommandException: _show_help_and_list_commands(pearunfile) sys.exit(1) except PearunException as e: print(e, end='\n\n') _list_commands(pearunfile) sys.exit(1) cwd = os.path.dirname(file_path) _execute_command(command, cwd)