def main(): from optparse import OptionParser usage = ('Usage: %prog [option] [modules to be tested]\n' 'Modules names have to be given in the form utils.mail (without ' 'pyClanSphere.)\nIf no module names are given, all tests are run') parser = OptionParser(usage=usage) parser.add_option('-c', '--coverage', action='store_true', dest='coverage', help='show coverage information (slow!)') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='show which tests are run') parser.add_option('--real-instance', dest='instance', help='instance to use instead of a temporary one, only use it if you know what you are doing!') options, args = parser.parse_args(sys.argv[1:]) modnames = ['pyClanSphere.' + modname for modname in args] if options.coverage: if coverage is not None: use_coverage = True else: sys.stderr.write("coverage information requires Ned Batchelder's " "coverage.py to be installed!\n") sys.exit(1) else: use_coverage = False # get our instance if options.instance: sys.stdout.write("Opening given instance ... ") sys.stdout.flush() from pyClanSphere.upgrades.webapp import WebUpgrades from pyClanSphere import setup instance = setup(options.instance) if isinstance(instance, WebUpgrades): sys.stderr.write("Please migrate your instance to latest schema first!\n") sys.exit(1) else: sys.stdout.write("Creating temporary instance ... ") sys.stdout.flush() instance, instance_folder = create_temporary_instance() sys.stdout.write("ok\nCollecting tests ... ") sys.stdout.flush() if use_coverage: coverage.erase() coverage.start() s, covermods = suite(instance, modnames, True) else: s = suite(instance, modnames) sys.stdout.write("ok\n") TextTestRunner(verbosity=options.verbose + 1).run(s) if use_coverage: coverage.stop() print '\n\n' + '=' * 25 + ' coverage information ' + '=' * 25 coverage.report(covermods) if not options.instance: try: for root, dirs, files in os.walk(instance_folder, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) except OSError: print "Could not remove all tempfiles, please remove", \ instance_folder, "yourself" pass
def create_temporary_instance(): """Create a sqlite based test instance in a temporary directory""" dbname = 'sqlite://database.db' instance_folder = mkdtemp(prefix='pyclanspheretest') # create database and all tables from pyClanSphere.database import db, init_database e = db.create_engine(dbname, instance_folder) from pyClanSphere.schema import users, user_privileges, privileges init_database(e) # create admin account from pyClanSphere.privileges import CLAN_ADMIN user_id = e.execute(users.insert(), username=u'TestAdmin', pw_hash=gen_pwhash('TestPassWord'), email=u'*****@*****.**', real_name=u'', description=u'', extra={}, display_name='$username' ).inserted_primary_key[0] # insert a privilege for the user privilege_id = e.execute(privileges.insert(), name=CLAN_ADMIN.name ).inserted_primary_key[0] e.execute(user_privileges.insert(), user_id=user_id, privilege_id=privilege_id ) # set up the initial config from pyClanSphere.config import Configuration config_filename = join(instance_folder, 'pyClanSphere.ini') cfg = Configuration(config_filename) t = cfg.edit() t.update( maintenance_mode=False, site_url='http://localtest', secret_key=gen_secret_key(), database_uri=dbname, iid=new_iid() ) t.commit() from pyClanSphere import setup from pyClanSphere.upgrades.webapp import WebUpgrades instance = setup(instance_folder) if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>": # Fast Migration from pyClanSphere.upgrades import ManageDatabase manage = ManageDatabase(instance) upgrade = manage.cmd_upgrade() while True: try: upgrade.next() except StopIteration: break from pyClanSphere._core import _unload_pyClanSphere _unload_pyClanSphere() instance = setup(instance_folder) if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>": sys.stderr.write('Automatic db migration failed, check your scripts!\n') sys.exit(1) return instance, instance_folder
def get_pyClanSphere_instance(self): if not self.instance_path: self.parser.error('you need to pass your pyClanSphere instance path.') if not hasattr(self, 'pyClanSphere_instance'): self.pyClanSphere_instance = setup(expanduser(self.instance_path)) return self.pyClanSphere_instance