~dobey/tarmac/plugins-tests

« back to all changes in this revision

Viewing changes to tarmac/tests/test_commands.py

  • Committer: Tarmac
  • Author(s): Rodney Dawes
  • Date: 2010-08-14 02:42:22 UTC
  • mfrom: (336.1.2 fake-config)
  • Revision ID: paul@eventuallyanyway.com-20100814024222-x95iwbfc1j9ycrm6
A lot of cleanups and fixes for configuration passing and testing
Remove unsued imports and clean up some other pylint warnings
Fix the TARMAC_PLUGIN_PATHS to also get TARMAC_PLUGIN_PATH from env

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    def run(self, *args, **kwargs):
18
18
        return
19
19
 
 
20
 
20
21
class FakeLaunchpad(object):
21
22
    '''Fake Launchpad object for testing.'''
22
23
 
23
24
    def __init__(self, config=None, *args, **kwargs):
24
 
        if not config:
25
 
            raise Exception('No config provided.')
26
 
        else:
27
 
            self.config = config
28
 
 
29
 
        for section in self.config.sections():
30
 
            if section == 'Tarmac':
31
 
                continue
32
 
            else:
33
 
                print 'Merging %(branch)s' % {'branch' : section}
 
25
        """Fake Launchpad object."""
34
26
 
35
27
 
36
28
class TestCommand(TarmacTestCase):
37
29
    '''Test for tarmac.bin.commands.Command.'''
38
30
 
39
31
    def test__init__(self):
40
 
        registry = CommandRegistry()
 
32
        registry = CommandRegistry(config=self.config)
41
33
        command_name = u'test'
42
34
        command = commands.TarmacCommand(registry)
43
35
        command.NAME = command_name
45
37
        self.assertTrue(isinstance(command.config, TarmacConfig))
46
38
 
47
39
    def test_run(self):
48
 
        registry = CommandRegistry()
 
40
        registry = CommandRegistry(config=self.config)
49
41
        command = commands.TarmacCommand(registry)
 
42
        command.run()
50
43
 
51
44
 
52
45
class TestAuthCommand(TarmacTestCase):
53
46
    '''Test for tarmac.bin.command.cmd_auth.'''
54
47
 
55
 
    NEEDS_SAMPLE_DATA = True
56
 
 
57
48
    # XXX: rockstar - 10 Jan 2010 - How do I test this with the OAuth request,
58
49
    # etc?
59
50
    #def test_run(self):
72
63
 
73
64
    def test_run_already_authenticated(self):
74
65
        '''If the user has already been authenticated, don't try again.'''
75
 
        registry = CommandRegistry()
 
66
        registry = CommandRegistry(config=self.config)
76
67
        registry.register_command('authenticate', commands.cmd_authenticate)
77
68
        command = registry._get_command(commands.cmd_authenticate,
78
69
                                        'authenticate')
90
81
        old_stdout = sys.stdout
91
82
        sys.stdout = tmp_stdout
92
83
 
93
 
        registry = CommandRegistry()
 
84
        registry = CommandRegistry(config=self.config)
94
85
        registry.register_command('foo', FakeCommand)
95
86
 
96
87
        registry.register_command('help', commands.cmd_help)
103
94
 
104
95
        sys.stdout = old_stdout
105
96
 
 
97
 
106
98
class TestMergeCommand(TarmacTestCase):
107
99
 
108
 
    NEEDS_SAMPLE_DATA = True
109
 
 
110
100
    def test_run(self):
111
101
        tmp_stdout = StringIO()
112
102
        old_stdout = sys.stdout
113
103
        sys.stdout = tmp_stdout
114
104
 
115
 
        config = TarmacConfig()
116
 
        self.write_config_file(config)
117
 
 
118
 
        registry = CommandRegistry()
 
105
        def _do_merges(branch_url, *args, **kwargs):
 
106
            """Just checking."""
 
107
            print 'Merging %s' % branch_url
 
108
 
 
109
        branches = ['lp:foo', 'lp:bar', 'lp:baz']
 
110
        for branch in branches:
 
111
            self.config.add_section(branch)
 
112
 
 
113
        registry = CommandRegistry(config=self.config)
119
114
        registry.register_command('merge', commands.cmd_merge)
 
115
 
120
116
        command = registry._get_command(commands.cmd_merge, 'merge')
121
 
        command.run(launchpad=FakeLaunchpad(config=config))
122
 
        self.assertEqual(
123
 
            tmp_stdout.getvalue(),
124
 
            'Merging lp:~tarmac/tarmac/tarmac\n'
125
 
            'Merging lp:~tarmac/tarmac/tarmac3\n'
126
 
            'Merging lp:~tarmac/tarmac/tarmac2\n'
127
 
            'Merging lp:~tarmac/tarmac/tarmac4\n')
 
117
        command._do_merges = _do_merges
 
118
        command.run(launchpad=FakeLaunchpad())
 
119
        self.assertEqual(tmp_stdout.getvalue(),
 
120
                         '\n'.join(
 
121
                ['Merging %s' % b for b in  sorted(branches, reverse=True)]
 
122
                ) + '\n')
 
123
 
 
124
        for branch in branches:
 
125
            self.config.remove_section(branch)
128
126
 
129
127
        sys.stdout = old_stdout