Skip to content

Commit

Permalink
Fix loading global cfgs (#312)
Browse files Browse the repository at this point in the history
* Fix loading global cfgs + add tests

* Add docs about global config

* Update docs/source/basics.md

Co-Authored-By: michaelboulton <michaelboulton@users.noreply.github.com>

* Update tavern/core.py

Co-Authored-By: michaelboulton <michaelboulton@users.noreply.github.com>

* Split loading config into separate helper

* formatting
  • Loading branch information
michaelboulton authored and benhowes committed Mar 19, 2019
1 parent 2cc9cfd commit d013d20
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
4 changes: 3 additions & 1 deletion docs/source/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1086,14 +1086,16 @@ from tavern.core import run

extra_cfg = {
"variables": {
"key_1": "value":,
"key_1": "value",
"key_2": 123,
}
}

success = run("test_server.tavern.yaml", extra_cfg)
```

An absolute filepath to a configuration file can also be passed.

This is also how things such as strict key checking is controlled via the
`run()` function. Extra keyword arguments that are taken by this function:

Expand Down
52 changes: 45 additions & 7 deletions tavern/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from contextlib2 import ExitStack
from box import Box

from tavern.schemas.files import wrapfile
from .util import exceptions
from .util.dict_util import format_keys
from .util.delay import delay
Expand Down Expand Up @@ -216,7 +217,7 @@ def run_stage(sessions, stage, tavern_box, test_block_config):
"""Run one stage from the test
Args:
sessions (list): List of relevant 'session' objects used for this test
sessions (dict): Dictionary of relevant 'session' objects used for this test
stage (dict): specification of stage to be run
tavern_box (box.Box): Box object containing format variables to be used
in test
Expand Down Expand Up @@ -278,13 +279,8 @@ def _run_pytest(
FutureWarning,
)

if tavern_global_cfg:
global_filename = tavern_global_cfg

pytest_args = pytest_args or []
pytest_args += [in_file]
if tavern_global_cfg:
pytest_args += ["--tavern-global-cfg", global_filename]

if tavern_mqtt_backend:
pytest_args += ["--tavern-mqtt-backend", tavern_mqtt_backend]
Expand All @@ -293,7 +289,49 @@ def _run_pytest(
if tavern_strict:
pytest_args += ["--tavern-strict", tavern_strict]

return pytest.main(args=pytest_args)
with ExitStack() as stack:
if tavern_global_cfg:
global_filename = _get_or_wrap_global_cfg(stack, tavern_global_cfg)
pytest_args += ["--tavern-global-cfg", global_filename]

return pytest.main(args=pytest_args)


def _get_or_wrap_global_cfg(stack, tavern_global_cfg):
"""
Try to parse global configuration from given argument.
Args:
stack (ExitStack): context stack for wrapping file if a dictionary is given
tavern_global_cfg (dict, str): Dictionary or string. It should be a
path to a file or a dictionary with configuration.
Returns:
str: path to global config file
Raises:
InvalidSettingsError: If global config was not of the right type or a given path
does not exist
Todo:
Once python 2 is dropped, allow this to take a 'path like object'
"""
if isinstance(tavern_global_cfg, str):
if not os.path.exists(tavern_global_cfg):
raise exceptions.InvalidSettingsError(
"global config file '{}' does not exist".format(tavern_global_cfg)
)
global_filename = tavern_global_cfg
elif isinstance(tavern_global_cfg, dict):
global_filename = stack.enter_context(wrapfile(tavern_global_cfg))
else:
raise exceptions.InvalidSettingsError(
"Invalid format for global settings - must be dict or path to settings file, was {}".format(
type(tavern_global_cfg)
)
)

return global_filename


def run(in_file, tavern_global_cfg=None, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions tavern/util/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ class InvalidStageReferenceError(TavernException):

class DuplicateStageDefinitionError(TavernException):
"""Stage with the specified ID previously defined"""


class InvalidSettingsError(TavernException):
"""Configuration was passed incorrectly in some fashion"""
12 changes: 12 additions & 0 deletions tests/unit/test_call_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tavern.core import run


class TestBasicRun:
def test_run(self):
run("")

def test_run_with_empty_cfg(self):
run("", {})

def test_run_with_cfg(self):
run("", {"a": 2})

0 comments on commit d013d20

Please sign in to comment.