-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·358 lines (321 loc) · 13.4 KB
/
tests.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2009-2010, Anton Korenyushkin
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the names of contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import with_statement
from getpass import getpass
import cStringIO
import os.path
import shutil
import sys
import tempfile
import unittest
import urllib2
script = akshell = None # To be set in main()
def _create_config():
print '''\
For running akshell unit tests you need a test application.
If you already have one type your credentials, application name and
spot name in it below for config file generation.'''
user = raw_input('User name: ')
password = getpass('Password: ')
app = raw_input('App name: ')
spot = raw_input('Spot name: ')
path = os.path.join(os.path.dirname(__file__), 'test_vars.py')
with open(path, 'w') as f:
f.write('''\
# Akshell test config file. Generated automatically.
USER = %s
PASSWORD = %s
APP = %s
SPOT = %s
''' % tuple(repr(var) for var in (user, password, app, spot)))
print 'Config file "test_vars.py" was generated.'
return user, password, app, spot
try:
from test_vars import USER, PASSWORD, APP, SPOT
except ImportError:
USER, PASSWORD, APP, SPOT = _create_config()
class _ToolTestCase(unittest.TestCase):
def _launch(self, args, code=0, input=None, stream='stdout'):
if type(args) in (str, unicode):
args = args.split()
assert stream in ('stdout', 'stderr')
return_code = 0
if input is not None:
old_stdin = sys.stdin
sys.stdin = cStringIO.StringIO(input)
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = cStringIO.StringIO()
sys.stderr = cStringIO.StringIO()
try:
script.main(args)
except SystemExit, system_exit:
return_code = system_exit.code
finally:
result = (sys.stdout.getvalue() if stream == 'stdout' else
sys.stderr.getvalue())
sys.stdout.close()
sys.stderr.close()
if input is not None:
sys.stdin.close()
sys.stdin = old_stdin
sys.stdout = old_stdout
sys.stderr = old_stderr
self.assertEqual(return_code, code)
return result
class CommandTestCase(_ToolTestCase):
def testHelp(self):
main_help = self._launch([])
self.assertEqual(self._launch('help'), main_help)
self.assertEqual(self._launch('--help'), main_help)
self.assertEqual(self._launch('help help'), self._launch('help -h'))
self.assertEqual(self._launch('help login'), self._launch('login -h'))
self.assertEqual(self._launch('help get'), self._launch('get --help'))
self.assertEqual(
self._launch('help login get'),
self._launch('login -h') + '\n' + self._launch('get -h'))
def testVersion(self):
version = 'akshell ' + akshell.__version__ + '\n'
self.assertEqual(self._launch('-v'), version)
old_argv = sys.argv
sys.argv = ['akshell', '--version']
try:
self.assertEqual(self._launch(None), version)
finally:
sys.argv = old_argv
def testCommandErrors(self):
self._launch('help no_such_command')
self._launch('no_such_command', 1)
def testLoginLogout(self):
try:
script.getpass = raw_input
credentials = '%s\n%s\n' % (USER, PASSWORD)
self._launch('login', input=credentials)
self._launch('login', input=credentials)
self._launch('logout')
self._launch('logout')
self._launch('login', input='%s\n%s \n' % (USER, PASSWORD), code=1)
self._launch('login', input='')
def interrupt(*args):
raise KeyboardInterrupt
script.getpass = interrupt
self._launch('login', input=credentials, code=1)
def raise_url_error(prompt_):
raise urllib2.URLError(None)
script.getpass = raise_url_error
self._launch('login', input=credentials, code=1)
finally:
script.getpass = getpass
def testGetPutEval(self):
self._launch('get 1 2 3', code=1)
self._launch('put app', input='n\n')
self._launch('eval app 2+2', input='n\n')
self._launch('eval 1 2 3', code=1)
def _write(path, data):
with open(path, 'w') as f:
f.write(data)
def _read(path):
with open(path) as f:
return f.read()
class WorkTestCase(_ToolTestCase):
def setUp(self):
akshell.login(USER, PASSWORD)
self._dir = tempfile.mkdtemp()
self._old_cwd = os.getcwd()
os.chdir(self._dir)
self._launch(['put', '-cf', APP, '.'])
os.makedirs(os.path.join('dir', 'subdir'))
_write(os.path.join('dir', 'hello.txt'), 'hello world')
_write('__main__.js', 'var x = 42;')
self._launch(['put', '-f', APP, '.'])
self._launch(['put', '-c', '%s:%s' % (APP, SPOT), '.'])
shutil.rmtree('dir')
os.remove('__main__.js')
def tearDown(self):
os.chdir(self._old_cwd)
shutil.rmtree(self._dir)
def testGet(self):
self._launch(['get', 'no-such-app'], code=1)
diff = akshell.transfer(akshell.Remote(APP), akshell.Local('.'))
self.assertEqual(diff.delete, [])
self.assertEqual(diff.create, [['dir'], ['dir', 'subdir']])
self.assertEqual(diff.save, [['__main__.js'], ['dir', 'hello.txt']])
self.assertEqual(_read(os.path.join('dir', 'hello.txt')), 'hello world')
os.remove('__main__.js')
os.mkdir('__main__.js')
_write(os.path.join('dir', 'hello.txt'), 'wuzzup')
self._launch(['get', APP, '.'])
self.assertEqual(_read(os.path.join('dir', 'hello.txt')),
'hello world')
os.rmdir(os.path.join('dir', 'subdir'))
_write(os.path.join('dir', 'subdir'), '')
os.mkdir('some_dir')
_write('some_file', '')
self._launch(['get', APP, '.'])
self.assert_(os.path.exists('some_dir'))
self.assert_(os.path.exists('some_file'))
self._launch(['get', '-c', APP, '.'])
self.assert_(not os.path.exists('some_dir'))
self.assert_(not os.path.exists('some_file'))
os.remove('__main__.js')
self._launch(['get', APP + '/__main__.js'])
self.assert_(os.path.isfile('__main__.js'))
akshell.logout()
self._launch(['get', '%s:%s@%s' % (APP, USER, SPOT)], code=1)
def testGetSpot(self):
self._launch(['get', '%s:%s@%s/dir' % (APP, USER, SPOT)])
self.assert_(os.path.isdir('dir'))
self._launch(['get', '%s:%s@%s' % (APP, USER, SPOT), SPOT])
self.assert_(os.path.isdir(SPOT))
shutil.rmtree(SPOT)
self._launch(['get', '%s:%s' % (APP, SPOT), SPOT])
self.assert_(os.path.isdir(SPOT))
def testExceptions(self):
self.assertRaises(AssertionError,
akshell.Remote,
APP, USER)
self.assertRaises(AssertionError,
akshell.Remote,
APP, None, SPOT)
self.assertRaises(akshell.RequestError,
akshell.transfer,
akshell.Local('.'), akshell.Remote(APP, cookie=None))
akshell.logout()
self.assertRaises(akshell.LoginRequiredError,
akshell.Remote,
APP, spot_name=SPOT)
def testPut(self):
self._launch(['get', APP, '.'])
self.assertEqual(
self._launch(['put', '-qf', APP, '.', '-e', 'x']),
'42\n')
_write('file', 'some text')
os.mkdir('some_dir')
os.mkdir(os.path.join('some_dir', '.svn'))
shutil.rmtree('dir')
_write('backup~', '')
_write('1.bak', '')
_write('russian имя файла', '')
output = self._launch(
['put', '-cfi', ':*~::::*.bak:.*::', APP, '.'])
self.assert_('file' in output)
self.assert_('some_dir' in output)
self.assert_('dir' in output)
self.assert_('.svn' not in output)
self.assert_('backup~' not in output)
self.assert_('1.bak' not in output)
shutil.rmtree('some_dir')
_write('some_dir', '')
self.assert_('some_dir' in
self._launch(['put', '-f', APP, '.']))
self._launch(['get', '-c', APP, '.'])
self.assert_(os.path.exists('russian имя файла'))
self.assert_(os.path.exists('backup~'))
self._launch(['get', '-c', '-i', '', APP, '.'])
self.assert_(not os.path.exists('backup~'))
_write('another file', '')
self.assert_('S another file' in
self._launch(['put', '-f', APP + '/another file']))
def testEval(self):
self.assertEqual(self._launch(['eval', '-f', APP, 'x']), '42\n')
place = '%s:%s' % (APP, SPOT)
self._launch(['eval', place, 'y=1'])
self.assertEqual(self._launch(['eval', place, 'y']), '1\n')
self.assert_('ReferenceError' in self._launch(['eval', '-f', APP, 'y']))
def testBuffer(self):
buffer = akshell.Buffer()
diff = akshell.transfer(akshell.Remote(APP), buffer)
self.assertEqual(diff.delete, [])
self.assertEqual(diff.create, [[], ['dir'], ['dir', 'subdir']])
self.assertEqual(diff.save, [['__main__.js'], ['dir', 'hello.txt']])
self.assertEqual(
buffer.data,
{'__main__.js': 'var x = 42;',
'dir': {'hello.txt': 'hello world', 'subdir': {}},
})
del buffer.data['dir']
buffer.data['other dir'] = {'subdir': {'file': 'text'}}
diff = akshell.transfer(buffer, akshell.Remote(APP), True)
self.assertEqual(diff.delete, [['dir']])
self.assertEqual(diff.create, [['other dir'], ['other dir', 'subdir']])
self.assertEqual(diff.save, [['other dir', 'subdir', 'file']])
diff = akshell.transfer(akshell.Remote(APP, path='other dir'),
buffer,
True)
self.assertEqual(diff.delete, [['__main__.js'], ['other dir']])
self.assertEqual(diff.create, [['subdir']])
self.assertEqual(diff.save, [['subdir', 'file']])
self.assertEqual(buffer.data['subdir']['file'], 'text')
self.assertRaises(akshell.DoesNotExistError,
akshell.transfer,
akshell.Buffer(), akshell.Remote(APP))
diff = akshell.transfer(akshell.Buffer({}), akshell.Remote(APP), True)
self.assertEqual(diff.delete, [['__main__.js'], ['other dir']])
def suite():
result = unittest.TestSuite()
result.addTest(unittest.makeSuite(CommandTestCase))
result.addTest(unittest.makeSuite(WorkTestCase))
return result
def main():
try:
cov_idx = sys.argv.index('--cov')
except ValueError:
coverage = None
else:
del sys.argv[cov_idx]
try:
import coverage
except ImportError:
sys.stderr.write('''\
Please install "coverage" module to collect coverage. Try typing:
sudo easy_install coverage
''')
sys.exit(1)
import coverage_color
coverage.start()
global akshell, script
akshell = __import__('akshell')
script = __import__('script')
try:
server_idx = sys.argv.index('--server')
except ValueError:
pass
else:
akshell.SERVER = sys.argv[server_idx + 1]
del sys.argv[server_idx : server_idx + 2]
try:
unittest.main(defaultTest='suite')
finally:
if coverage:
coverage.stop()
for module in (akshell, script):
path, stmts_, missing_, missing_str = coverage.analysis(module)
with open('coverage_%s.html' % module.__name__, 'w') as f:
coverage_color.colorize_file(path, f, missing_str)
coverage.report([akshell, script], show_missing=False)
coverage.erase()
if __name__ == '__main__':
main()