def app(username, password): tmp = make_tmp() with open(os.path.join(tmp, 'static.json'), 'w') as f: json.dump( { 'user': username, 'password': generate_password_hash(password), 'main_log': "main.log" }, f) yield create_app({'TESTING': True, 'DATA_DIR': tmp}) shutil.rmtree(tmp)
# Note: This function will be removed during deployment. def extra_files(): """ Watch files and reload the server when they change. """ import os extra_dirs = ['frontend/static', 'frontend/templates'] extra_files = extra_dirs[:] for extra_dir in extra_dirs: for dirname, dirs, files in os.walk(extra_dir): for filename in files: filename = os.path.join(dirname, filename) if os.path.isfile(filename): extra_files.append(filename) return extra_files if __name__ == '__main__': app = create_app() run_simple(hostname=app.config['SERVER_HOSTNAME'], port=app.config['SERVER_PORT'], application=app, use_reloader=app.debug, extra_files=extra_files())
from werkzeug.contrib.fixers import ProxyFix from frontend import create_app application = create_app() application.wsgi_app = ProxyFix(application.wsgi_app)
def test_development_environment_sets_secret_to_dev(environment): environment['FLASK_ENV'] = "development" assert create_app().config['SECRET_KEY'] == 'dev'
def test_default_creation_uses_instance_path_as_data_dir(): app = create_app() assert app.config['DATA_DIR'] == app.instance_path
def test_create_instance_path(makedirs): app = create_app() assert makedirs.path == app.instance_path and makedirs.exist_ok
def test_creating_a_test_environment(): app = create_app({'TESTING': True}) assert app.testing and app.config['SECRET_KEY'] == 'dev'
def test_load_config_from_json_by_default(from_json): create_app() assert from_json.file == "config.json" and from_json.silent
def test_not_using_dev_secret_by_default(): assert 'dev' != create_app().config['SECRET_KEY']
def test_creating_non_test_env_by_default(): assert not create_app().testing
Watch files and reload the server when they change. """ import os extra_dirs = [ 'frontend/static', 'frontend/templates' ] extra_files = extra_dirs[:] for extra_dir in extra_dirs: for dirname, dirs, files in os.walk(extra_dir): for filename in files: filename = os.path.join(dirname, filename) if os.path.isfile(filename): extra_files.append(filename) return extra_files if __name__ == '__main__': app = create_app() run_simple( hostname=app.config['SERVER_HOSTNAME'], port=app.config['SERVER_PORT'], application=app, use_reloader=app.debug, extra_files=extra_files() )
from frontend import create_app from flask_script import Manager, Server app = create_app('develop') manager = Manager(app) manager.add_command('server', Server) @manager.command def test(): """Run the unit tests.""" import unittest tests = unittest.TestLoader().discover('tests') unittest.TextTestRunner(verbosity=2).run(tests) if __name__ == '__main__': manager.run()
def app(): _app = frontend.create_app(test_settings) ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
def create_app(self): app = create_app(TestingConfig) return app
"""Application entry point.""" import frontend app = frontend.create_app() if __name__ == "__main__": app.run(host="0.0.0.0")