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
1=== modified file 'snapcraft/plugin.py'
2--- snapcraft/plugin.py 2015-07-23 17:19:25 +0000
3+++ snapcraft/plugin.py 2015-07-29 15:07:56 +0000
4@@ -69,14 +69,14 @@
5 # only set to valid if it loads without PluginError
6 self.part_names.append(part_name)
7 self.valid = True
8- except PluginError:
9+ except PluginError as e:
10+ logger.error(str(e))
11 return
12
13 def _load_config(self, name):
14 configPath = os.path.join(plugindir(name), name + ".yaml")
15 if not os.path.exists(configPath):
16- logger.error("Unknown plugin %s" % name)
17- raise PluginError()
18+ raise PluginError('Unknown plugin: {}'.format(name))
19 with open(configPath, 'r') as fp:
20 self.config = yaml.load(fp) or {}
21
22@@ -92,8 +92,7 @@
23 setattr(options, attrname, properties[opt])
24 else:
25 if opt_parameters.get('required', False):
26- logger.error("Required field %s missing on part %s" % (opt, name))
27- raise PluginError()
28+ raise PluginError('Required field {} missing on part {}'.format(opt, name))
29 setattr(options, attrname, None)
30
31 return options
32@@ -230,7 +229,7 @@
33 # validate
34 for d in includes + excludes:
35 if os.path.isabs(d):
36- raise PluginError("path '%s' must be relative" % d)
37+ raise PluginError("path '{}' must be relative".format(d))
38
39 sourceFiles = set()
40 for root, dirs, files in os.walk(self.installdir):
41@@ -280,6 +279,6 @@
42 def load_plugin(part_name, plugin_name, properties={}, load_code=True):
43 part = PluginHandler(plugin_name, part_name, properties, load_code=load_code)
44 if not part.is_valid():
45- logger.error("Could not load part %s" % plugin_name)
46+ logger.error('Could not load part {}'.format(plugin_name))
47 sys.exit(1)
48 return part
49
50=== modified file 'snapcraft/tests/test_plugin.py'
51--- snapcraft/tests/test_plugin.py 2015-07-23 14:23:29 +0000
52+++ snapcraft/tests/test_plugin.py 2015-07-29 15:07:56 +0000
53@@ -25,15 +25,31 @@
54 patch,
55 )
56
57-import snapcraft.tests.mock_plugin
58-from snapcraft.plugin import PluginHandler, PluginError
59-from snapcraft.tests import TestCase
60-
61-
62-class TestPlugin(TestCase):
63-
64- def get_test_plugin(self):
65- return PluginHandler('mock', 'mock-part', {}, load_config=False, load_code=False)
66+from snapcraft import (
67+ plugin,
68+ tests
69+)
70+from snapcraft.tests import mock_plugin
71+
72+
73+class TestPlugin(tests.TestCase):
74+
75+ def get_test_plugin(self, name='mock', part_name='mock-part',
76+ properties=None, load_code=False, load_config=False):
77+ if properties is None:
78+ properties = {}
79+ return plugin.PluginHandler(
80+ name, part_name, properties, load_code=load_code,
81+ load_config=load_config)
82+
83+ def test_init_unknown_plugin_must_log_error(self):
84+ fake_logger = fixtures.FakeLogger(level=logging.ERROR)
85+ self.useFixture(fake_logger)
86+
87+ self.get_test_plugin('test_unexisting_name', load_config=True)
88+
89+ self.assertEqual(
90+ 'Unknown plugin: test_unexisting_name\n', fake_logger.output)
91
92 def test_is_dirty(self):
93 p = self.get_test_plugin()
94@@ -100,8 +116,8 @@
95 fake_logger = fixtures.FakeLogger(level=logging.INFO)
96 self.useFixture(fake_logger)
97
98- plugin = self.get_test_plugin()
99- plugin.notify_stage('test stage')
100+ p = self.get_test_plugin()
101+ p.notify_stage('test stage')
102
103 self.assertEqual('test stage mock-part\n', fake_logger.output)
104
105@@ -111,9 +127,9 @@
106 # called with the name only and sys.path set
107 self.assertEqual(module_name, "x-mock")
108 self.assertTrue(sys.path[0].endswith("parts/plugins"))
109- return snapcraft.tests.mock_plugin
110+ return mock_plugin
111 with patch("importlib.import_module", side_effect=mock_import_modules):
112- PluginHandler(
113+ plugin.PluginHandler(
114 "x-mock", "mock-part", {}, load_config=False, load_code=True)
115 # sys.path is cleaned afterwards
116 self.assertFalse(sys.path[0].endswith("parts/plugins"))
117@@ -123,15 +139,37 @@
118 def mock_import_modules(module_name):
119 # called with the full snapcraft path
120 self.assertEqual(module_name, "snapcraft.plugins.mock")
121- return snapcraft.tests.mock_plugin
122+ return mock_plugin
123 with patch("importlib.import_module", side_effect=mock_import_modules):
124- PluginHandler(
125+ plugin.PluginHandler(
126 "mock", "mock-part", {}, load_config=False, load_code=True)
127
128- def test_collect_snap_files_with_abs_path_raises(self):
129- # ensure that absolute path raise an error
130- # (os.path.join will throw an error otherwise)
131- with patch("importlib.import_module", return_value=snapcraft.tests.mock_plugin):
132- p = PluginHandler("mock", "mock-part", {}, load_config=False)
133- with self.assertRaises(PluginError):
134- p.collect_snap_files(['*'], ['/1'])
135+ def test_collect_snap_files_with_absolute_includes_must_raise_error(self):
136+ p = self.get_test_plugin()
137+ with self.assertRaises(plugin.PluginError) as raised:
138+ p.collect_snap_files(includes=['rel', '/abs/include'], excludes=[])
139+
140+ self.assertEqual(
141+ "path '/abs/include' must be relative", str(raised.exception))
142+
143+ def test_collect_snap_files_with_absolute_excludes_must_raise_error(self):
144+ p = self.get_test_plugin()
145+ with self.assertRaises(plugin.PluginError) as raised:
146+ p.collect_snap_files(includes=[], excludes=['rel', '/abs/exclude'])
147+
148+ self.assertEqual(
149+ "path '/abs/exclude' must be relative", str(raised.exception))
150+
151+ def test_load_plugin_with_invalid_part_must_exit_with_error(self):
152+ fake_logger = fixtures.FakeLogger(level=logging.ERROR)
153+ self.useFixture(fake_logger)
154+
155+ with self.assertRaises(SystemExit) as raised:
156+ plugin.load_plugin(
157+ 'dummy-part', 'test_unexisting_name', load_code=False)
158+
159+ self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
160+ self.assertEqual(
161+ 'Unknown plugin: test_unexisting_name\n'
162+ 'Could not load part test_unexisting_name\n',
163+ fake_logger.output)

Subscribers

People subscribed via source and target branches

to all changes: