Skip to content

Commit

Permalink
Minor changes required for plugins to work as expected (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboulton authored Mar 27, 2024
1 parent a7546c4 commit 71d0c2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
55 changes: 28 additions & 27 deletions tavern/_core/schema/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SchemaCache:
"""Caches loaded schemas"""

def __init__(self) -> None:
self._loaded: Dict[str, dict] = {}
self._loaded: Dict[str, Dict] = {}

def _load_base_schema(self, schema_filename):
try:
Expand All @@ -33,37 +33,37 @@ def _load_base_schema(self, schema_filename):

return self._loaded[schema_filename]

def _load_schema_with_plugins(self, schema_filename):
def _load_schema_with_plugins(self, schema_filename: str) -> Dict:
mangled = f"{schema_filename}-plugins"

try:
return self._loaded[mangled]
except KeyError:
plugins = load_plugins()
base_schema = copy.deepcopy(self._load_base_schema(schema_filename))

logger.debug("Adding plugins to schema: %s", plugins)

for p in plugins:
try:
plugin_schema = p.plugin.schema
except AttributeError:
# Don't require a schema
logger.debug("No schema defined for %s", p.name)
else:
base_schema["properties"].update(
plugin_schema.get("properties", {})
)

self._loaded[mangled] = base_schema
return self._loaded[mangled]
pass

plugins = load_plugins()
base_schema = copy.deepcopy(self._load_base_schema(schema_filename))

logger.debug("Adding plugins to schema: %s", [p.name for p in plugins])

for p in plugins:
try:
plugin_schema = p.plugin.schema
except AttributeError:
# Don't require a schema
logger.debug("No schema defined for %s", p.name)
else:
base_schema["properties"].update(plugin_schema.get("properties", {}))

self._loaded[mangled] = base_schema
return self._loaded[mangled]

def __call__(self, schema_filename, with_plugins):
def __call__(self, schema_filename: str, with_plugins: bool):
"""Load the schema file and cache it for future use
Args:
schema_filename (str): filename of schema
with_plugins (bool): Whether to load plugin schema into this schema as well
schema_filename: filename of schema
with_plugins: Whether to load plugin schema into this schema as well
Returns:
loaded schema
Expand All @@ -83,8 +83,8 @@ def __call__(self, schema_filename, with_plugins):
def verify_pykwalify(to_verify, schema) -> None:
"""Verify a generic file against a given pykwalify schema
Args:
to_verify (dict): Filename of source tests to check
schema (dict): Schema to verify against
to_verify: Filename of source tests to check
schema: Schema to verify against
Raises:
BadSchemaError: Schema did not match
"""
Expand All @@ -111,7 +111,7 @@ def wrapfile(to_wrap):
"""Wrap a dictionary into a temporary yaml file
Args:
to_wrap (dict): Dictionary to write to temporary file
to_wrap: Dictionary to write to temporary file
Yields:
filename: name of temporary file object that will be destroyed at the end of the
Expand All @@ -136,7 +136,8 @@ def verify_tests(test_spec: Mapping, with_plugins: bool = True) -> None:
Load schema file once. Requires some caching of the file
Args:
test_spec (dict): Test in dictionary form
test_spec: Test in dictionary form
with_plugins: Whether to load plugin schema into this schema as well
Raises:
BadSchemaError: Schema did not match
Expand Down
11 changes: 6 additions & 5 deletions tavern/_plugins/rest/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
logger: logging.Logger = logging.getLogger(__name__)


def get_request_args(rspec: Dict, test_block_config: TestConfig) -> dict:
def get_request_args(rspec: Dict, test_block_config: TestConfig) -> Dict:
"""Format the test spec given values inthe global config
Todo:
Expand Down Expand Up @@ -150,8 +150,6 @@ def add_request_args(keys, optional):
# Ones that are required and are enforced to be present by the schema
required_in_file = ["method", "url"]

optional_with_default = {"verify": True, "stream": False}

add_request_args(["file_body"], True)
add_request_args(required_in_file, False)
add_request_args(RestRequest.optional_in_file, True)
Expand Down Expand Up @@ -184,8 +182,11 @@ def add_request_args(keys, optional):
if isinstance(value, dict):
request_args["params"][key] = quote_plus(json.dumps(value))

for key, val in optional_with_default.items():
request_args[key] = fspec.get(key, val)
optional = {"verify", "stream"}

for key in optional:
if key in fspec:
request_args[key] = fspec[key]

# TODO
# requests takes all of these - we need to parse the input to get them
Expand Down

0 comments on commit 71d0c2a

Please sign in to comment.