def test_can_validate_feature_flags(sample_app): # The _features_used is marked internal because we don't want # chalice users to access it, but this attribute is intended to be # accessed by anything within the chalice codebase. sample_app._features_used.add('SOME_NEW_FEATURE') with pytest.raises(ExperimentalFeatureError): validate_feature_flags(sample_app) # Now if we opt in, validation is fine. sample_app.experimental_feature_flags.add('SOME_NEW_FEATURE') try: validate_feature_flags(sample_app) except ExperimentalFeatureError: raise AssertionError("App was not suppose to raise an error when " "opting in to features via a feature flag.")
def load_chalice_app(self, environment_variables=None, validate_feature_flags=True): # type: (Optional[MutableMapping], Optional[bool]) -> Chalice # validate_features indicates that we should validate that # any expiremental features used have the appropriate feature flags. if self.project_dir not in sys.path: sys.path.insert(0, self.project_dir) # The vendor directory has its contents copied up to the top level of # the deployment package. This means that imports will work in the # lambda function as if the vendor directory is on the python path. # For loading the config locally we must add the vendor directory to # the path so it will be treated the same as if it were running on # lambda. vendor_dir = os.path.join(self.project_dir, 'vendor') if os.path.isdir(vendor_dir) and vendor_dir not in sys.path: # This is a tradeoff we have to make for local use. # The common use case of vendor/ is to include # extension modules built for AWS Lambda. If you're # running on a non-linux dev machine, then attempting # to import these files will raise exceptions. As # a workaround, the vendor is added to the end of # sys.path so it's after `./lib/site-packages`. # This gives you a change to install the correct # version locally and still keep the lambda # specific one in vendor/ sys.path.append(vendor_dir) if environment_variables is not None: self._environ.update(environment_variables) try: app = importlib.import_module('app') chalice_app = getattr(app, 'app') except SyntaxError as e: message = ('Unable to import your app.py file:\n\n' 'File "%s", line %s\n' ' %s\n' 'SyntaxError: %s') % (getattr( e, 'filename'), e.lineno, e.text, e.msg) raise RuntimeError(message) if validate_feature_flags: validate.validate_feature_flags(chalice_app) return chalice_app
def load_chalice_app(self, environment_variables=None, validate_feature_flags=True): # type: (Optional[MutableMapping], Optional[bool]) -> Chalice # validate_features indicates that we should validate that # any expiremental features used have the appropriate feature flags. if self.project_dir not in sys.path: sys.path.insert(0, self.project_dir) # The vendor directory has its contents copied up to the top level of # the deployment package. This means that imports will work in the # lambda function as if the vendor directory is on the python path. # For loading the config locally we must add the vendor directory to # the path so it will be treated the same as if it were running on # lambda. vendor_dir = os.path.join(self.project_dir, 'vendor') if os.path.isdir(vendor_dir) and vendor_dir not in sys.path: # This is a tradeoff we have to make for local use. # The common use case of vendor/ is to include # extension modules built for AWS Lambda. If you're # running on a non-linux dev machine, then attempting # to import these files will raise exceptions. As # a workaround, the vendor is added to the end of # sys.path so it's after `./lib/site-packages`. # This gives you a change to install the correct # version locally and still keep the lambda # specific one in vendor/ sys.path.append(vendor_dir) if environment_variables is not None: self._environ.update(environment_variables) try: app = importlib.import_module('app') chalice_app = getattr(app, 'app') except SyntaxError as e: message = ( 'Unable to import your app.py file:\n\n' 'File "%s", line %s\n' ' %s\n' 'SyntaxError: %s' ) % (getattr(e, 'filename'), e.lineno, e.text, e.msg) raise RuntimeError(message) if validate_feature_flags: validate.validate_feature_flags(chalice_app) return chalice_app