-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrun_tests.py
executable file
·660 lines (495 loc) · 21.9 KB
/
run_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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import sys
import os
import kaptan
import glob
import tempfile
import shutil
import collections
import subprocess
from pprint import pprint
def expand_config(config):
'''Expand configuration into full form. Enables shorthand forms for
analects config.
:param config: the configuration for the session
:type config: dict
'''
for directory, repos in config.iteritems():
for repo, repo_data in repos.iteritems():
'''
repo_name: http://myrepo.com/repo.git
to
repo_name: { repo: 'http://myrepo.com/repo.git' }
also assures the repo is a :py:class:`dict`.
'''
if isinstance(repo_data, basestring):
config[directory][repo] = {'repo': repo_data}
'''
``shell_command_after``: if str, turn to list.
'''
if 'shell_command_after' in repo_data:
if isinstance(repo_data['shell_command_after'], basestring):
repo_data['shell_command_after'] = [
repo_data['shell_command_after']]
return config
class ConfigTestCaseBase(unittest.TestCase):
""" contains the fresh config dict/yaml's to test against.
this is because running ConfigExpand on SAMPLECONFIG_DICT would alter
it in later test cases. these configs are used throughout the tests.
"""
def setUp(self):
SAMPLECONFIG_YAML = """
/home/user/study/:
linux: git+git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
freebsd: git+https://github.com/freebsd/freebsd.git
sqlalchemy: hg+https://bitbucket.org/zzzeek/sqlalchemy.git
docutils: svn+http://svn.code.sf.net/p/docutils/code/trunk
/home/user/github_projects/:
kaptan:
repo: [email protected]:tony/kaptan.git
remotes:
upstream: git+https://github.com/emre/kaptan
marksteve: git+https://github.com/marksteve/kaptan.git
/home/tony/:
.vim:
repo: [email protected]:tony/vim-config.git
shell_command_after: ln -sf /home/tony/.vim/.vimrc /home/tony/.vimrc
.tmux:
repo: [email protected]:tony/tmux-config.git
shell_command_after:
- ln -sf /home/tony/.tmux/.tmux.conf /home/tony/.tmux.conf
"""
SAMPLECONFIG_DICT = {
'/home/user/study/': {
'linux': 'git+git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git',
'freebsd': 'git+https://github.com/freebsd/freebsd.git',
'sqlalchemy': 'hg+https://bitbucket.org/zzzeek/sqlalchemy.git',
'docutils': 'svn+http://svn.code.sf.net/p/docutils/code/trunk',
},
'/home/user/github_projects/': {
'kaptan': {
'repo': '[email protected]:tony/kaptan.git',
'remotes': {
'upstream': 'git+https://github.com/emre/kaptan',
'marksteve': 'git+https://github.com/marksteve/kaptan.git'
}
}
},
'/home/tony/': {
'.vim': {
'repo': '[email protected]:tony/vim-config.git',
'shell_command_after': 'ln -sf /home/tony/.vim/.vimrc /home/tony/.vimrc'
},
'.tmux': {
'repo': '[email protected]:tony/tmux-config.git',
'shell_command_after': ['ln -sf /home/tony/.tmux/.tmux.conf /home/tony/.tmux.conf']
}
}
}
SAMPLECONFIG_FINAL_DICT = {
'/home/user/study/': {
'linux': {'repo': 'git+git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git', },
'freebsd': {'repo': 'git+https://github.com/freebsd/freebsd.git', },
'sqlalchemy': {'repo': 'hg+https://bitbucket.org/zzzeek/sqlalchemy.git', },
'docutils': {'repo': 'svn+http://svn.code.sf.net/p/docutils/code/trunk', },
},
'/home/user/github_projects/': {
'kaptan': {
'repo': '[email protected]:tony/kaptan.git',
'remotes': {
'upstream': 'git+https://github.com/emre/kaptan',
'marksteve': 'git+https://github.com/marksteve/kaptan.git'
}
}
},
'/home/tony/': {
'.vim': {
'repo': '[email protected]:tony/vim-config.git',
'shell_command_after': ['ln -sf /home/tony/.vim/.vimrc /home/tony/.vimrc']
},
'.tmux': {
'repo': '[email protected]:tony/tmux-config.git',
'shell_command_after': ['ln -sf /home/tony/.tmux/.tmux.conf /home/tony/.tmux.conf']
}
}
}
self.config_dict = SAMPLECONFIG_DICT
self.config_dict_expanded = SAMPLECONFIG_FINAL_DICT
self.config_yaml = SAMPLECONFIG_YAML
class ConfigFormatTestCase(ConfigTestCaseBase):
""" verify that example YAML is returning expected dict format """
def test_dict_equals_yaml(self):
config = kaptan.Kaptan(handler='yaml')
config.import_config(self.config_yaml)
self.maxDiff = None
self.assertDictEqual(self.config_dict, config.export('dict'))
class ConfigImportExportTestCase(ConfigTestCaseBase):
def test_export_json(self):
TMP_DIR = self.TMP_DIR
json_config_file = os.path.join(TMP_DIR, '.analects.json')
config = kaptan.Kaptan()
config.import_config(self.config_dict)
json_config_data = config.export('json', indent=2)
buf = open(json_config_file, 'w')
buf.write(json_config_data)
buf.close()
new_config = kaptan.Kaptan()
new_config_data = new_config.import_config(json_config_file).get()
self.assertDictEqual(self.config_dict, new_config_data)
def test_export_yaml(self):
TMP_DIR = self.TMP_DIR
yaml_config_file = os.path.join(TMP_DIR, '.analects.yaml')
config = kaptan.Kaptan()
config.import_config(self.config_dict)
yaml_config_data = config.export('yaml', indent=2)
buf = open(yaml_config_file, 'w')
buf.write(yaml_config_data)
buf.close()
new_config = kaptan.Kaptan()
new_config_data = new_config.import_config(yaml_config_file).get()
self.assertDictEqual(self.config_dict, new_config_data)
def test_scan_config(self):
TMP_DIR = self.TMP_DIR
configs = []
garbage_file = os.path.join(TMP_DIR, '.analects.psd')
buf = open(garbage_file, 'w')
buf.write('wat')
buf.close()
for r, d, f in os.walk(TMP_DIR):
for filela in (x for x in f if x.endswith(('.json', '.ini', 'yaml')) and x.startswith('.analects')):
configs.append(os.path.join(
TMP_DIR, filela))
files = 0
if os.path.exists(os.path.join(TMP_DIR, '.analects.json')):
files += 1
self.assertIn(os.path.join(
TMP_DIR, '.analects.json'), configs)
if os.path.exists(os.path.join(TMP_DIR, '.analects.yaml')):
files += 1
self.assertIn(os.path.join(
TMP_DIR, '.analects.yaml'), configs)
if os.path.exists(os.path.join(TMP_DIR, '.analects.ini')):
files += 1
self.assertIn(os.path.join(TMP_DIR, '.analects.ini'), configs)
self.assertEqual(len(configs), files)
@classmethod
def setUpClass(cls):
cls.TMP_DIR = tempfile.mkdtemp('analects')
@classmethod
def tearDownClass(cls):
if os.path.isdir(cls.TMP_DIR):
shutil.rmtree(cls.TMP_DIR)
class BackboneCollection(collections.MutableSequence):
'''emulate backbone collection
'''
def __init__(self, models=None):
self.attributes = list(models) if models is None else []
def __getitem__(self, index):
return self.attributes[index]
def __setitem__(self, index, value):
self.attributes[index] = value
def __delitem__(self, index):
del self.attributes[index]
def insert(self, index, value):
self.attributes.insert(index, value)
def __len__(self):
return len(self.attributes)
class BackboneModel(collections.MutableMapping):
'''emulate backbone model
'''
def __init__(self, attributes=None):
self.attributes = dict(attributes) if attributes is not None else {}
def __getitem__(self, key):
return self.attributes[key]
def __setitem__(self, key, value):
self.attributes[key] = value
self.dirty = True
def __delitem__(self, key):
del self.attributes[key]
self.dirty = True
def keys(self):
return self.attributes.keys()
def __iter__(self):
return self.attributes.__iter__()
def __len__(self):
return len(self.attributes.keys())
class Repos(BackboneCollection):
""".find, .findWhere returns a ReposProxy class of filtered repos, these
may be .update()'d. make repos underscore.py compatible?
"""
pass
class Repo(BackboneModel):
def __new__(cls, attributes, *args, **kwargs):
vcs_url = attributes['remote_location']
if vcs_url.startswith('git+'):
return super(Repo, cls).__new__(GitRepo, attributes, *args, **kwargs)
if vcs_url.startswith('hg+'):
return super(Repo, cls).__new__(MercurialRepo, attributes, *args, **kwargs)
if vcs_url.startswith('svn+'):
return super(Repo, cls).__new__(SubversionRepo, attributes, *args, **kwargs)
else:
return super(Repo, cls).__new__(cls, attributes, *args, **kwargs)
def __init__(self, attributes=None):
self.attributes = dict(attributes) if attributes is not None else {}
self['path'] = os.path.join(self['parent_path'], self['name'])
from pip.vcs.bazaar import Bazaar
from pip.vcs.git import Git
from pip.vcs.subversion import Subversion
from pip.vcs.mercurial import Mercurial
class GitRepo(Repo, Git):
vcs = 'git'
def __init__(self, arguments, *args, **kwargs):
Repo.__init__(self, arguments, *args, **kwargs)
super(Git, self).__init__(arguments.get(
'remote_location'), *args, **kwargs)
def obtain(self, dest=None):
dest = self['path'] if not dest else dest
return Git.obtain(self, dest)
def update_repo(self, dest=None, rev_options=[]):
dest = self['path'] if not dest else dest
return Git.update(self, dest, rev_options)
class MercurialRepo(Repo, Mercurial):
vcs = 'hg'
def __init__(self, arguments, *args, **kwargs):
Repo.__init__(self, arguments, *args, **kwargs)
super(Mercurial, self).__init__(arguments.get(
'remote_location'), *args, **kwargs)
def obtain(self, dest=None):
dest = self['path'] if not dest else dest
return Mercurial.obtain(self, dest)
def update_repo(self, dest=None, rev_options=[]):
dest = self['path'] if not dest else dest
return Mercurial.update(self, dest, rev_options)
class SubversionRepo(Repo, Subversion):
vcs = 'svn'
def __init__(self, arguments, *args, **kwargs):
Repo.__init__(self, arguments, *args, **kwargs)
super(Subversion, self).__init__(arguments.get(
'remote_location'), *args, **kwargs)
def obtain(self, dest=None):
dest = self['path'] if not dest else dest
return Subversion.obtain(self, dest)
def update_repo(self, dest=None):
dest = self['path'] if not dest else dest
from pip.vcs.subversion import get_rev_options
url, rev = Subversion.get_url_rev(self)
rev_options = get_rev_options(url, rev)
return Subversion.update(self, dest, rev_options)
class ConfigExpandTestCase(ConfigTestCaseBase):
'''
assumes the configuration has been imported into a python dict correctly.
'''
def test_expand_shell_command_after(self):
'''
expands shell commands from string to list
'''
self.maxDiff = None
config = expand_config(self.config_dict)
self.assertDictEqual(config, self.config_dict_expanded)
class ConfigToObjectTestCase(ConfigTestCaseBase):
'''create an individual dictionary for each repository'''
def setUp(self):
SAMPLECONFIG_LIST = [
{
'name': None,
'parent_path': None,
'remote_location': None,
'remotes': []
}
]
super(ConfigToObjectTestCase, self).setUp()
@staticmethod
def get_objects(config):
repo_list = []
for directory, repos in config.iteritems():
for repo, repo_data in repos.iteritems():
repo_dict = {
'name': repo,
'parent_path': directory,
'remote_location': repo_data['repo'],
}
if 'remotes' in repo_data:
repo_dict['remotes'] = []
for remote_name, remote_location in repo_data['remotes'].iteritems():
remote_dict = {
'remote_name': remote_name,
'remote_location': remote_location
}
repo_dict['remotes'].append(remote_dict)
repo_list.append(repo_dict)
return repo_list
def test_to_dictlist(self):
config = self.config_dict_expanded
repo_list = self.get_objects(self.config_dict_expanded)
for r in repo_list:
self.assertIsInstance(r, dict)
self.assertIn('name', r)
self.assertIn('parent_path', r)
self.assertIn('remote_location', r)
if 'remotes' in r:
self.assertIsInstance(r['remotes'], list)
for remote in r['remotes']:
self.assertIsInstance(remote, dict)
self.assertIn('remote_name', remote)
self.assertIn('remote_location', remote)
def test_vcs_url_scheme_to_object(self):
"""test that ``remote_location`` with :class:`Repo` returns a
:class:`GitRepo`, :class:`MercurialRepo` or :class:`SubversionRepo`
object based on the pip-style URL scheme.
"""
git_repo = Repo({
'remote_location': 'git+git://git.myproject.org/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709',
'parent_path': self.TMP_DIR,
'name': 'myproject1'
})
# TODO parent_path and name if duplicated should give an error
self.assertIsInstance(git_repo, GitRepo)
self.assertIsInstance(git_repo, Repo)
hg_repo = Repo({
'remote_location': 'hg+https://hg.myproject.org/MyProject#egg=MyProject',
'parent_path': self.TMP_DIR,
'name': 'myproject2'
})
self.assertIsInstance(hg_repo, MercurialRepo)
self.assertIsInstance(hg_repo, Repo)
svn_repo = Repo({
'remote_location': 'svn+svn://svn.myproject.org/svn/MyProject#egg=MyProject',
'parent_path': self.TMP_DIR,
'name': 'myproject3'
})
self.assertIsInstance(svn_repo, SubversionRepo)
self.assertIsInstance(svn_repo, Repo)
def test_repo_svn(self):
svn_test_repo = os.path.join(self.TMP_DIR, '.svn_test_repo')
svn_repo_name = 'my_svn_project'
svn_repo = Repo({
'remote_location': 'svn+file://' + os.path.join(svn_test_repo, svn_repo_name),
'parent_path': self.TMP_DIR,
'name': svn_repo_name
})
self.assertIsInstance(svn_repo, SubversionRepo)
self.assertIsInstance(svn_repo, Repo)
os.mkdir(svn_test_repo)
subprocess.call(['svnadmin', 'create', svn_repo[
'name']], cwd=svn_test_repo)
self.assertTrue(os.path.exists(svn_test_repo))
svn_checkout_dest = os.path.join(self.TMP_DIR, svn_repo['name'])
#svn_repo.obtain(svn_checkout_dest)
svn_repo.obtain()
testfile_filename = 'testfile.test'
self.assertEqual(svn_repo.get_revision(svn_checkout_dest), 0)
subprocess.call(['touch', testfile_filename], cwd=svn_checkout_dest)
subprocess.call(
['svn', 'add', testfile_filename], cwd=svn_checkout_dest)
subprocess.call(['svn', 'commit', '-m', 'a test file for %s' %
svn_repo['name']], cwd=svn_checkout_dest)
#svn_repo.update_repo(svn_checkout_dest)
svn_repo.update_repo()
self.assertEqual(svn_repo.get_revision(svn_checkout_dest), 1)
self.assertTrue(os.path.exists(svn_checkout_dest))
def test_repo_git(self):
git_test_repo = os.path.join(self.TMP_DIR, '.git_test_repo')
git_repo_name = 'my_git_project'
git_repo = Repo({
'remote_location': 'git+file://' + os.path.join(git_test_repo, git_repo_name),
'parent_path': self.TMP_DIR,
'name': git_repo_name
})
self.assertIsInstance(git_repo, GitRepo)
self.assertIsInstance(git_repo, Repo)
os.mkdir(git_test_repo)
subprocess.call(['git', 'init', git_repo['name']], cwd=git_test_repo)
self.assertTrue(os.path.exists(git_test_repo))
git_checkout_dest = os.path.join(self.TMP_DIR, git_repo['name'])
#git_repo.obtain(git_checkout_dest)
git_repo.obtain()
testfile_filename = 'testfile.test'
subprocess.call(['touch', testfile_filename], cwd=os.path.join(
git_test_repo, git_repo_name))
subprocess.call(['git', 'add', testfile_filename], cwd=os.path.join(
git_test_repo, git_repo_name))
subprocess.call(['git', 'commit', '-m', 'a test file for %s' %
git_repo['name']], cwd=os.path.join(git_test_repo, git_repo_name))
#git_repo.update_repo(git_checkout_dest, ['origin/master'])
git_repo.update_repo(rev_options=['origin/master'])
self.assertEqual(git_repo.get_revision(git_checkout_dest), git_repo.get_revision(
os.path.join(git_test_repo, git_repo_name)))
self.assertTrue(os.path.exists(git_checkout_dest))
def test_repo_mercurial(self):
mercurial_test_repo = os.path.join(
self.TMP_DIR, '.mercurial_test_repo')
mercurial_repo_name = 'my_mercurial_project'
mercurial_repo = Repo({
'remote_location': 'hg+file://' + os.path.join(mercurial_test_repo, mercurial_repo_name),
'parent_path': self.TMP_DIR,
'name': mercurial_repo_name
})
self.assertIsInstance(mercurial_repo, MercurialRepo)
self.assertIsInstance(mercurial_repo, Repo)
os.mkdir(mercurial_test_repo)
subprocess.call(['hg', 'init', mercurial_repo[
'name']], cwd=mercurial_test_repo)
self.assertTrue(os.path.exists(mercurial_test_repo))
mercurial_checkout_dest = os.path.join(
self.TMP_DIR, mercurial_repo['name'])
#mercurial_repo.obtain(mercurial_checkout_dest)
mercurial_repo.obtain()
testfile_filename = 'testfile.test'
subprocess.call(['touch', testfile_filename], cwd=os.path.join(
mercurial_test_repo, mercurial_repo_name))
subprocess.call(['hg', 'add', testfile_filename], cwd=os.path.join(
mercurial_test_repo, mercurial_repo_name))
subprocess.call(['hg', 'commit', '-m', 'a test file for %s' % mercurial_repo[
'name']], cwd=os.path.join(mercurial_test_repo, mercurial_repo_name))
#mercurial_repo.update_repo(mercurial_checkout_dest)
mercurial_repo.update_repo()
self.assertEqual(mercurial_repo.get_revision(mercurial_checkout_dest), mercurial_repo.get_revision(
os.path.join(mercurial_test_repo, mercurial_repo_name)))
self.assertTrue(os.path.exists(mercurial_checkout_dest))
def test_to_repo_objects(self):
repo_list = self.get_objects(self.config_dict_expanded)
for repo_dict in repo_list:
r = Repo(repo_dict)
self.assertIsInstance(r, Repo)
self.assertIn('name', r)
self.assertEqual(r['name'], repo_dict['name'])
self.assertIn('parent_path', r)
self.assertEqual(r['parent_path'], repo_dict['parent_path'])
self.assertIn('remote_location', r)
self.assertEqual(r['remote_location'], repo_dict['remote_location'])
self.assertEqual(r['path'], os.path.join(r['parent_path'], r['name']))
if 'remotes' in r:
self.assertIsInstance(r['remotes'], list)
for remote in r['remotes']:
self.assertIsInstance(remote, dict)
self.assertIn('remote_name', remote)
self.assertIn('remote_location', remote)
@classmethod
def setUpClass(cls):
cls.TMP_DIR = tempfile.mkdtemp('analects')
@classmethod
def tearDownClass(cls):
if os.path.isdir(cls.TMP_DIR):
shutil.rmtree(cls.TMP_DIR)
class TestFabric(object):
""" we may want to skip testing in travis, and offer conditions to pass
if there is no SSH server on the local machine.
see: https://github.com/fabric/fabric/blob/master/.travis.yml
"""
pass
class TestIterateThroughEachObject(object):
"""todo:
iterate through each object and return a list of them.
look into being able to use variation of https://github.com/serkanyersen/underscore.py/blob/master/src/underscore.py
.find / .findWhere to easily look up results in collection lf repos.
"""
pass
class TestVCS(object):
def test_can_get_repository(self):
raise NotImplementedError
class TestGit(TestVCS):
pass
if __name__ == '__main__':
unittest.main(verbosity=2)