def test_has_parameter_option(self): input_ = ListInput([('name', 'John'), ('--foo', 'bar')]) self.assertTrue(input_.has_parameter_option('--foo')) self.assertFalse(input_.has_parameter_option('--bar')) input_ = ListInput(['--foo']) self.assertTrue(input_.has_parameter_option('--foo'))
def test_run_returns_integer_exit_code(self): command = SomeCommand() exit_code = command.run(ListInput([]), NullOutput()) self.assertEqual(0, exit_code) command = SomeCommand() command.execute = self.mock().MagicMock(return_value=2.3) exit_code = command.run(ListInput([]), NullOutput()) self.assertEqual(2, exit_code)
def test_parse_arguments(self): input_ = ListInput( [('name', 'foo')], InputDefinition([ InputArgument('name') ]) ) self.assertEqual({'name': 'foo'}, input_.get_arguments())
def test_get_first_argument(self): input_ = ListInput([]) self.assertEqual(None, input_.get_first_argument()) input_ = ListInput([('name', 'John')]) self.assertEqual('John', input_.get_first_argument()) input_ = ListInput([('--foo', 'bar'), ('name', 'John')]) self.assertEqual('John', input_.get_first_argument())
def test_execute_method_needs_to_be_overwridden(self): command = Command('foo') self.assertRaises( NotImplementedError, command.run, ListInput([]), NullOutput() )
def test_run_return_exit_code_one_for_exception_code_zero(self): exception = OSError(0, '') application = Application() application.set_auto_exit(False) application.do_run = self.mock().MagicMock(side_effect=exception) exit_code = application.run(ListInput([]), NullOutput()) self.assertEqual(1, exit_code)
def run(self, input_, options=None): options = options or {} self._input = ListInput(input_) if self._inputs: self._input.set_stream(self._create_stream(self._inputs)) if "interactive" in options: self._input.set_interactive(options["interactive"]) self._output = StreamOutput(io.BytesIO()) if "decorated" in options: self._output.set_decorated(options["decorated"]) else: self._output.set_decorated(False) if "verbosity" in options: self._output.set_verbosity(options["verbosity"]) return self._application.run(self._input, self._output)
def test_adding_already_set_definition_element_data(self): data = [[InputArgument('command', InputArgument.REQUIRED)], [InputOption('quiet', '', InputOption.VALUE_NONE)], [InputOption('query', 'q', InputOption.VALUE_NONE)]] for d in data: application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.register('foo')\ .set_definition(d)\ .set_code(lambda in_, out_: None) input_ = ListInput([('command', 'foo')]) output_ = NullOutput() self.assertRaises(Exception, application.run, input_, output_)
def __init__(self): super().__init__(ListInput([]), NullOutput())
def test_parse_options(self): for iopts, opts, expected in self.provide_options(): input_ = ListInput(iopts, InputDefinition(opts)) self.assertEqual(expected, input_.get_options())
def test_parse_arguments(self): input_ = ListInput([('name', 'foo')], InputDefinition([InputArgument('name')])) self.assertEqual({'name': 'foo'}, input_.get_arguments())