<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import flask <EOL> import pkgutil <EOL> import unittest <EOL> from contextlib import contextmanager <EOL> from flask . testsuite import FlaskTestCase <EOL> TEST_KEY = '<STR_LIT:foo>' <EOL> SECRET_KEY = '<STR_LIT>' <EOL> class ConfigTestCase ( FlaskTestCase ) : <EOL> def common_object_test ( self , app ) : <EOL> self . assert_equal ( app . secret_key , '<STR_LIT>' ) <EOL> self . assert_equal ( app . config [ '<STR_LIT>' ] , '<STR_LIT:foo>' ) <EOL> self . assert_not_in ( '<STR_LIT>' , app . config ) <EOL> def test_config_from_file ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> app . config . from_pyfile ( __file__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' ) <EOL> self . common_object_test ( app ) <EOL> def test_config_from_object ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> app . config . from_object ( __name__ ) <EOL> self . common_object_test ( app ) <EOL> def test_config_from_class ( self ) : <EOL> class Base ( object ) : <EOL> TEST_KEY = '<STR_LIT:foo>' <EOL> class Test ( Base ) : <EOL> SECRET_KEY = '<STR_LIT>' <EOL> app = flask . Flask ( __name__ ) <EOL> app . config . from_object ( Test ) <EOL> self . common_object_test ( app ) <EOL> def test_config_from_envvar ( self ) : <EOL> env = os . environ <EOL> try : <EOL> os . environ = { } <EOL> app = flask . Flask ( __name__ ) <EOL> try : <EOL> app . config . from_envvar ( '<STR_LIT>' ) <EOL> except RuntimeError as e : <EOL> self . assert_true ( "<STR_LIT>" in str ( e ) ) <EOL> else : <EOL> self . assert_true ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> self . assert_false ( app . config . from_envvar ( '<STR_LIT>' , silent = True ) ) <EOL> os . environ = { '<STR_LIT>' : __file__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' } <EOL> self . assert_true ( app . config . from_envvar ( '<STR_LIT>' ) ) <EOL> self . common_object_test ( app ) <EOL> finally : <EOL> os . environ = env <EOL> def test_config_from_envvar_missing ( self ) : <EOL> env = os . environ <EOL> try : <EOL> os . environ = { '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> app = flask . Flask ( __name__ ) <EOL> app . config . from_envvar ( '<STR_LIT>' ) <EOL> except IOError as e : <EOL> msg = str ( e ) <EOL> self . assert_true ( msg . startswith ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . assert_true ( msg . endswith ( "<STR_LIT>" ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . assertFalse ( app . config . from_envvar ( '<STR_LIT>' , silent = True ) ) <EOL> finally : <EOL> os . environ = env <EOL> def test_config_missing ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> try : <EOL> app . config . from_pyfile ( '<STR_LIT>' ) <EOL> except IOError as e : <EOL> msg = str ( e ) <EOL> self . assert_true ( msg . startswith ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . assert_true ( msg . endswith ( "<STR_LIT>" ) ) <EOL> else : <EOL> self . assert_true ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> self . assert_false ( app . config . from_pyfile ( '<STR_LIT>' , silent = True ) ) <EOL> def test_session_lifetime ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assert_equal ( app . permanent_session_lifetime . seconds , <NUM_LIT> ) <EOL> class LimitedLoaderMockWrapper ( object ) : <EOL> def __init__ ( self , loader ) : <EOL> self . loader = loader <EOL> def __getattr__ ( self , name ) : <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> msg = '<STR_LIT>' % name <EOL> raise AttributeError ( msg ) <EOL> return getattr ( self . loader , name ) <EOL> @ contextmanager <EOL> def patch_pkgutil_get_loader ( wrapper_class = LimitedLoaderMockWrapper ) : <EOL> """<STR_LIT>""" <EOL> old_get_loader = pkgutil . get_loader <EOL> def get_loader ( * args , ** kwargs ) : <EOL> return wrapper_class ( old_get_loader ( * args , ** kwargs ) ) <EOL> try : <EOL> pkgutil . get_loader = get_loader <EOL> yield <EOL> finally : <EOL> pkgutil . get_loader = old_get_loader <EOL> class InstanceTestCase ( FlaskTestCase ) : <EOL> def test_explicit_instance_paths ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> try : <EOL> flask . Flask ( __name__ , instance_path = '<STR_LIT>' ) <EOL> except ValueError as e : <EOL> self . assert_in ( '<STR_LIT>' , str ( e ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> app = flask . Flask ( __name__ , instance_path = here ) <EOL> self . assert_equal ( app . instance_path , here ) <EOL> def test_main_module_paths ( self ) : <EOL> from main_app import app <EOL> here = os . path . abspath ( os . getcwd ( ) ) <EOL> self . assert_equal ( app . instance_path , os . path . join ( here , '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_uninstalled_module_paths ( self ) : <EOL> from config_module_app import app <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> self . assert_equal ( app . instance_path , os . path . join ( here , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_uninstalled_package_paths ( self ) : <EOL> from config_package_app import app <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> self . assert_equal ( app . instance_path , os . path . join ( here , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_installed_module_paths ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> site_packages = os . path . join ( expected_prefix , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( site_packages ) <EOL> try : <EOL> import site_app <EOL> self . assert_equal ( site_app . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( site_packages ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_installed_module_paths_with_limited_loader ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> site_packages = os . path . join ( expected_prefix , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( site_packages ) <EOL> with patch_pkgutil_get_loader ( ) : <EOL> try : <EOL> import site_app <EOL> self . assert_equal ( site_app . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( site_packages ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_installed_package_paths ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> installed_path = os . path . join ( expected_prefix , '<STR_LIT:path>' ) <EOL> sys . path . append ( installed_path ) <EOL> try : <EOL> import installed_package <EOL> self . assert_equal ( installed_package . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( installed_path ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_installed_package_paths_with_limited_loader ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> installed_path = os . path . join ( expected_prefix , '<STR_LIT:path>' ) <EOL> sys . path . append ( installed_path ) <EOL> with patch_pkgutil_get_loader ( ) : <EOL> try : <EOL> import installed_package <EOL> self . assert_equal ( installed_package . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( installed_path ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_prefix_package_paths ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> site_packages = os . path . join ( expected_prefix , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( site_packages ) <EOL> try : <EOL> import site_package <EOL> self . assert_equal ( site_package . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( site_packages ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_prefix_package_paths_with_limited_loader ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> site_packages = os . path . join ( expected_prefix , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path . append ( site_packages ) <EOL> with patch_pkgutil_get_loader ( ) : <EOL> try : <EOL> import site_package <EOL> self . assert_equal ( site_package . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( site_packages ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def test_egg_installed_paths ( self ) : <EOL> here = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> expected_prefix = os . path . join ( here , '<STR_LIT>' ) <EOL> real_prefix , sys . prefix = sys . prefix , expected_prefix <EOL> site_packages = os . path . join ( expected_prefix , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> egg_path = os . path . join ( site_packages , '<STR_LIT>' ) <EOL> sys . path . append ( site_packages ) <EOL> sys . path . append ( egg_path ) <EOL> try : <EOL> import site_egg <EOL> self . assert_equal ( site_egg . app . instance_path , <EOL> os . path . join ( expected_prefix , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> finally : <EOL> sys . prefix = real_prefix <EOL> sys . path . remove ( site_packages ) <EOL> sys . path . remove ( egg_path ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( ConfigTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( InstanceTestCase ) ) <EOL> return suite </s>
