-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_new.py
executable file
·230 lines (203 loc) · 7.4 KB
/
test_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" test_new.py
Unit tests for new.py v. 0.0.1
-Christopher Welborn 01-26-2016
"""
import os
import socket
import sys
import unittest
# If this fails we have problems.
import plugins
# Can be set for more output.
plugins.DEBUG = False
plugins.debugprinter.enable(plugins.DEBUG)
# Use the existing print_err, instead of writing a new one.
print_err = plugins.print_err
SCRIPTDIR = os.path.abspath(sys.path[0])
PLUGINDIR = os.path.join(SCRIPTDIR, 'plugins')
print('Loading plugins from: {}'.format(PLUGINDIR))
plugins.load_plugins(PLUGINDIR)
if not all(len(plugins.plugins[k]) > 0 for k in plugins.plugins):
print(
'Failed to load any plugins: {!r}'.format(plugins.plugins),
file=sys.stderr)
sys.exit(1)
# Check internet connection, for plugins that download things (html.jquery).
has_internet = True
try:
con = socket.create_connection(('8.8.8.8', 5353))
except OSError as ex:
if ex.errno == 101:
has_internet = False
else:
print_err('Error while detecting internet: {!r}'.format(ex))
else:
con.close()
class NewTest(unittest.TestCase):
""" Tests for New.
Ensures...
plugins are loaded properly, and can be initialized.
plugins create content, or raise the proper exceptions.
plugins can be loaded by name, file extension, or both.
"""
def setUp(self):
# Empty command line arg dict to build on.
self.default_args = {
'ARGS': [],
'PLUGIN': None,
'FILENAME': None,
'-c': False,
'--config': False,
'-C': False,
'--pluginconfig': False,
'-d': False,
'--dryrun': False,
'-D': False,
'--debug ': False,
'-H': False,
'--pluginhelp': False,
'-h': False,
'--help ': False,
'-O': False,
'--overwrite': False,
'-p': False,
'--plugins ': False,
'-x': False,
'--executable': False,
'-v': False,
'--version ': False,
}
# Save a local reference to plugin classes.
self.plugins = {k: v for k, v in plugins.plugins.items()}
self.types = [p for p in self.plugins['types'].values()]
self.post = [p for p in self.plugins['post'].values()]
self.deferred = [p for p in self.plugins['deferred'].values()]
self.default_plugin = plugins.get_plugin_default(_name='python')
def iter_plugin_classes(self):
""" Iterate through all loaded plugin classes. """
for pluginset in self.plugins.values():
yield from (p for p in pluginset.values())
def get_argd(self, updatedict=None):
""" Get an updated arg dict. """
d = {k: v for k, v in self.default_args.items()}
if updatedict is None:
return d
d.update(updatedict)
return d
def test_determine_plugin_byboth(self):
""" Plugins can be determined by both name and file name. """
argd = self.get_argd({'PLUGIN': 'text', 'FILENAME': 'test.txt'})
cls = plugins.determine_plugin(argd)
self.assertIsNot(
cls,
self.default_plugin,
msg='\n'.join((
'Failed to determine plugin by name and file name:',
' FILENAME: {a[FILENAME]!r}',
' PLUGIN: {a[PLUGIN]!r}'
)).format(a=argd)
)
def test_determine_plugin_byfile(self):
""" Plugins can be determined by file name. """
argd = self.get_argd({'FILENAME': 'test.txt'})
cls = plugins.determine_plugin(argd)
self.assertIsNot(
cls,
self.default_plugin,
msg='Failed to determine plugin by file name: {!r}'.format(
argd['FILENAME']
)
)
def test_determine_plugin_byname(self):
""" Plugins can be determined by name. """
argd = self.get_argd({'PLUGIN': 'text'})
cls = plugins.determine_plugin(argd)
self.assertIsNot(
cls,
self.default_plugin,
msg='Failed to determine plugin by explicit name: {!r}'.format(
argd['PLUGIN']
)
)
def test_get_plugin_byext(self):
""" Plugins can be loaded by file extension. """
ext = 'test.txt'
cls = plugins.get_plugin_byext(ext)
self.assertIsNotNone(
cls,
msg='get_plugin_byext returned None for a known file extension!'
)
self.assertIsNot(
cls,
self.default_plugin,
msg='Failed to load plugin by file extension: {!r}'.format(cls)
)
def test_get_plugin_byname(self):
""" Plugins can be loaded by name. """
name = 'text'
cls = plugins.get_plugin_byname(name)
self.assertIsNotNone(
cls,
msg='get_plugin_byname returned None for a known plugin!'
)
self.assertIsNot(
cls,
self.default_plugin,
msg='Failed to load plugin by explicit name: {!r}'.format(cls)
)
def test_plugin_create(self):
""" Filetype plugins should create. (unless allow_blank is set) """
for cls in self.types:
plugin = cls()
# Notify plugin that this is just a test (disabled side-effects)
plugin.dryrun = True
# Notify the plugin that it should not try to dl without inet.
plugin.config['no_download'] = not has_internet
try:
content = plugin._create('no file', [])
except plugins.SignalAction as sigaction:
# Some plugins create through signals, to change content
# or change the file name.
self.assertTrue(
sigaction.content,
msg='Plugin signalled with no content!: {}'.format(
plugin.get_name()
)
)
else:
# Content was supposed to be returned normally.
if plugin.allow_blank:
self.assertIsNone(
content,
msg='Blank plugin created content: {}'.format(
plugin.get_name()
)
)
else:
self.assertIsNotNone(
content,
msg='Plugin failed to create content: {}'.format(
plugin.get_name()
)
)
def test_plugin_init(self):
""" Plugins should initialize """
for cls in self.iter_plugin_classes():
self.assertTrue(
isinstance(cls(), plugins.PluginBase),
msg='Failed to initialize {ptype} plugin: {pname}'.format(
ptype=cls.__name__,
pname=cls.get_name())
)
def test_plugins_loaded(self):
""" load_plugins should set plugins.plugins to non-empty values. """
for key in ('types', 'post', 'deferred'):
self.assertTrue(
plugins.plugins.get(key, {}),
msg='Global file {!r} plugins were not loaded!'.format(key)
)
if __name__ == '__main__':
print('{!r}'.format(plugins.plugins))
sys.exit(unittest.main(argv=sys.argv))