~jocave/snapcraft/plainbox-provider-plugin

« back to all changes in this revision

Viewing changes to snapcraft/tests/test_plugin.py

  • Committer: Michael Terry
  • Date: 2015-08-06 14:59:07 UTC
  • mfrom: (129 snapcraft)
  • mto: This revision was merged to the branch mainline in revision 132.
  • Revision ID: michael.terry@canonical.com-20150806145907-wzkkzm3mitiewa03
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from snapcraft.tests import mock_plugin
33
33
 
34
34
 
35
 
class TestPlugin(tests.TestCase):
36
 
 
37
 
    def get_test_plugin(self, name='mock', part_name='mock-part',
38
 
                        properties=None, load_code=False, load_config=False):
39
 
        if properties is None:
40
 
            properties = {}
41
 
        return plugin.PluginHandler(
42
 
            name, part_name, properties, load_code=load_code,
43
 
            load_config=load_config)
 
35
def get_test_plugin(name='mock', part_name='mock-part',
 
36
                    properties=None, load_code=False, load_config=False):
 
37
    if properties is None:
 
38
        properties = {}
 
39
    return plugin.PluginHandler(
 
40
        name, part_name, properties, load_code=load_code,
 
41
        load_config=load_config)
 
42
 
 
43
 
 
44
class PluginTestCase(tests.TestCase):
44
45
 
45
46
    def test_init_unknown_plugin_must_log_error(self):
46
47
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
47
48
        self.useFixture(fake_logger)
48
49
 
49
 
        self.get_test_plugin('test_unexisting_name', load_config=True)
 
50
        get_test_plugin('test_unexisting_name', load_config=True)
50
51
 
51
52
        self.assertEqual(
52
53
            'Unknown plugin: test_unexisting_name\n', fake_logger.output)
53
54
 
54
55
    def test_is_dirty(self):
55
 
        p = self.get_test_plugin()
 
56
        p = get_test_plugin()
56
57
        p.statefile = tempfile.NamedTemporaryFile().name
57
58
        self.addCleanup(os.remove, p.statefile)
58
59
        p.code = Mock()
65
66
        self.assertFalse(p.code.pull.called)
66
67
 
67
68
    def test_collect_snap_files(self):
68
 
        p = self.get_test_plugin()
 
69
        p = get_test_plugin()
69
70
 
70
71
        tmpdirObject = tempfile.TemporaryDirectory()
71
72
        self.addCleanup(tmpdirObject.cleanup)
116
117
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
117
118
        self.useFixture(fake_logger)
118
119
 
119
 
        p = self.get_test_plugin()
 
120
        p = get_test_plugin()
120
121
        p.notify_stage('test stage')
121
122
 
122
123
        self.assertEqual('test stage mock-part\n', fake_logger.output)
145
146
                "mock", "mock-part", {}, load_config=False, load_code=True)
146
147
 
147
148
    def test_collect_snap_files_with_absolute_includes_must_raise_error(self):
148
 
        p = self.get_test_plugin()
 
149
        p = get_test_plugin()
149
150
        with self.assertRaises(plugin.PluginError) as raised:
150
151
            p.collect_snap_files(includes=['rel', '/abs/include'], excludes=[])
151
152
 
153
154
            "path '/abs/include' must be relative", str(raised.exception))
154
155
 
155
156
    def test_collect_snap_files_with_absolute_excludes_must_raise_error(self):
156
 
        p = self.get_test_plugin()
 
157
        p = get_test_plugin()
157
158
        with self.assertRaises(plugin.PluginError) as raised:
158
159
            p.collect_snap_files(includes=[], excludes=['rel', '/abs/exclude'])
159
160
 
173
174
            'Unknown plugin: test_unexisting_name\n'
174
175
            'Could not load part test_unexisting_name\n',
175
176
            fake_logger.output)
 
177
 
 
178
 
 
179
class PluginMakedirsTestCase(tests.TestCase):
 
180
 
 
181
    scenarios = [
 
182
        ('existing_dirs', {'make_dirs': True}),
 
183
        ('unexisting_dirs', {'make_dirs': False})
 
184
    ]
 
185
 
 
186
    def get_plugin_dirs(self, part_name):
 
187
        parts_dir = os.path.join(self.path, 'parts')
 
188
        return [
 
189
            os.path.join(parts_dir, part_name, 'src'),
 
190
            os.path.join(parts_dir, part_name, 'build'),
 
191
            os.path.join(parts_dir, part_name, 'install'),
 
192
            os.path.join(self.path, 'stage'),
 
193
            os.path.join(self.path, 'snap')
 
194
        ]
 
195
 
 
196
    def test_makedirs_with_existing_dirs(self):
 
197
        part_name = 'test_part'
 
198
        dirs = self.get_plugin_dirs(part_name)
 
199
        if self.make_dirs:
 
200
            os.makedirs(os.path.join('parts', part_name))
 
201
            for d in dirs:
 
202
                os.mkdir(d)
 
203
 
 
204
        p = get_test_plugin(part_name=part_name)
 
205
        p.makedirs()
 
206
        for d in dirs:
 
207
            self.assertTrue(os.path.exists(d), '{} does not exist'.format(d))