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 run_argv(argv, stdin, stdout, stderr): """Convenience function to run a command with a CLIUI. :param argv: The argv to run the command with. :param stdin: The stdin stream for the command. :param stdout: The stdout stream for the command. :param stderr: The stderr stream for the command. :return: An integer exit code for the command. """ cmd_name = None cmd_args = argv[1:] for arg in argv[1:]: if not arg.startswith('-'): cmd_name = arg break if cmd_name is None: cmd_name = 'help' cmd_args = ['help'] cmd_args.remove(cmd_name) cmdclass = _find_command(cmd_name) from testrepository.ui import cli ui = cli.UI(cmd_args, stdin, stdout, stderr) cmd = cmdclass(ui) result = cmd.execute() if not result: return 0 return result
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_initial_stream(self): # CLITestResult.__init__ does not do anything to the stream it is # given. bytestream = BytesIO() stream = TextIOWrapper(bytestream, 'utf8', line_buffering=True) ui = cli.UI(None, None, None, None) cli.CLITestResult(ui, stream, lambda: None) self.assertEqual(_b(''), bytestream.getvalue())
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_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 get_test_ui_and_cmd(options=(), args=()): stdout = TextIOWrapper(BytesIO(), 'utf8', line_buffering=True) stdin = StringIO() stderr = StringIO() argv = list(args) for option, value in options: # only bool handled so far if value: argv.append('--%s' % option) ui = cli.UI(argv, stdin, stdout, stderr) cmd = run.run(ui) ui.set_command(cmd) return ui, cmd
def test_outputs_error_string(self): try: raise Exception('fooo') except Exception: err_tuple = sys.exc_info() expected = str(err_tuple[1]) + '\n' bytestream = BytesIO() stdout = TextIOWrapper(bytestream, 'utf8', line_buffering=True) stdin = StringIO() stderr = StringIO() ui = cli.UI([], stdin, stdout, stderr) ui.output_error(err_tuple) self.assertThat(stderr.getvalue(), DocTestMatches(expected))
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 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 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 cli_ui_factory(input_streams=None, options=(), args=()): if input_streams and len(input_streams) > 1: # TODO: turn additional streams into argv and simulated files, or # something - however, may need to be cli specific tests at that # point. raise NotImplementedError(cli_ui_factory) stdout = TextIOWrapper(BytesIO(), line_buffering=True) if input_streams: stdin = TextIOWrapper(BytesIO(input_streams[0][1])) else: stdin = TextIOWrapper(BytesIO()) stderr = TextIOWrapper(BytesIO(), line_buffering=True) argv = list(args) for option, value in options: # only bool handled so far if value: argv.append('--%s' % option) return cli.UI(argv, stdin, stdout, stderr)
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_error_enters_pdb_when_TESTR_PDB_set(self): os.environ['TESTR_PDB'] = '1' try: raise Exception('fooo') except Exception: err_tuple = sys.exc_info() expected = dedent("""\ File "...test_cli.py", line ..., in ...pdb_when_TESTR_PDB_set raise Exception('fooo') <BLANKLINE> fooo """) # This should be a BytesIO + Textwrapper, but pdb on 2.7 writes bytes # - this code is the most pragmatic to test on 2.6 and up, and 3.2 and # up. stdout = StringIO() stdin = StringIO(_u('c\n')) stderr = StringIO() ui = cli.UI([], stdin, stdout, stderr) ui.output_error(err_tuple) self.assertThat(stderr.getvalue(), DocTestMatches(expected, doctest.ELLIPSIS))
def test_construct(self): stdout = BytesIO() stdin = BytesIO() stderr = BytesIO() cli.UI([], stdin, stdout, stderr)