~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

  • Committer: Vincent Ladeuil
  • Date: 2008-09-11 19:36:38 UTC
  • mfrom: (3703 +trunk)
  • mto: (3705.1.1 trunk2)
  • mto: This revision was merged to the branch mainline in revision 3708.
  • Revision ID: v.ladeuil+lp@free.fr-20080911193638-wtjyc1kcmacc6t1f
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import re
18
18
 
42
42
        # to cmd_commit, when they are meant to be about option parsing in
43
43
        # general.
44
44
        self.assertEqual(parse_args(cmd_commit(), ['--help']),
45
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'help': True}))
 
45
           ([], {'exclude': [], 'fixes': [], 'help': True}))
46
46
        self.assertEqual(parse_args(cmd_commit(), ['--message=biter']),
47
 
           ([], {'author': [], 'exclude': [], 'fixes': [], 'message': 'biter'}))
 
47
           ([], {'exclude': [], 'fixes': [], 'message': 'biter'}))
48
48
 
49
49
    def test_no_more_opts(self):
50
50
        """Terminated options"""
51
51
        self.assertEqual(parse_args(cmd_commit(), ['--', '-file-with-dashes']),
52
 
                          (['-file-with-dashes'], {'author': [], 'exclude': [], 'fixes': []}))
 
52
                          (['-file-with-dashes'], {'exclude': [], 'fixes': []}))
53
53
 
54
54
    def test_option_help(self):
55
55
        """Options have help strings."""
102
102
        self.assertRaises(errors.BzrCommandError, self.parse, options,
103
103
                          ['--no-number'])
104
104
 
105
 
    def test_is_hidden(self):
106
 
        self.assertTrue(option.Option('foo', hidden=True).is_hidden('foo'))
107
 
        self.assertFalse(option.Option('foo', hidden=False).is_hidden('foo'))
108
 
 
109
105
    def test_registry_conversion(self):
110
106
        registry = bzrdir.BzrDirFormatRegistry()
111
107
        registry.register_metadir('one', 'RepositoryFormat7', 'one help')
155
151
        self.assertIsInstance(opts.format.repository_format,
156
152
                              knitrepo.RepositoryFormatKnit1)
157
153
 
158
 
    def test_lazy_registry(self):
159
 
        options = [option.RegistryOption('format', '',
160
 
                   lazy_registry=('bzrlib.bzrdir','format_registry'),
161
 
                   converter=str)]
162
 
        opts, args = self.parse(options, ['--format', 'knit'])
163
 
        self.assertEqual({'format': 'knit'}, opts)
164
 
        self.assertRaises(
165
 
            errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
166
 
 
167
154
    def test_from_kwargs(self):
168
155
        my_option = option.RegistryOption.from_kwargs('my-option',
169
156
            help='test option', short='be short', be_long='go long')
318
305
        self.assertEqual('hello', name)
319
306
        self.assertEqual([], value)
320
307
 
321
 
    def test_list_option_param_name(self):
322
 
        """Test list options can have their param_name set."""
323
 
        options = [option.ListOption('hello', type=str, param_name='greeting')]
324
 
        opts, args = self.parse(
325
 
            options, ['--hello=world', '--hello=sailor'])
326
 
        self.assertEqual(['world', 'sailor'], opts.greeting)
327
 
 
328
308
 
329
309
class TestOptionDefinitions(TestCase):
330
310
    """Tests for options in the Bazaar codebase."""
331
311
 
332
312
    def get_builtin_command_options(self):
333
313
        g = []
334
 
        for cmd_name in sorted(commands.all_command_names()):
335
 
            cmd = commands.get_cmd_object(cmd_name)
 
314
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
315
            cmd = cmd_class()
336
316
            for opt_name, opt in sorted(cmd.options().items()):
337
317
                g.append((cmd_name, opt))
338
318
        return g
345
325
        g = dict(option.Option.OPTIONS.items())
346
326
        used_globals = {}
347
327
        msgs = []
348
 
        for cmd_name in sorted(commands.all_command_names()):
349
 
            cmd = commands.get_cmd_object(cmd_name)
350
 
            for option_or_name in sorted(cmd.takes_options):
 
328
        for cmd_name, cmd_class in sorted(commands.get_all_cmds()):
 
329
            for option_or_name in sorted(cmd_class.takes_options):
351
330
                if not isinstance(option_or_name, basestring):
352
331
                    self.assertIsInstance(option_or_name, option.Option)
353
332
                elif not option_or_name in g:
354
333
                    msgs.append("apparent reference to undefined "
355
334
                        "global option %r from %r"
356
 
                        % (option_or_name, cmd))
 
335
                        % (option_or_name, cmd_class))
357
336
                else:
358
337
                    used_globals.setdefault(option_or_name, []).append(cmd_name)
359
338
        unused_globals = set(g.keys()) - set(used_globals.keys())
374
353
        # period and be all on a single line, because the display code will
375
354
        # wrap it.
376
355
        option_re = re.compile(r'^[A-Z][^\n]+\.$')
377
 
        for scope, opt in self.get_builtin_command_options():
378
 
            if not opt.help:
379
 
                msgs.append('%-16s %-16s %s' %
380
 
                       ((scope or 'GLOBAL'), opt.name, 'NO HELP'))
381
 
            elif not option_re.match(opt.help):
382
 
                msgs.append('%-16s %-16s %s' %
383
 
                        ((scope or 'GLOBAL'), opt.name, opt.help))
 
356
        for scope, option in self.get_builtin_command_options():
 
357
            if not option.help:
 
358
                msgs.append('%-16s %-16s %s' %
 
359
                       ((scope or 'GLOBAL'), option.name, 'NO HELP'))
 
360
            elif not option_re.match(option.help):
 
361
                msgs.append('%-16s %-16s %s' %
 
362
                        ((scope or 'GLOBAL'), option.name, option.help))
384
363
        if msgs:
385
364
            self.fail("The following options don't match the style guide:\n"
386
365
                    + '\n'.join(msgs))