def pytest_collect_file(parent, path): """On collecting files, get any files that end in .tavern.yaml or .tavern.yml as tavern test files """ if int(pytest.__version__.split(".")[0]) < 5: raise exceptions.TavernException("Only pytest >=5 is supported") pattern = get_option_generic(parent.config, "tavern-file-path-regex", r".+\.tavern\.ya?ml$") if isinstance(pattern, list): if len(pattern) != 1: raise exceptions.InvalidConfigurationException( "tavern-file-path-regex must have exactly one option") pattern = pattern[0] try: compiled = re.compile(pattern) except Exception as e: # pylint: disable=broad-except raise exceptions.InvalidConfigurationException(e) from e match_tavern_file = compiled.search if match_tavern_file(path.strpath): return YamlFile(path, parent) return None
def setting_for(self, section): """Provides a string-based way of getting strict settings for a section""" try: return getattr(self, section) except AttributeError as e: raise exceptions.InvalidConfigurationException( "No setting for '{}'".format(section)) from e
def from_options(cls, options): if isinstance(options, str): options = [options] elif not isinstance(options, list): raise exceptions.InvalidConfigurationException( "'strict' setting should be a list of strings") parsed = [validate_and_parse_option(key) for key in options] return cls(**{i.section: i for i in parsed})
def _load_global_strictness(pytest_config): """Load the global 'strictness' setting""" strict = get_option_generic(pytest_config, "tavern-strict", []) if isinstance(strict, list): valid_keys = ["body", "headers", "redirect_query_params"] if any(i not in valid_keys for i in strict): msg = "Invalid values for 'strict' given - expected one of {}, got {}".format( valid_keys, strict ) raise exceptions.InvalidConfigurationException(msg) return strict
def validate_and_parse_option(key): regex = r"(?P<section>{})(:(?P<setting>on|off))?".format( "|".join(valid_keys)) match = re.fullmatch(regex, key) if not match: raise exceptions.InvalidConfigurationException( "Invalid value for 'strict' given - expected one of {}, got '{}'". format(["{}[:on/off]".format(key) for key in valid_keys], key)) as_dict = match.groupdict() return _StrictOption(as_dict["section"], setting_factory(as_dict["setting"]))
def extract_strict_setting(strict): """Takes either a bool, StrictOption, or a StrictSetting and return the bool representation and StrictSetting representation""" if isinstance(strict, StrictSetting): strict_setting = strict strict = strict == StrictSetting.ON elif isinstance(strict, StrictOption): strict_setting = strict.setting strict = strict.is_on() elif isinstance(strict, bool): strict_setting = strict_setting_factory(str(strict)) elif strict is None: strict = False strict_setting = strict_setting_factory("false") else: raise exceptions.InvalidConfigurationException( "Unable to parse strict setting '{}' of type '{}'".format( strict, type(strict) ) ) return strict, strict_setting
def validate_and_parse_option(key): regex = re.compile( "(?P<section>{sections})(:(?P<setting>{switches}))?".format( sections="|".join(valid_keys), switches="|".join(valid_switches) ) ) match = regex.fullmatch(key) if not match: raise exceptions.InvalidConfigurationException( "Invalid value for 'strict' given - expected one of {}, got '{}'".format( ["{}[:on/off]".format(key) for key in valid_keys], key ) ) as_dict = match.groupdict() if as_dict["section"] != "json" and as_dict["setting"] == "list_any_order": logger.warning( "Using 'list_any_order' key outside of 'json' section has no meaning" ) return StrictOption(as_dict["section"], strict_setting_factory(as_dict["setting"]))