|
1 |
| -import distutils.dir_util |
2 |
| -import tempfile |
3 |
| - |
4 |
| -import pytest |
5 | 1 | from overrides import overrides
|
6 | 2 |
|
7 | 3 | from allennlp.commands import Subcommand
|
8 | 4 | from allennlp.common.plugins import (
|
9 |
| - discover_file_plugins, |
10 |
| - discover_namespace_plugins, |
11 | 5 | discover_plugins,
|
12 | 6 | import_plugins,
|
13 | 7 | )
|
14 | 8 | from allennlp.common.testing import AllenNlpTestCase
|
15 | 9 | from allennlp.common.util import pushd
|
16 |
| -from allennlp.tests.common.plugins_util import pip_install |
17 | 10 |
|
18 | 11 |
|
19 | 12 | class TestPlugins(AllenNlpTestCase):
|
20 | 13 | @overrides
|
21 | 14 | def setUp(self):
|
22 | 15 | super().setUp()
|
23 | 16 | self.plugins_root = self.FIXTURES_ROOT / "plugins"
|
24 |
| - # "a" sets a "global" namespace plugin, because it's gonna be installed with pip. |
25 |
| - self.project_a_fixtures_root = self.plugins_root / "project_a" |
26 |
| - # "b" sets a "local" namespace plugin, because it's supposed to be run from that directory. |
27 |
| - self.project_b_fixtures_root = self.plugins_root / "project_b" |
28 |
| - # "c" sets a "global" namespace plugin, because it's gonna be installed with pip. |
29 |
| - self.project_c_fixtures_root = self.plugins_root / "project_c" |
30 |
| - # "d" sets a "local" file plugin, because it's supposed to be run from that directory |
31 |
| - # and has a ".allennlp_plugins" file in it. |
32 |
| - self.project_d_fixtures_root = self.plugins_root / "project_d" |
33 | 17 |
|
34 | 18 | def test_no_plugins(self):
|
35 | 19 | available_plugins = set(discover_plugins())
|
36 | 20 | self.assertSetEqual(set(), available_plugins)
|
37 | 21 |
|
38 |
| - def test_namespace_package_does_not_exist(self): |
39 |
| - available_plugins = set(discover_namespace_plugins("dummy_namespace")) |
40 |
| - self.assertSetEqual(set(), available_plugins) |
41 |
| - |
42 |
| - def test_file_plugins_does_not_exist(self): |
43 |
| - available_plugins = set(discover_file_plugins("dummy_file")) |
44 |
| - self.assertSetEqual(set(), available_plugins) |
45 |
| - |
46 |
| - def test_global_namespace_plugin(self): |
47 |
| - available_plugins = set(discover_plugins()) |
48 |
| - self.assertSetEqual(set(), available_plugins) |
49 |
| - |
50 |
| - with pip_install(self.project_a_fixtures_root, "a"): |
51 |
| - available_plugins = set(discover_plugins()) |
52 |
| - self.assertSetEqual({"allennlp_plugins.a"}, available_plugins) |
53 |
| - |
54 |
| - import_plugins() |
55 |
| - subcommands_available = Subcommand.list_available() |
56 |
| - self.assertIn("a", subcommands_available) |
57 |
| - |
58 |
| - def test_two_global_namespace_plugins(self): |
59 |
| - available_plugins = set(discover_plugins()) |
60 |
| - self.assertSetEqual(set(), available_plugins) |
61 |
| - |
62 |
| - with pip_install(self.project_a_fixtures_root, "a"), pip_install( |
63 |
| - self.project_c_fixtures_root, "c" |
64 |
| - ): |
65 |
| - available_plugins = set(discover_plugins()) |
66 |
| - self.assertSetEqual({"allennlp_plugins.a", "allennlp_plugins.c"}, available_plugins) |
67 |
| - |
68 |
| - import_plugins() |
69 |
| - subcommands_available = Subcommand.list_available() |
70 |
| - self.assertIn("a", subcommands_available) |
71 |
| - self.assertIn("c", subcommands_available) |
72 |
| - |
73 |
| - def test_local_namespace_plugin(self): |
74 |
| - available_plugins = set(discover_plugins()) |
75 |
| - self.assertSetEqual(set(), available_plugins) |
76 |
| - |
77 |
| - with pushd(self.project_b_fixtures_root): |
78 |
| - available_plugins = set(discover_plugins()) |
79 |
| - self.assertSetEqual({"allennlp_plugins.b"}, available_plugins) |
80 |
| - |
81 |
| - import_plugins() |
82 |
| - subcommands_available = Subcommand.list_available() |
83 |
| - self.assertIn("b", subcommands_available) |
84 |
| - |
85 | 22 | def test_file_plugin(self):
|
86 | 23 | available_plugins = set(discover_plugins())
|
87 | 24 | self.assertSetEqual(set(), available_plugins)
|
88 | 25 |
|
89 |
| - with pushd(self.project_d_fixtures_root): |
| 26 | + with pushd(self.plugins_root): |
90 | 27 | available_plugins = set(discover_plugins())
|
91 | 28 | self.assertSetEqual({"d"}, available_plugins)
|
92 | 29 |
|
93 | 30 | import_plugins()
|
94 | 31 | subcommands_available = Subcommand.list_available()
|
95 | 32 | self.assertIn("d", subcommands_available)
|
96 |
| - |
97 |
| - def test_local_namespace_plugin_different_path(self): |
98 |
| - available_plugins = set(discover_plugins()) |
99 |
| - self.assertSetEqual(set(), available_plugins) |
100 |
| - |
101 |
| - with tempfile.TemporaryDirectory() as temp_dir_b: |
102 |
| - distutils.dir_util.copy_tree(self.project_b_fixtures_root, temp_dir_b) |
103 |
| - |
104 |
| - # We move to another directory with a different plugin "b", as if it were another |
105 |
| - # separate project which is not installed ("local" usage of the plugin declared in |
106 |
| - # the namespace). |
107 |
| - with pushd(temp_dir_b): |
108 |
| - available_plugins = set(discover_plugins()) |
109 |
| - self.assertSetEqual({"allennlp_plugins.b"}, available_plugins) |
110 |
| - |
111 |
| - import_plugins() |
112 |
| - subcommands_available = Subcommand.list_available() |
113 |
| - self.assertIn("b", subcommands_available) |
114 |
| - |
115 |
| - def test_local_and_two_global_namespace_plugins(self): |
116 |
| - available_plugins = set(discover_plugins()) |
117 |
| - self.assertSetEqual(set(), available_plugins) |
118 |
| - |
119 |
| - # We make plugins "a" and "c" available as packages, each from other directories, |
120 |
| - # as if they were separate installed projects ("global" usage of the plugins). |
121 |
| - # We move to another directory with a different plugin "b", as if it were another separate |
122 |
| - # project which is not installed ("local" usage of the plugin declared in the namespace). |
123 |
| - with pip_install(self.project_a_fixtures_root, "a"), pip_install( |
124 |
| - self.project_c_fixtures_root, "c" |
125 |
| - ), pushd(self.project_b_fixtures_root): |
126 |
| - available_plugins = set(discover_plugins()) |
127 |
| - self.assertSetEqual( |
128 |
| - {"allennlp_plugins.a", "allennlp_plugins.b", "allennlp_plugins.c"}, |
129 |
| - available_plugins, |
130 |
| - ) |
131 |
| - |
132 |
| - import_plugins() |
133 |
| - subcommands_available = Subcommand.list_available() |
134 |
| - self.assertIn("a", subcommands_available) |
135 |
| - self.assertIn("b", subcommands_available) |
136 |
| - self.assertIn("c", subcommands_available) |
137 |
| - |
138 |
| - def test_file_and_two_global_namespace_plugins(self): |
139 |
| - available_plugins = set(discover_plugins()) |
140 |
| - self.assertSetEqual(set(), available_plugins) |
141 |
| - |
142 |
| - # We make plugins "a" and "c" available as packages, each from other directories, |
143 |
| - # as if they were separate installed projects ("global" usage of the plugins). |
144 |
| - # We move to another directory with a different plugin "b", as if it were another separate |
145 |
| - # project which is not installed ("local" usage of the plugin declared in a file). |
146 |
| - with pip_install(self.project_a_fixtures_root, "a"), pip_install( |
147 |
| - self.project_c_fixtures_root, "c" |
148 |
| - ), pushd(self.project_d_fixtures_root): |
149 |
| - available_plugins = set(discover_plugins()) |
150 |
| - self.assertSetEqual( |
151 |
| - {"allennlp_plugins.a", "allennlp_plugins.c", "d"}, available_plugins |
152 |
| - ) |
153 |
| - |
154 |
| - import_plugins() |
155 |
| - subcommands_available = Subcommand.list_available() |
156 |
| - self.assertIn("a", subcommands_available) |
157 |
| - self.assertIn("c", subcommands_available) |
158 |
| - self.assertIn("d", subcommands_available) |
159 |
| - |
160 |
| - def test_reload_plugins_adds_new(self): |
161 |
| - available_plugins = set(discover_plugins()) |
162 |
| - self.assertSetEqual(set(), available_plugins) |
163 |
| - |
164 |
| - with pip_install(self.project_a_fixtures_root, "a"): |
165 |
| - available_plugins = set(discover_plugins()) |
166 |
| - self.assertSetEqual({"allennlp_plugins.a"}, available_plugins) |
167 |
| - |
168 |
| - import_plugins() |
169 |
| - subcommands_available = Subcommand.list_available() |
170 |
| - self.assertIn("a", subcommands_available) |
171 |
| - |
172 |
| - with pip_install(self.project_c_fixtures_root, "c"): |
173 |
| - available_plugins = set(discover_plugins()) |
174 |
| - self.assertSetEqual({"allennlp_plugins.a", "allennlp_plugins.c"}, available_plugins) |
175 |
| - |
176 |
| - import_plugins() |
177 |
| - subcommands_available = Subcommand.list_available() |
178 |
| - self.assertIn("a", subcommands_available) |
179 |
| - self.assertIn("c", subcommands_available) |
180 |
| - |
181 |
| - @pytest.mark.skip("Plugin unloading is not supported.") |
182 |
| - def test_unload_plugin(self): |
183 |
| - available_plugins = set(discover_plugins()) |
184 |
| - self.assertSetEqual(set(), available_plugins) |
185 |
| - |
186 |
| - with pip_install(self.project_a_fixtures_root, "a"): |
187 |
| - available_plugins = set(discover_plugins()) |
188 |
| - self.assertSetEqual({"allennlp_plugins.a"}, available_plugins) |
189 |
| - |
190 |
| - import_plugins() |
191 |
| - subcommands_available = Subcommand.list_available() |
192 |
| - self.assertIn("a", subcommands_available) |
193 |
| - |
194 |
| - available_plugins = set(discover_plugins()) |
195 |
| - self.assertSetEqual(set(), available_plugins) |
196 |
| - |
197 |
| - import_plugins() |
198 |
| - subcommands_available = Subcommand.list_available() |
199 |
| - self.assertNotIn("a", subcommands_available) |
200 |
| - |
201 |
| - @pytest.mark.skip("Plugin unloading is not supported.") |
202 |
| - def test_reload_plugins_removes_one_adds_one(self): |
203 |
| - available_plugins = set(discover_plugins()) |
204 |
| - self.assertSetEqual(set(), available_plugins) |
205 |
| - |
206 |
| - with pip_install(self.project_a_fixtures_root, "a"): |
207 |
| - available_plugins = set(discover_plugins()) |
208 |
| - self.assertSetEqual({"allennlp_plugins.a"}, available_plugins) |
209 |
| - |
210 |
| - import_plugins() |
211 |
| - subcommands_available = Subcommand.list_available() |
212 |
| - self.assertIn("a", subcommands_available) |
213 |
| - self.assertNotIn("c", subcommands_available) |
214 |
| - |
215 |
| - with pip_install(self.project_c_fixtures_root, "c"): |
216 |
| - available_plugins = set(discover_plugins()) |
217 |
| - self.assertSetEqual({"allennlp_plugins.c"}, available_plugins) |
218 |
| - |
219 |
| - import_plugins() |
220 |
| - subcommands_available = Subcommand.list_available() |
221 |
| - self.assertNotIn("a", subcommands_available) |
222 |
| - self.assertIn("c", subcommands_available) |
0 commit comments