def test_pre_command_error(self): """Ensure an BzrCommandError in pre_command aborts the command""" hook_calls = [] def pre_command(cmd): hook_calls.append('pre') # verify that all subclasses of BzrCommandError caught too raise errors.BzrOptionError() def post_command(cmd, e): self.fail('post_command should not be called') def run(cmd): self.fail('command should not be called') self.overrideAttr(builtins.cmd_rocks, 'run', run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("pre_command", pre_command, None) commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.assertRaises(errors.BzrCommandError, commands.run_bzr, [u'rocks']) self.assertEqual(['pre'], hook_calls)
def test_pre_and_post_hooks(self): hook_calls = [] def pre_command(cmd): self.assertEqual([], hook_calls) hook_calls.append('pre') def post_command(cmd): self.assertEqual(['pre', 'run'], hook_calls) hook_calls.append('post') def run(cmd): self.assertEqual(['pre'], hook_calls) hook_calls.append('run') self.overrideAttr(builtins.cmd_rocks, 'run', run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("pre_command", pre_command, None) commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo']) self.assertEqual(['pre', 'run', 'post'], hook_calls)
def get_script(self): commands.install_bzr_command_hooks() dc = DataCollector() data = dc.collect() cg = BashCodeGen(data) res = cg.function() return res
def test_fires_on_get_cmd_object(self): # The get_command(cmd) hook fires when commands are delivered to the # ui. commands.install_bzr_command_hooks() hook_calls = [] class ACommand(commands.Command): __doc__ = """A sample command.""" def get_cmd(cmd_or_None, cmd_name): hook_calls.append(("called", cmd_or_None, cmd_name)) if cmd_name in ("foo", "info"): return ACommand() commands.Command.hooks.install_named_hook("get_command", get_cmd, None) # create a command directly, should not fire cmd = ACommand() self.assertEqual([], hook_calls) # ask by name, should fire and give us our command cmd = commands.get_cmd_object("foo") self.assertEqual([("called", None, "foo")], hook_calls) self.assertIsInstance(cmd, ACommand) del hook_calls[:] # ask by a name that is supplied by a builtin - the hook should still # fire and we still get our object, but we should see the builtin # passed to the hook. cmd = commands.get_cmd_object("info") self.assertIsInstance(cmd, ACommand) self.assertEqual(1, len(hook_calls)) self.assertEqual("info", hook_calls[0][2]) self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
def setUp(self): super(TestRegisterLazy, self).setUp() import bzrlib.tests.fake_command del sys.modules['bzrlib.tests.fake_command'] global lazy_command_imported lazy_command_imported = False commands.install_bzr_command_hooks()
def test_fires_on_get_cmd_object(self): # The get_command(cmd) hook fires when commands are delivered to the # ui. commands.install_bzr_command_hooks() hook_calls = [] class ACommand(commands.Command): __doc__ = """A sample command.""" def get_cmd(cmd_or_None, cmd_name): hook_calls.append(('called', cmd_or_None, cmd_name)) if cmd_name in ('foo', 'info'): return ACommand() commands.Command.hooks.install_named_hook("get_command", get_cmd, None) # create a command directly, should not fire cmd = ACommand() self.assertEqual([], hook_calls) # ask by name, should fire and give us our command cmd = commands.get_cmd_object('foo') self.assertEqual([('called', None, 'foo')], hook_calls) self.assertIsInstance(cmd, ACommand) del hook_calls[:] # ask by a name that is supplied by a builtin - the hook should still # fire and we still get our object, but we should see the builtin # passed to the hook. cmd = commands.get_cmd_object('info') self.assertIsInstance(cmd, ACommand) self.assertEqual(1, len(hook_calls)) self.assertEqual('info', hook_calls[0][2]) self.assertIsInstance(hook_calls[0][1], builtins.cmd_info)
def test_invoked_as(self): """The command object knows the actual name used to invoke it.""" commands.install_bzr_command_hooks() commands._register_builtin_commands() # get one from the real get_cmd_object. c = commands.get_cmd_object("ci") self.assertIsInstance(c, builtins.cmd_commit) self.assertEquals(c.invoked_as, "ci")
def setUp(self): tests.TestCase.setUp(self) import bzrlib.tests.fake_command del sys.modules["bzrlib.tests.fake_command"] global lazy_command_imported lazy_command_imported = False commands.install_bzr_command_hooks()
def test_invoked_as(self): """The command object knows the actual name used to invoke it.""" commands.install_bzr_command_hooks() commands._register_builtin_commands() # get one from the real get_cmd_object. c = commands.get_cmd_object('ci') self.assertIsInstance(c, builtins.cmd_commit) self.assertEqual(c.invoked_as, 'ci')
def get_builtin_command_options(self): g = [] commands.install_bzr_command_hooks() for cmd_name in sorted(commands.builtin_command_names()): cmd = commands.get_cmd_object(cmd_name) for opt_name, opt in sorted(cmd.options().items()): g.append((cmd_name, opt)) self.assertTrue(g) return g
def get_builtin_command_options(self): g = [] commands.install_bzr_command_hooks() for cmd_name in sorted(commands.builtin_command_names()): cmd = commands.get_cmd_object(cmd_name) for opt_name, opt in sorted(cmd.options().items()): g.append((cmd_name, opt)) self.assert_(g) return g
def main(argv): parser = OptionParser(usage="""%prog [options] OUTPUT_FORMAT Available OUTPUT_FORMAT: man man page rstx man page in ReStructuredText format bash_completion bash completion script""") parser.add_option("-s", "--show-filename", action="store_true", dest="show_filename", default=False, help="print default filename on stdout") parser.add_option("-o", "--output", dest="filename", metavar="FILE", help="write output to FILE") parser.add_option("-b", "--bzr-name", dest="bzr_name", default="bzr", metavar="EXEC_NAME", help="name of bzr executable") parser.add_option("-e", "--examples", action="callback", callback=print_extended_help, help="Examples of ways to call generate_doc") (options, args) = parser.parse_args(argv) if len(args) != 2: parser.print_help() sys.exit(1) with bzrlib.initialize(): commands.install_bzr_command_hooks() infogen_type = args[1] infogen_mod = doc_generate.get_module(infogen_type) if options.filename: outfilename = options.filename else: outfilename = infogen_mod.get_filename(options) if outfilename == "-": outfile = sys.stdout else: outfile = open(outfilename, "w") if options.show_filename and (outfilename != "-"): sys.stdout.write(outfilename) sys.stdout.write('\n') infogen_mod.infogen(options, outfile)
def main(argv): parser = OptionParser(usage="""%prog [options] OUTPUT_FORMAT Available OUTPUT_FORMAT: man man page rstx man page in ReStructuredText format bash_completion bash completion script""") parser.add_option("-s", "--show-filename", action="store_true", dest="show_filename", default=False, help="print default filename on stdout") parser.add_option("-o", "--output", dest="filename", metavar="FILE", help="write output to FILE") parser.add_option("-b", "--bzr-name", dest="bzr_name", default="bzr", metavar="EXEC_NAME", help="name of bzr executable") parser.add_option("-e", "--examples", action="callback", callback=print_extended_help, help="Examples of ways to call generate_doc") (options, args) = parser.parse_args(argv) if len(args) != 2: parser.print_help() sys.exit(1) with bzrlib.initialize(): commands.install_bzr_command_hooks() infogen_type = args[1] infogen_mod = doc_generate.get_module(infogen_type) if options.filename: outfilename = options.filename else: outfilename = infogen_mod.get_filename(options) if outfilename == "-": outfile = sys.stdout else: outfile = open(outfilename,"w") if options.show_filename and (outfilename != "-"): sys.stdout.write(outfilename) sys.stdout.write('\n') infogen_mod.infogen(options, outfile)
def test_post_hook_provided_exception(self): hook_calls = [] def post_command(cmd): hook_calls.append("post") def run(cmd): hook_calls.append("run") raise self.TestError() self.overrideAttr(builtins.cmd_rocks, "run", run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.assertRaises(self.TestError, commands.run_bzr, [u"rocks"]) self.assertEqual(["run", "post"], hook_calls)
def test_fires_on_all_command_names(self): # The list_commands() hook fires when all_command_names() is invoked. hook_calls = [] commands.install_bzr_command_hooks() def list_my_commands(cmd_names): hook_calls.append("called") cmd_names.update(["foo", "bar"]) return cmd_names commands.Command.hooks.install_named_hook("list_commands", list_my_commands, None) # Get a command, which should not trigger the hook. cmd = commands.get_cmd_object("info") self.assertEqual([], hook_calls) # Get all command classes (for docs and shell completion). cmds = list(commands.all_command_names()) self.assertEqual(["called"], hook_calls) self.assertSubset(["foo", "bar"], cmds)
def test_fires_on_get_cmd_object(self): # The get_missing_command(cmd) hook fires when commands are delivered to the # ui. self.hook_missing() # create a command directly, should not fire self.cmd = self.ACommand() self.assertEqual([], self.hook_calls) # ask by name, should fire and give us our command cmd = commands.get_cmd_object('foo') self.assertEqual([('called', 'foo')], self.hook_calls) self.assertIsInstance(cmd, self.ACommand) del self.hook_calls[:] # ask by a name that is supplied by a builtin - the hook should not # fire and we still get our object. commands.install_bzr_command_hooks() cmd = commands.get_cmd_object('info') self.assertNotEqual(None, cmd) self.assertEqual(0, len(self.hook_calls))
def test_fires_on_get_cmd_object(self): # The get_missing_command(cmd) hook fires when commands are delivered to the # ui. self.hook_missing() # create a command directly, should not fire self.cmd = self.ACommand() self.assertEqual([], self.hook_calls) # ask by name, should fire and give us our command cmd = commands.get_cmd_object("foo") self.assertEqual([("called", "foo")], self.hook_calls) self.assertIsInstance(cmd, self.ACommand) del self.hook_calls[:] # ask by a name that is supplied by a builtin - the hook should not # fire and we still get our object. commands.install_bzr_command_hooks() cmd = commands.get_cmd_object("info") self.assertNotEqual(None, cmd) self.assertEqual(0, len(self.hook_calls))
def test_post_hook_provided_exception(self): hook_calls = [] def post_command(cmd): hook_calls.append('post') def run(cmd): hook_calls.append('run') raise self.TestError() self.overrideAttr(builtins.cmd_rocks, 'run', run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.assertRaises(self.TestError, commands.run_bzr, [u'rocks']) self.assertEqual(['run', 'post'], hook_calls)
def test_fires_on_all_command_names(self): # The list_commands() hook fires when all_command_names() is invoked. hook_calls = [] commands.install_bzr_command_hooks() def list_my_commands(cmd_names): hook_calls.append('called') cmd_names.update(['foo', 'bar']) return cmd_names commands.Command.hooks.install_named_hook("list_commands", list_my_commands, None) # Get a command, which should not trigger the hook. cmd = commands.get_cmd_object('info') self.assertEqual([], hook_calls) # Get all command classes (for docs and shell completion). cmds = list(commands.all_command_names()) self.assertEqual(['called'], hook_calls) self.assertSubset(['foo', 'bar'], cmds)
def test_fires_on_get_cmd_object(self): # The extend_command(cmd) hook fires when commands are delivered to the # ui, not simply at registration (because lazy registered plugin # commands are registered). # when they are simply created. hook_calls = [] commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("extend_command", hook_calls.append, None) # create a command, should not fire class cmd_test_extend_command_hook(commands.Command): __doc__ = """A sample command.""" self.assertEqual([], hook_calls) # -- as a builtin # register the command class, should not fire try: commands.builtin_command_registry.register( cmd_test_extend_command_hook) self.assertEqual([], hook_calls) # and ask for the object, should fire cmd = commands.get_cmd_object('test-extend-command-hook') # For resilience - to ensure all code paths hit it - we # fire on everything returned in the 'cmd_dict', which is currently # all known commands, so assert that cmd is in hook_calls self.assertSubset([cmd], hook_calls) del hook_calls[:] finally: commands.builtin_command_registry.remove( 'test-extend-command-hook') # -- as a plugin lazy registration try: # register the command class, should not fire commands.plugin_cmds.register_lazy('cmd_fake', [], 'bzrlib.tests.fake_command') self.assertEqual([], hook_calls) # and ask for the object, should fire cmd = commands.get_cmd_object('fake') self.assertEqual([cmd], hook_calls) finally: commands.plugin_cmds.remove('fake')
def test_pre_and_post_hooks(self): hook_calls = [] def pre_command(cmd): self.assertEqual([], hook_calls) hook_calls.append("pre") def post_command(cmd): self.assertEqual(["pre", "run"], hook_calls) hook_calls.append("post") def run(cmd): self.assertEqual(["pre"], hook_calls) hook_calls.append("run") self.overrideAttr(builtins.cmd_rocks, "run", run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("pre_command", pre_command, None) commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.run_bzr(["rocks", "-Oxx=12", "-Oyy=foo"]) self.assertEqual(["pre", "run", "post"], hook_calls)
def test_fires_on_get_cmd_object(self): # The extend_command(cmd) hook fires when commands are delivered to the # ui, not simply at registration (because lazy registered plugin # commands are registered). # when they are simply created. hook_calls = [] commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("extend_command", hook_calls.append, None) # create a command, should not fire class cmd_test_extend_command_hook(commands.Command): __doc__ = """A sample command.""" self.assertEqual([], hook_calls) # -- as a builtin # register the command class, should not fire try: commands.builtin_command_registry.register(cmd_test_extend_command_hook) self.assertEqual([], hook_calls) # and ask for the object, should fire cmd = commands.get_cmd_object("test-extend-command-hook") # For resilience - to ensure all code paths hit it - we # fire on everything returned in the 'cmd_dict', which is currently # all known commands, so assert that cmd is in hook_calls self.assertSubset([cmd], hook_calls) del hook_calls[:] finally: commands.builtin_command_registry.remove("test-extend-command-hook") # -- as a plugin lazy registration try: # register the command class, should not fire commands.plugin_cmds.register_lazy("cmd_fake", [], "bzrlib.tests.fake_command") self.assertEqual([], hook_calls) # and ask for the object, should fire cmd = commands.get_cmd_object("fake") self.assertEqual([cmd], hook_calls) finally: commands.plugin_cmds.remove("fake")
def test_pre_command_error(self): """Ensure an BzrCommandError in pre_command aborts the command""" hook_calls = [] def pre_command(cmd): hook_calls.append("pre") # verify that all subclasses of BzrCommandError caught too raise errors.BzrOptionError() def post_command(cmd, e): self.fail("post_command should not be called") def run(cmd): self.fail("command should not be called") self.overrideAttr(builtins.cmd_rocks, "run", run) commands.install_bzr_command_hooks() commands.Command.hooks.install_named_hook("pre_command", pre_command, None) commands.Command.hooks.install_named_hook("post_command", post_command, None) self.assertEqual([], hook_calls) self.assertRaises(errors.BzrCommandError, commands.run_bzr, [u"rocks"]) self.assertEqual(["pre"], hook_calls)
def setUp(self): super(TestHelp, self).setUp() commands.install_bzr_command_hooks()
else: values.append(value) parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]") parser.add_option("--function-name", "-f", metavar="NAME", help="Name of the generated function (default: _bzr)") parser.add_option("--function-only", "-o", action="store_true", help="Generate only the shell function, don't enable it") parser.add_option("--debug", action="store_true", help=optparse.SUPPRESS_HELP) parser.add_option("--no-plugins", action="store_true", help="Don't load any bzr plugins") parser.add_option("--plugin", metavar="NAME", type="string", dest="selected_plugins", default=[], action="callback", callback=plugin_callback, help="Enable completions for the selected plugin" + " (default: all plugins)") (opts, args) = parser.parse_args() if args: parser.error("script does not take positional arguments") kwargs = dict() for name, value in opts.__dict__.iteritems(): if value is not None: kwargs[name] = value locale.setlocale(locale.LC_ALL, '') if not kwargs.get('no_plugins', False): plugin.load_plugins() commands.install_bzr_command_hooks() bash_completion_function(sys.stdout, **kwargs)
def setUp(self): tests.TestCase.setUp(self) commands.install_bzr_command_hooks()
def setUp(self): super(TestDataCollector, self).setUp() commands.install_bzr_command_hooks()
action="store_true", help="Generate only the shell function, don't enable it") parser.add_option("--debug", action="store_true", help=optparse.SUPPRESS_HELP) parser.add_option("--no-plugins", action="store_true", help="Don't load any bzr plugins") parser.add_option("--plugin", metavar="NAME", type="string", dest="selected_plugins", default=[], action="callback", callback=plugin_callback, help="Enable completions for the selected plugin" + " (default: all plugins)") (opts, args) = parser.parse_args() if args: parser.error("script does not take positional arguments") kwargs = dict() for name, value in opts.__dict__.iteritems(): if value is not None: kwargs[name] = value locale.setlocale(locale.LC_ALL, '') if not kwargs.get('no_plugins', False): plugin.load_plugins() commands.install_bzr_command_hooks() bash_completion_function(sys.stdout, **kwargs)