Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/plugin_importer/tests/test_plugin_importer.py
1 | import os | 1 | import os | ||
---|---|---|---|---|---|
2 | import pytest | 2 | import pytest | ||
3 | from tempfile import TemporaryDirectory | 3 | from tempfile import TemporaryDirectory | ||
4 | from unittest import TestCase | 4 | from unittest import mock, TestCase | ||
5 | from zipfile import ZipFile | ||||
5 | 6 | | |||
6 | 7 | from .. plugin_importer import ( | |||
7 | from .. plugin_importer import PluginImporter, PluginReadError | 8 | NoPluginsFoundException, | ||
9 | PluginImporter, | ||||
10 | PluginReadError, | ||||
11 | ) | ||||
8 | 12 | | |||
9 | 13 | | |||
10 | class PluginImporterTestCase(TestCase): | 14 | class PluginImporterTestCase(TestCase): | ||
Context not available. | |||||
19 | 23 | | |||
20 | @property | 24 | @property | ||
21 | def zip_filename(self): | 25 | def zip_filename(self): | ||
22 | return os.path.join( | 26 | return os.path.join(self.plugin_dir.name, 'plugin.zip') | ||
23 | self.resources_dir.name, 'plugin.zip') | 27 | | ||
28 | def zip_plugin(self, dirname): | ||||
29 | """Zip a directory into the temporary directory.""" | ||||
30 | testroot = os.path.dirname(os.path.realpath(__file__)) | ||||
31 | src = os.path.join(testroot, 'fixtures', dirname) | ||||
32 | | ||||
33 | with ZipFile(self.zip_filename, 'w') as plugin_zip: | ||||
34 | for root, dirs, files in os.walk(src): | ||||
35 | dirname = root.replace(src, '') | ||||
36 | for filename in files + dirs: | ||||
37 | plugin_zip.write( | ||||
38 | filename=os.path.join(root, filename), | ||||
39 | arcname=os.path.join(dirname, filename)) | ||||
40 | | ||||
41 | def assert_in_resources_dir(self, *path): | ||||
42 | assert os.path.exists(os.path.join(self.resources_dir.name, *path)) | ||||
43 | | ||||
44 | def assert_not_in_resources_dir(self, *path): | ||||
45 | assert not os.path.exists(os.path.join(self.resources_dir.name, *path)) | ||||
24 | 46 | | |||
25 | def test_zipfile_doesnt_exist(self): | 47 | def test_zipfile_doesnt_exist(self): | ||
26 | with pytest.raises(PluginReadError): | 48 | with pytest.raises(PluginReadError): | ||
Context not available. | |||||
35 | PluginImporter(self.zip_filename, | 57 | PluginImporter(self.zip_filename, | ||
36 | self.resources_dir.name, | 58 | self.resources_dir.name, | ||
37 | lambda x: True) | 59 | lambda x: True) | ||
60 | | ||||
61 | def test_simple_plugin_success(self): | ||||
62 | self.zip_plugin('success_simple') | ||||
63 | importer = PluginImporter(self.zip_filename, | ||||
64 | self.resources_dir.name, | ||||
65 | lambda x: True) | ||||
66 | imported = importer.import_all() | ||||
67 | assert len(imported) == 1 | ||||
68 | self.assert_in_resources_dir('pykrita', 'foo') | ||||
69 | self.assert_in_resources_dir('pykrita', 'foo', '__init__.py') | ||||
70 | self.assert_in_resources_dir('pykrita', 'foo', 'foo.py') | ||||
71 | self.assert_in_resources_dir('pykrita', 'foo.desktop') | ||||
72 | self.assert_in_resources_dir('actions', 'foo.action') | ||||
73 | | ||||
74 | def test_toplevel_plugin_success(self): | ||||
75 | self.zip_plugin('success_toplevel') | ||||
76 | importer = PluginImporter(self.zip_filename, | ||||
77 | self.resources_dir.name, | ||||
78 | lambda x: True) | ||||
79 | imported = importer.import_all() | ||||
80 | assert len(imported) == 1 | ||||
81 | self.assert_in_resources_dir('pykrita', 'foo') | ||||
82 | self.assert_in_resources_dir('pykrita', 'foo', '__init__.py') | ||||
83 | self.assert_in_resources_dir('pykrita', 'foo', 'foo.py') | ||||
84 | self.assert_in_resources_dir('pykrita', 'foo.desktop') | ||||
85 | self.assert_in_resources_dir('actions', 'foo.action') | ||||
86 | | ||||
87 | def test_nested_plugin_success(self): | ||||
88 | self.zip_plugin('success_nested') | ||||
89 | importer = PluginImporter(self.zip_filename, | ||||
90 | self.resources_dir.name, | ||||
91 | lambda x: True) | ||||
92 | imported = importer.import_all() | ||||
93 | assert len(imported) == 1 | ||||
94 | self.assert_in_resources_dir('pykrita', 'foo') | ||||
95 | self.assert_in_resources_dir('pykrita', 'foo', '__init__.py') | ||||
96 | self.assert_in_resources_dir('pykrita', 'foo', 'foo.py') | ||||
97 | self.assert_in_resources_dir('pykrita', 'foo.desktop') | ||||
98 | self.assert_in_resources_dir('actions', 'foo.action') | ||||
99 | | ||||
100 | def test_no_action_success(self): | ||||
101 | self.zip_plugin('success_no_action') | ||||
102 | importer = PluginImporter(self.zip_filename, | ||||
103 | self.resources_dir.name, | ||||
104 | lambda x: True) | ||||
105 | imported = importer.import_all() | ||||
106 | assert len(imported) == 1 | ||||
107 | self.assert_in_resources_dir('pykrita', 'foo') | ||||
108 | self.assert_in_resources_dir('pykrita', 'foo', '__init__.py') | ||||
109 | self.assert_in_resources_dir('pykrita', 'foo', 'foo.py') | ||||
110 | self.assert_in_resources_dir('pykrita', 'foo.desktop') | ||||
111 | self.assert_not_in_resources_dir('actions', 'foo.action') | ||||
112 | | ||||
113 | def test_overwrite_existing(self): | ||||
114 | self.zip_plugin('success_simple') | ||||
115 | plugin_dir = os.path.join(self.resources_dir.name, 'pykrita', 'foo') | ||||
116 | os.makedirs(plugin_dir) | ||||
117 | init_file = os.path.join(plugin_dir, '__init__.py') | ||||
118 | with open(init_file, 'w') as f: | ||||
119 | f.write('# existing module') | ||||
120 | | ||||
121 | confirm_callback = mock.MagicMock(return_value=True) | ||||
122 | importer = PluginImporter(self.zip_filename, | ||||
123 | self.resources_dir.name, | ||||
124 | confirm_callback) | ||||
125 | imported = importer.import_all() | ||||
126 | assert len(imported) == 1 | ||||
127 | assert confirm_callback.called | ||||
128 | | ||||
129 | with open(init_file, 'r') as f: | ||||
130 | assert f.read().strip() == "print('hello')" | ||||
131 | | ||||
132 | def test_dont_overwrite_existing(self): | ||||
133 | self.zip_plugin('success_simple') | ||||
134 | plugin_dir = os.path.join(self.resources_dir.name, 'pykrita', 'foo') | ||||
135 | os.makedirs(plugin_dir) | ||||
136 | init_file = os.path.join(plugin_dir, '__init__.py') | ||||
137 | with open(init_file, 'w') as f: | ||||
138 | f.write('# existing module') | ||||
139 | | ||||
140 | confirm_callback = mock.MagicMock(return_value=False) | ||||
141 | importer = PluginImporter(self.zip_filename, | ||||
142 | self.resources_dir.name, | ||||
143 | confirm_callback) | ||||
144 | imported = importer.import_all() | ||||
145 | assert len(imported) == 0 | ||||
146 | assert confirm_callback.called | ||||
147 | | ||||
148 | with open(init_file, 'r') as f: | ||||
149 | assert f.read().strip() == '# existing module' | ||||
150 | | ||||
151 | def test_missing_desktop_file(self): | ||||
152 | self.zip_plugin('fail_no_desktop_file') | ||||
153 | importer = PluginImporter(self.zip_filename, | ||||
154 | self.resources_dir.name, | ||||
155 | lambda x: True) | ||||
156 | with pytest.raises(NoPluginsFoundException): | ||||
157 | importer.import_all() | ||||
158 | | ||||
159 | def test_unparsable_desktop_file(self): | ||||
160 | self.zip_plugin('fail_unparsable_desktop_file') | ||||
161 | importer = PluginImporter(self.zip_filename, | ||||
162 | self.resources_dir.name, | ||||
163 | lambda x: True) | ||||
164 | with pytest.raises(PluginReadError): | ||||
165 | importer.import_all() | ||||
166 | | ||||
167 | def test_missing_keys_in_desktop_file(self): | ||||
168 | self.zip_plugin('fail_missing_keys_desktop_file') | ||||
169 | importer = PluginImporter(self.zip_filename, | ||||
170 | self.resources_dir.name, | ||||
171 | lambda x: True) | ||||
172 | with pytest.raises(PluginReadError): | ||||
173 | importer.import_all() | ||||
174 | | ||||
175 | def test_no_matching_plugindir(self): | ||||
176 | self.zip_plugin('fail_no_matching_plugindir') | ||||
177 | importer = PluginImporter(self.zip_filename, | ||||
178 | self.resources_dir.name, | ||||
179 | lambda x: True) | ||||
180 | with pytest.raises(NoPluginsFoundException): | ||||
181 | importer.import_all() | ||||
182 | | ||||
183 | def test_no_init_file(self): | ||||
184 | self.zip_plugin('fail_no_init_file') | ||||
185 | importer = PluginImporter(self.zip_filename, | ||||
186 | self.resources_dir.name, | ||||
187 | lambda x: True) | ||||
188 | with pytest.raises(NoPluginsFoundException): | ||||
189 | importer.import_all() | ||||
190 | | ||||
191 | def test_unparsable_action_file(self): | ||||
192 | self.zip_plugin('fail_unparsable_action_file') | ||||
193 | importer = PluginImporter(self.zip_filename, | ||||
194 | self.resources_dir.name, | ||||
195 | lambda x: True) | ||||
196 | with pytest.raises(PluginReadError): | ||||
197 | importer.import_all() | ||||
Context not available. |