|
1
|
|
|
""" |
|
2
|
|
|
Test usage of plugin |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
import os |
|
6
|
|
|
import subprocess |
|
7
|
|
|
import sys |
|
8
|
|
|
import unittest |
|
9
|
|
|
from shutil import rmtree |
|
10
|
|
|
from stakkr import package_utils |
|
11
|
|
|
__base_dir__ = os.path.abspath(os.path.dirname(__file__)) |
|
12
|
|
|
sys.path.insert(0, __base_dir__ + '/../') |
|
13
|
|
|
__venv_dir__ = package_utils.get_venv_basedir() |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class PluginsTest(unittest.TestCase): |
|
17
|
|
|
cmd_base = ['stakkr', '-c', __base_dir__ + '/static/config_valid.ini'] |
|
18
|
|
|
|
|
19
|
|
|
def test_no_plugin_dir(self): |
|
20
|
|
|
"""Make sure I have the right message when no plugin is present""" |
|
21
|
|
|
|
|
22
|
|
|
clean_plugin_dir() |
|
23
|
|
|
os.rmdir(__venv_dir__ + '/plugins') |
|
24
|
|
|
|
|
25
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
26
|
|
|
res = exec_cmd(cmd) |
|
27
|
|
|
self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*') |
|
28
|
|
|
self.assertRegex(res['stdout'], '.*No plugin to add*') |
|
29
|
|
|
self.assertEqual(res['stderr'], '') |
|
30
|
|
|
self.assertIs(res['status'], 0) |
|
31
|
|
|
|
|
32
|
|
|
os.mkdir(__venv_dir__ + '/plugins') |
|
33
|
|
|
|
|
34
|
|
|
def test_no_plugin(self): |
|
35
|
|
|
"""Make sure I have the right message when no plugin is present""" |
|
36
|
|
|
|
|
37
|
|
|
clean_plugin_dir() |
|
38
|
|
|
|
|
39
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
40
|
|
|
res = exec_cmd(cmd) |
|
41
|
|
|
self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*') |
|
42
|
|
|
self.assertRegex(res['stdout'], '.*No plugin to add*') |
|
43
|
|
|
self.assertEqual(res['stderr'], '') |
|
44
|
|
|
self.assertIs(res['status'], 0) |
|
45
|
|
|
|
|
46
|
|
|
def test_plugin_empty(self): |
|
47
|
|
|
"""Create a directory with nothing inside and see if I get the right message""" |
|
48
|
|
|
|
|
49
|
|
|
clean_plugin_dir() |
|
50
|
|
|
|
|
51
|
|
|
folder = __venv_dir__ + '/plugins/empty_plugin' |
|
52
|
|
|
os.mkdir(folder) |
|
53
|
|
|
self.assertTrue(os.path.isdir(folder)) |
|
54
|
|
|
|
|
55
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
56
|
|
|
res = exec_cmd(cmd) |
|
57
|
|
|
self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*') |
|
58
|
|
|
self.assertRegex(res['stdout'], '.*No plugin found in "plugins/empty_plugin/".*') |
|
59
|
|
|
self.assertEqual(res['stderr'], '') |
|
60
|
|
|
self.assertIs(res['status'], 0) |
|
61
|
|
|
|
|
62
|
|
|
os.rmdir(folder) |
|
63
|
|
|
|
|
64
|
|
|
def test_bad_plugin(self): |
|
65
|
|
|
"""Install a bad plugin and check if it works (it shouldn't)""" |
|
66
|
|
|
|
|
67
|
|
|
clean_plugin_dir() |
|
68
|
|
|
|
|
69
|
|
|
folder = __venv_dir__ + '/plugins/test_bad_plugin' |
|
70
|
|
|
os.mkdir(folder) |
|
71
|
|
|
|
|
72
|
|
|
# Add setup |
|
73
|
|
|
with open(folder + '/setup.py', 'w') as file: |
|
74
|
|
|
file.write(r"""from setuptools import setup |
|
75
|
|
|
|
|
76
|
|
|
setup( |
|
77
|
|
|
name='StakkrTestPlugin', |
|
78
|
|
|
version='3.5', |
|
79
|
|
|
packages=['test_plugin'], |
|
80
|
|
|
entry_points=''' |
|
81
|
|
|
[stakkr.plugins] |
|
82
|
|
|
test_plugin=test_plugin.core:my_test |
|
83
|
|
|
''' |
|
84
|
|
|
) |
|
85
|
|
|
""") |
|
86
|
|
|
|
|
87
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
88
|
|
|
res = exec_cmd(cmd) |
|
89
|
|
|
self.assertRegex(res['stdout'], 'Adding plugins from plugins.*') |
|
90
|
|
|
self.assertRegex(res['stderr'], 'Command .* failed with error code 1') |
|
91
|
|
|
self.assertIs(res['status'], 1) |
|
92
|
|
|
|
|
93
|
|
|
rmtree(folder) |
|
94
|
|
|
|
|
95
|
|
|
def test_plugin_ok(self): |
|
96
|
|
|
"""Install a good plugin and test it""" |
|
97
|
|
|
|
|
98
|
|
|
clean_plugin_dir() |
|
99
|
|
|
|
|
100
|
|
|
folder = __venv_dir__ + '/plugins/test_plugin' |
|
101
|
|
|
os.mkdir(folder) |
|
102
|
|
|
os.mkdir(folder + '/test_plugin') |
|
103
|
|
|
|
|
104
|
|
|
# Add setup |
|
105
|
|
|
with open(folder + '/setup.py', 'w') as file: |
|
106
|
|
|
file.write(r"""from setuptools import setup |
|
107
|
|
|
|
|
108
|
|
|
setup( |
|
109
|
|
|
name='StakkrTestPlugin', |
|
110
|
|
|
version='3.5', |
|
111
|
|
|
packages=['test_plugin'], |
|
112
|
|
|
entry_points=''' |
|
113
|
|
|
[stakkr.plugins] |
|
114
|
|
|
test_plugin=test_plugin.core:my_test |
|
115
|
|
|
''' |
|
116
|
|
|
) |
|
117
|
|
|
""") |
|
118
|
|
|
|
|
119
|
|
|
# Add plugin content |
|
120
|
|
|
with open(folder + '/test_plugin/core.py', 'w') as file: |
|
121
|
|
|
file.write(r"""import click |
|
122
|
|
|
|
|
123
|
|
|
@click.command(name="hello-world") |
|
124
|
|
|
@click.pass_context |
|
125
|
|
|
def my_test(ctx): |
|
126
|
|
|
print('Hello test !') |
|
127
|
|
|
""") |
|
128
|
|
|
|
|
129
|
|
|
# Refresh plugins, plugin should be added |
|
130
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
131
|
|
|
res = exec_cmd(cmd) |
|
132
|
|
|
self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*') |
|
133
|
|
|
self.assertRegex(res['stdout'], '.*Plugin "test_plugin" added.*') |
|
134
|
|
|
self.assertEqual(res['stderr'], '') |
|
135
|
|
|
self.assertIs(res['status'], 0) |
|
136
|
|
|
|
|
137
|
|
|
# Plugin installed, command available ! |
|
138
|
|
|
cmd = self.cmd_base + ['hello-world'] |
|
139
|
|
|
res = exec_cmd(cmd) |
|
140
|
|
|
self.assertEqual(res['stdout'], 'Hello test !') |
|
141
|
|
|
self.assertEqual(res['stderr'], '') |
|
142
|
|
|
self.assertIs(res['status'], 0) |
|
143
|
|
|
|
|
144
|
|
|
# Now it should remove |
|
145
|
|
|
cmd = self.cmd_base + ['refresh-plugins'] |
|
146
|
|
|
res = exec_cmd(cmd) |
|
147
|
|
|
self.assertRegex(res['stdout'], '.*Adding plugins from plugins.*') |
|
148
|
|
|
self.assertRegex(res['stdout'], '.*Cleaning "StakkrTestPlugin".*') |
|
149
|
|
|
self.assertRegex(res['stdout'], '.*Plugin "test_plugin" added.*') |
|
150
|
|
|
self.assertEqual(res['stderr'], '') |
|
151
|
|
|
self.assertIs(res['status'], 0) |
|
152
|
|
|
|
|
153
|
|
|
rmtree(folder) |
|
154
|
|
|
|
|
155
|
|
|
def tearDownClass(): |
|
156
|
|
|
clean_plugin_dir() |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
def clean_plugin_dir(): |
|
160
|
|
|
if not os.path.isdir(__venv_dir__ + '/plugins'): |
|
161
|
|
|
os.mkdir(__venv_dir__ + '/plugins') |
|
162
|
|
|
|
|
163
|
|
|
if os.path.isdir(__venv_dir__ + '/plugins/empty_plugin'): |
|
164
|
|
|
os.rmdir(__venv_dir__ + '/plugins/empty_plugin') |
|
165
|
|
|
|
|
166
|
|
|
if os.path.isdir(__venv_dir__ + '/plugins/test_plugin'): |
|
167
|
|
|
rmtree(__venv_dir__ + '/plugins/test_plugin') |
|
168
|
|
|
|
|
169
|
|
|
if os.path.isdir(__venv_dir__ + '/plugins/test_bad_plugin'): |
|
170
|
|
|
rmtree(__venv_dir__ + '/plugins/test_bad_plugin') |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
def exec_cmd(cmd: list): |
|
174
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
175
|
|
|
stdout, stderr = p.communicate() |
|
176
|
|
|
status = p.returncode |
|
177
|
|
|
stdout = stdout.decode().strip().replace('\n', '') |
|
178
|
|
|
stderr = stderr.decode().strip().replace('\n', '') |
|
179
|
|
|
|
|
180
|
|
|
return {'stdout': stdout, 'stderr': stderr, 'status': status} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
if __name__ == "__main__": |
|
184
|
|
|
unittest.main() |
|
185
|
|
|
|