Michael Merickel
2018-10-16 8eed333343e4e9e7f11f3aee67299030d6bf2783
commit | author | age
eb0079 1 import sys
bac455 2 import os
CM 3 import shutil
4 import subprocess
5 import tempfile
6 import time
eb0079 7
1b494c 8 try:
cf4ad5 9     import http.client as httplib
MM 10 except ImportError:
1b494c 11     import httplib
25c64c 12
3be1ba 13
bac455 14 class TemplateTest(object):
50a8a0 15     def make_venv(self, directory):  # pragma: no cover
bac455 16         import virtualenv
CM 17         from virtualenv import Logger
0c29cf 18
bac455 19         logger = Logger([(Logger.level_for_integer(2), sys.stdout)])
CM 20         virtualenv.logger = logger
0c29cf 21         virtualenv.create_environment(
2d369c 22             directory, site_packages=False, clear=False
0c29cf 23         )
25c64c 24
50a8a0 25     def install(self, tmpl_name):  # pragma: no cover
bac455 26         try:
CM 27             self.old_cwd = os.getcwd()
28             self.directory = tempfile.mkdtemp()
29             self.make_venv(self.directory)
2ecc60 30             here = os.path.abspath(os.path.dirname(__file__))
2d369c 31             os.chdir(os.path.dirname(os.path.dirname(os.path.dirname(here))))
aa52c5 32             pip = os.path.join(self.directory, 'bin', 'pip')
MM 33             subprocess.check_call([pip, 'install', '-e', '.'])
bac455 34             os.chdir(self.directory)
0e0cb7 35             subprocess.check_call(['bin/pcreate', '-s', tmpl_name, 'Dingle'])
bac455 36             os.chdir('Dingle')
aa52c5 37             subprocess.check_call([pip, 'install', '.[testing]'])
8fe021 38             if tmpl_name == 'alchemy':
0c29cf 39                 populate = os.path.join(
MM 40                     self.directory, 'bin', 'initialize_Dingle_db'
41                 )
8fe021 42                 subprocess.check_call([populate, 'development.ini'])
0c29cf 43             subprocess.check_call(
MM 44                 [os.path.join(self.directory, 'bin', 'py.test')]
45             )
cfb2b5 46             pserve = os.path.join(self.directory, 'bin', 'pserve')
0c29cf 47             for ininame, hastoolbar in (
MM 48                 ('development.ini', True),
49                 ('production.ini', False),
50             ):
cfb2b5 51                 proc = subprocess.Popen([pserve, ininame])
c9c0cc 52                 try:
CM 53                     time.sleep(5)
54                     proc.poll()
55                     if proc.returncode is not None:
56                         raise RuntimeError('%s didnt start' % ininame)
57                     conn = httplib.HTTPConnection('localhost:6543')
58                     conn.request('GET', '/')
59                     resp = conn.getresponse()
60                     assert resp.status == 200, ininame
61                     data = resp.read()
3be1ba 62                     toolbarchunk = b'<div id="pDebug"'
c9c0cc 63                     if hastoolbar:
CM 64                         assert toolbarchunk in data, ininame
65                     else:
25c64c 66                         assert toolbarchunk not in data, ininame
c9c0cc 67                 finally:
fa043a 68                     proc.terminate()
bac455 69         finally:
CM 70             shutil.rmtree(self.directory)
71             os.chdir(self.old_cwd)
72
0c29cf 73
MM 74 if __name__ == '__main__':  # pragma: no cover
973536 75     templates = ['starter', 'alchemy', 'zodb']
810942 76
eb0079 77     for name in templates:
bac455 78         test = TemplateTest()
CM 79         test.install(name)