Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_format_environ_empty(self):
val = "{ENV_VARS.nothing_variable:s}"
variables = {}
with pytest.raises(exceptions.MissingFormatError):
format_keys(val, variables)
for (_, field_name, _, _) in would_format:
if field_name is None:
continue
try:
would_replace = formatter.get_field(field_name, [], box_vars)[0]
except KeyError as e:
logger.error(
"Failed to resolve string [%s] with variables [%s]", to_format, box_vars
)
logger.error("Key(s) not found in format: %s", field_name)
raise_from(exceptions.MissingFormatError(field_name), e)
except IndexError as e:
logger.error("Empty format values are invalid")
raise_from(exceptions.MissingFormatError(field_name), e)
else:
if not isinstance(would_replace, (str, ustr, int, float)):
logger.warning(
"Formatting '%s' will result in it being coerced to a string (it is a %s)",
field_name,
type(would_replace),
)
return to_format.format(**box_vars)
format_variables = list(read_formatted_vars(code_lines))
keys = self._get_available_format_keys()
missing = []
# Print out values of format variables, like Pytest prints out the
# values of function call variables
tw.line("Format variables:", white=True, bold=True)
for var in format_variables:
if re.match(r"^\s*\{\}\s*", var):
continue
try:
value_at_call = format_keys(var, keys)
except exceptions.MissingFormatError:
missing.append(var)
value_at_call = "???"
white = False
red = True
else:
white = True
red = False
line = " {} = '{}'".format(var[1:-1], value_at_call)
tw.line(line, white=white, red=red) # pragma: no cover
return missing
# skipif or parametrize (for now)
for markname, extra_arg in m.items():
# NOTE
# cannot do 'skipif' and rely on a parametrized
# argument.
try:
extra_arg = _format_without_inner(extra_arg, fmt_vars)
except exceptions.MissingFormatError as e:
msg = "Tried to use mark '{}' (with value '{}') in test '{}' but one or more format variables was not in any configuration file used by the test".format(
markname, extra_arg, test_name
)
# NOTE
# we could continue and let it fail in the test, but
# this gives a better indication of what actually
# happened (even if it is difficult to test)
raise_from(exceptions.MissingFormatError(msg), e)
else:
pytest_marks.append(getattr(pytest.mark, markname)(extra_arg))
formatted_marks.append({markname: extra_arg})
else:
raise exceptions.BadSchemaError("Unexpected mark type '{}'".format(type(m)))
return pytest_marks, formatted_marks
global_cfg = load_global_cfg(self.config)
fmt_vars.update(**global_cfg.get("variables", {}))
included = test_spec.get("includes", [])
for i in included:
fmt_vars.update(**i.get("variables", {}))
# Needed if something in a config file uses tavern.env_vars
tavern_box = Box({"tavern": {"env_vars": dict(os.environ)}})
try:
fmt_vars = _format_without_inner(fmt_vars, tavern_box)
except exceptions.MissingFormatError as e:
# eg, if we have {tavern.env_vars.DOESNT_EXIST}
msg = "Tried to use tavern format variable that did not exist"
raise_from(exceptions.MissingFormatError(msg), e)
return fmt_vars
# skipif or parametrize (for now)
for markname, extra_arg in m.items():
# NOTE
# cannot do 'skipif' and rely on a parametrized
# argument.
try:
extra_arg = format_keys(extra_arg, fmt_vars)
except exceptions.MissingFormatError as e:
msg = "Tried to use mark '{}' (with value '{}') in test '{}' but one or more format variables was not in any configuration file used by the test".format(
markname, extra_arg, test_spec["test_name"]
)
# NOTE
# we could continue and let it fail in the test, but
# this gives a better indication of what actually
# happened (even if it is difficult to test)
raise_from(exceptions.MissingFormatError(msg), e)
else:
pytest_marks.append(
getattr(pytest.mark, markname)(extra_arg)
)
formatted_marks.append({markname: extra_arg})
# Do this after we've added all the other marks so doing
# things like selecting on mark names still works even
# after parametrization
parametrize_marks = [
i for i in formatted_marks if isinstance(i, dict) and "parametrize" in i
]
if parametrize_marks:
# no 'yield from' in python 2...
for new_item in self.get_parametrized_items(
test_spec, parametrize_marks, pytest_marks