def test_options_overridable(self): base = model.UI(options=[('partial', True), ('other', False)]) cmd = commands.Command(base) base.set_command(cmd) ui = decorator.UI(options={'partial': False}, decorated=base) internal_cmd = commands.Command(ui) ui.set_command(internal_cmd) self.assertEqual(False, ui.options.partial) self.assertEqual(False, ui.options.other)
def test_dash_dash_help_shows_help(self): bytestream = BytesIO() stdout = TextIOWrapper(bytestream, 'utf8', line_buffering=True) stdin = StringIO() stderr = StringIO() ui = cli.UI(['--help'], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.args = [arguments.string.StringArgument('foo')] cmd.name = "bar" # By definition SystemExit is not caught by 'except Exception'. try: ui.set_command(cmd) except SystemExit: exc_info = sys.exc_info() self.assertThat(exc_info, MatchesException(SystemExit(0))) else: self.fail('ui.set_command did not raise') self.assertThat( bytestream.getvalue().decode('utf8'), DocTestMatches( """Usage: run.py bar [options] foo ... A command that can be run... ... -d HERE, --here=HERE... ...""", doctest.ELLIPSIS))
def test_dash_d_sets_here_option(self): stdout = BytesIO() stdin = BytesIO(_b('foo\n')) stderr = BytesIO() ui = cli.UI(['-d', '/nowhere/'], stdin, stdout, stderr) cmd = commands.Command(ui) ui.set_command(cmd) self.assertEqual('/nowhere/', ui.here)
def test_make_result(self): # make_result should return a StreamResult and a summary result. ui = self.ui_factory() ui.set_command(commands.Command(ui)) result, summary = ui.make_result(lambda: None, StubTestCommand()) result.startTestRun() result.status() result.stopTestRun() summary.wasSuccessful()
def test_parse_excess_goes_to_stderr(self): bytestream = BytesIO() stdout = TextIOWrapper(bytestream, 'utf8', line_buffering=True) stdin = StringIO() stderr = StringIO() ui = cli.UI(['one'], stdin, stdout, stderr) cmd = commands.Command(ui) ui.set_command(cmd) self.assertEqual("Unexpected arguments: ['one']\n", stderr.getvalue())
def test_parse_error_goes_to_stderr(self): bytestream = BytesIO() stdout = TextIOWrapper(bytestream, 'utf8', line_buffering=True) stdin = StringIO() stderr = StringIO() ui = cli.UI(['one'], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.args = [arguments.command.CommandArgument('foo')] ui.set_command(cmd) self.assertEqual("Could not find command 'one'.\n", stderr.getvalue())
def test_iter_streams_load_stdin_use_case(self): # A UI can be asked for the streams that a command has indicated it # accepts, which is what load < foo will require. ui = self.ui_factory([('subunit', _b('test: foo\nsuccess: foo\n'))]) cmd = commands.Command(ui) cmd.input_streams = ['subunit+'] ui.set_command(cmd) results = [] for result in ui.iter_streams('subunit'): results.append(result.read()) self.assertEqual([_b('test: foo\nsuccess: foo\n')], results)
def test_stream_comes_from_stdin(self): stdout = BytesIO() stdin = BytesIO(_b('foo\n')) stderr = BytesIO() ui = cli.UI([], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.input_streams = ['subunit'] ui.set_command(cmd) results = [] for stream in ui.iter_streams('subunit'): results.append(stream.read()) self.assertEqual([_b('foo\n')], results)
def test_make_result_previous_run(self): # make_result can take a previous run. ui = self.ui_factory() ui.set_command(commands.Command(ui)) result, summary = ui.make_result( lambda: None, StubTestCommand(), previous_run=memory.Repository().get_failing()) result.startTestRun() result.status() result.stopTestRun() summary.wasSuccessful()
def test_options_on_command_picked_up(self): ui = self.ui_factory(options=[('subunit', True)]) cmd = commands.Command(ui) cmd.options = [ optparse.Option("--subunit", action="store_true", default=False, help="Show output as a subunit stream.") ] ui.set_command(cmd) self.assertEqual(True, ui.options.subunit) # And when not given the default works. ui = self.ui_factory() cmd = commands.Command(ui) cmd.options = [ optparse.Option("--subunit", action="store_true", default=False, help="Show output as a subunit stream.") ] ui.set_command(cmd) self.assertEqual(False, ui.options.subunit)
def test_double_dash_passed_to_arguments(self): class CaptureArg(arguments.AbstractArgument): def _parse_one(self, arg): return arg stdout = BytesIO() stdin = BytesIO() stderr = BytesIO() ui = cli.UI(['one', '--', '--two', 'three'], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.args = [CaptureArg('args', max=None)] ui.set_command(cmd) self.assertEqual({'args': ['one', '--', '--two', 'three']}, ui.arguments)
def test_stream_type_honoured(self): # The CLI UI has only one stdin, so when a command asks for a stream # type it didn't declare, no streams are found. stdout = BytesIO() stdin = BytesIO(_b('foo\n')) stderr = BytesIO() ui = cli.UI([], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.input_streams = ['subunit+', 'interactive?'] ui.set_command(cmd) results = [] for stream in ui.iter_streams('interactive'): results.append(stream.read()) self.assertEqual([], results)
def make_result(self, stream=None, argv=None, filter_tags=None): if stream is None: stream = BytesIO() argv = argv or [] ui = cli.UI(argv, None, stream, None) cmd = commands.Command(ui) cmd.options = [ optparse.Option("--subunit", action="store_true", default=False, help="Display results in subunit format."), ] ui.set_command(cmd) return ui.make_result(lambda: None, StubTestCommand(filter_tags=filter_tags))
def test_parse_options_after_double_dash_are_arguments(self): stdout = BytesIO() stdin = BytesIO() stderr = BytesIO() ui = cli.UI(['one', '--', '--two', 'three'], stdin, stdout, stderr) cmd = commands.Command(ui) cmd.args = [ arguments.string.StringArgument('myargs', max=None), arguments.doubledash.DoubledashArgument(), arguments.string.StringArgument('subargs', max=None) ] ui.set_command(cmd) self.assertEqual( { 'doubledash': ['--'], 'myargs': ['one'], 'subargs': ['--two', 'three'] }, ui.arguments)
def test_options_when_set_at_options(self): ui = self.ui_factory(options=[('quiet', True)]) cmd = commands.Command(ui) ui.set_command(cmd) self.assertEqual(True, ui.options.quiet)
def test_set_command_with_no_name_works(self): # Degrade gracefully if the name attribute has not been set. ui = self.ui_factory() cmd = commands.Command(ui) self.assertEqual(True, ui.set_command(cmd))
def test_args_are_exposed_at_arguments(self): ui = self.ui_factory(args=['load']) cmd = commands.Command(ui) cmd.args = [arguments.command.CommandArgument('foo')] self.assertEqual(True, ui.set_command(cmd)) self.assertEqual({'foo': [load.load]}, ui.arguments)
def test_set_command_checks_args_invalid_arg(self): ui = self.ui_factory(args=['a']) cmd = commands.Command(ui) cmd.args = [arguments.command.CommandArgument('foo')] self.assertEqual(False, ui.set_command(cmd))
def test_set_command_checks_args_unwanted_arg(self): ui = self.ui_factory(args=['foo']) cmd = commands.Command(ui) self.assertEqual(False, ui.set_command(cmd))
def get_test_ui(self): ui = self.ui_factory() cmd = commands.Command(ui) ui.set_command(cmd) return ui
def test_set_command(self): # All ui objects can be given their command. ui = self.ui_factory() cmd = commands.Command(ui) self.assertEqual(True, ui.set_command(cmd))
def test_default_repository_factory(self): cmd = commands.Command(model.UI()) self.assertIsInstance(cmd.repository_factory, file.RepositoryFactory)