Merge lp:~elopio/snapcraft/fix1477638-format_strings-2 into lp:~snappy-dev/snapcraft/core

Proposed by Leo Arias
Status: Merged
Approved by: Leo Arias
Approved revision: 117
Merged at revision: 112
Proposed branch: lp:~elopio/snapcraft/fix1477638-format_strings-2
Merge into: lp:~snappy-dev/snapcraft/core
Prerequisite: lp:~elopio/snapcraft/fix1477638-format_strings1
Diff against target: 163 lines (+66/-29)
2 files modified
snapcraft/plugin.py (+6/-7)
snapcraft/tests/test_plugin.py (+60/-22)
To merge this branch: bzr merge lp:~elopio/snapcraft/fix1477638-format_strings-2
Reviewer Review Type Date Requested Status
Michael Terry (community) Approve
Review via email: mp+265717@code.launchpad.net

Commit message

Use the format string in plugin.py

To post a comment you must log in.
113. By Leo Arias

Prettify the code.

114. By Leo Arias

Typo

115. By Leo Arias

Use the format method in load_plugin.

116. By Leo Arias

Removed the !r as suggested by mterry

Revision history for this message
Michael Terry (mterry) wrote :

I'm fine with content, but you have a conflict with trunk

review: Approve
117. By Leo Arias

Merged with trunk.

Revision history for this message
Leo Arias (elopio) wrote :

There's no conflict anymore, so I'll top approve.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'snapcraft/plugin.py'
--- snapcraft/plugin.py 2015-07-23 17:19:25 +0000
+++ snapcraft/plugin.py 2015-07-29 15:07:56 +0000
@@ -69,14 +69,14 @@
69 # only set to valid if it loads without PluginError69 # only set to valid if it loads without PluginError
70 self.part_names.append(part_name)70 self.part_names.append(part_name)
71 self.valid = True71 self.valid = True
72 except PluginError:72 except PluginError as e:
73 logger.error(str(e))
73 return74 return
7475
75 def _load_config(self, name):76 def _load_config(self, name):
76 configPath = os.path.join(plugindir(name), name + ".yaml")77 configPath = os.path.join(plugindir(name), name + ".yaml")
77 if not os.path.exists(configPath):78 if not os.path.exists(configPath):
78 logger.error("Unknown plugin %s" % name)79 raise PluginError('Unknown plugin: {}'.format(name))
79 raise PluginError()
80 with open(configPath, 'r') as fp:80 with open(configPath, 'r') as fp:
81 self.config = yaml.load(fp) or {}81 self.config = yaml.load(fp) or {}
8282
@@ -92,8 +92,7 @@
92 setattr(options, attrname, properties[opt])92 setattr(options, attrname, properties[opt])
93 else:93 else:
94 if opt_parameters.get('required', False):94 if opt_parameters.get('required', False):
95 logger.error("Required field %s missing on part %s" % (opt, name))95 raise PluginError('Required field {} missing on part {}'.format(opt, name))
96 raise PluginError()
97 setattr(options, attrname, None)96 setattr(options, attrname, None)
9897
99 return options98 return options
@@ -230,7 +229,7 @@
230 # validate229 # validate
231 for d in includes + excludes:230 for d in includes + excludes:
232 if os.path.isabs(d):231 if os.path.isabs(d):
233 raise PluginError("path '%s' must be relative" % d)232 raise PluginError("path '{}' must be relative".format(d))
234233
235 sourceFiles = set()234 sourceFiles = set()
236 for root, dirs, files in os.walk(self.installdir):235 for root, dirs, files in os.walk(self.installdir):
@@ -280,6 +279,6 @@
280def load_plugin(part_name, plugin_name, properties={}, load_code=True):279def load_plugin(part_name, plugin_name, properties={}, load_code=True):
281 part = PluginHandler(plugin_name, part_name, properties, load_code=load_code)280 part = PluginHandler(plugin_name, part_name, properties, load_code=load_code)
282 if not part.is_valid():281 if not part.is_valid():
283 logger.error("Could not load part %s" % plugin_name)282 logger.error('Could not load part {}'.format(plugin_name))
284 sys.exit(1)283 sys.exit(1)
285 return part284 return part
286285
=== modified file 'snapcraft/tests/test_plugin.py'
--- snapcraft/tests/test_plugin.py 2015-07-23 14:23:29 +0000
+++ snapcraft/tests/test_plugin.py 2015-07-29 15:07:56 +0000
@@ -25,15 +25,31 @@
25 patch,25 patch,
26)26)
2727
28import snapcraft.tests.mock_plugin28from snapcraft import (
29from snapcraft.plugin import PluginHandler, PluginError29 plugin,
30from snapcraft.tests import TestCase30 tests
3131)
3232from snapcraft.tests import mock_plugin
33class TestPlugin(TestCase):33
3434
35 def get_test_plugin(self):35class TestPlugin(tests.TestCase):
36 return PluginHandler('mock', 'mock-part', {}, load_config=False, load_code=False)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)
44
45 def test_init_unknown_plugin_must_log_error(self):
46 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
47 self.useFixture(fake_logger)
48
49 self.get_test_plugin('test_unexisting_name', load_config=True)
50
51 self.assertEqual(
52 'Unknown plugin: test_unexisting_name\n', fake_logger.output)
3753
38 def test_is_dirty(self):54 def test_is_dirty(self):
39 p = self.get_test_plugin()55 p = self.get_test_plugin()
@@ -100,8 +116,8 @@
100 fake_logger = fixtures.FakeLogger(level=logging.INFO)116 fake_logger = fixtures.FakeLogger(level=logging.INFO)
101 self.useFixture(fake_logger)117 self.useFixture(fake_logger)
102118
103 plugin = self.get_test_plugin()119 p = self.get_test_plugin()
104 plugin.notify_stage('test stage')120 p.notify_stage('test stage')
105121
106 self.assertEqual('test stage mock-part\n', fake_logger.output)122 self.assertEqual('test stage mock-part\n', fake_logger.output)
107123
@@ -111,9 +127,9 @@
111 # called with the name only and sys.path set127 # called with the name only and sys.path set
112 self.assertEqual(module_name, "x-mock")128 self.assertEqual(module_name, "x-mock")
113 self.assertTrue(sys.path[0].endswith("parts/plugins"))129 self.assertTrue(sys.path[0].endswith("parts/plugins"))
114 return snapcraft.tests.mock_plugin130 return mock_plugin
115 with patch("importlib.import_module", side_effect=mock_import_modules):131 with patch("importlib.import_module", side_effect=mock_import_modules):
116 PluginHandler(132 plugin.PluginHandler(
117 "x-mock", "mock-part", {}, load_config=False, load_code=True)133 "x-mock", "mock-part", {}, load_config=False, load_code=True)
118 # sys.path is cleaned afterwards134 # sys.path is cleaned afterwards
119 self.assertFalse(sys.path[0].endswith("parts/plugins"))135 self.assertFalse(sys.path[0].endswith("parts/plugins"))
@@ -123,15 +139,37 @@
123 def mock_import_modules(module_name):139 def mock_import_modules(module_name):
124 # called with the full snapcraft path140 # called with the full snapcraft path
125 self.assertEqual(module_name, "snapcraft.plugins.mock")141 self.assertEqual(module_name, "snapcraft.plugins.mock")
126 return snapcraft.tests.mock_plugin142 return mock_plugin
127 with patch("importlib.import_module", side_effect=mock_import_modules):143 with patch("importlib.import_module", side_effect=mock_import_modules):
128 PluginHandler(144 plugin.PluginHandler(
129 "mock", "mock-part", {}, load_config=False, load_code=True)145 "mock", "mock-part", {}, load_config=False, load_code=True)
130146
131 def test_collect_snap_files_with_abs_path_raises(self):147 def test_collect_snap_files_with_absolute_includes_must_raise_error(self):
132 # ensure that absolute path raise an error148 p = self.get_test_plugin()
133 # (os.path.join will throw an error otherwise)149 with self.assertRaises(plugin.PluginError) as raised:
134 with patch("importlib.import_module", return_value=snapcraft.tests.mock_plugin):150 p.collect_snap_files(includes=['rel', '/abs/include'], excludes=[])
135 p = PluginHandler("mock", "mock-part", {}, load_config=False)151
136 with self.assertRaises(PluginError):152 self.assertEqual(
137 p.collect_snap_files(['*'], ['/1'])153 "path '/abs/include' must be relative", str(raised.exception))
154
155 def test_collect_snap_files_with_absolute_excludes_must_raise_error(self):
156 p = self.get_test_plugin()
157 with self.assertRaises(plugin.PluginError) as raised:
158 p.collect_snap_files(includes=[], excludes=['rel', '/abs/exclude'])
159
160 self.assertEqual(
161 "path '/abs/exclude' must be relative", str(raised.exception))
162
163 def test_load_plugin_with_invalid_part_must_exit_with_error(self):
164 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
165 self.useFixture(fake_logger)
166
167 with self.assertRaises(SystemExit) as raised:
168 plugin.load_plugin(
169 'dummy-part', 'test_unexisting_name', load_code=False)
170
171 self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
172 self.assertEqual(
173 'Unknown plugin: test_unexisting_name\n'
174 'Could not load part test_unexisting_name\n',
175 fake_logger.output)

Subscribers

People subscribed via source and target branches

to all changes: