summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2019-07-09 16:56:22 +0200
committerJürg Billeter <j@bitron.ch>2019-07-16 15:36:10 +0200
commit4f1d1b6ceb0a4eb19ad9f1f8c0f5dcd08eba28e4 (patch)
tree30976603f12dd3c6a1b729e16ccbe27ee19b5095
parentb0248da3b4b4d4d9a59636e442ffa6d647fe3dca (diff)
downloadbuildstream-4f1d1b6ceb0a4eb19ad9f1f8c0f5dcd08eba28e4.tar.gz
tests/internals/loader.py: Use dummy_context()
-rw-r--r--tests/internals/loader.py49
1 files changed, 17 insertions, 32 deletions
diff --git a/tests/internals/loader.py b/tests/internals/loader.py
index a4ebdb9ac..9af2bf161 100644
--- a/tests/internals/loader.py
+++ b/tests/internals/loader.py
@@ -1,11 +1,13 @@
+from contextlib import contextmanager
import os
import pytest
from buildstream._exceptions import LoadError, LoadErrorReason
-from buildstream._context import Context
from buildstream._project import Project
from buildstream._loader import MetaElement
+from tests.testutils import dummy_context
+
DATA_DIR = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
@@ -13,16 +15,11 @@ DATA_DIR = os.path.join(
)
-def dummy_handler(message, is_silenced):
- pass
-
-
+@contextmanager
def make_loader(basedir):
- context = Context()
- context.load(config=os.devnull)
- context.messenger.set_message_handler(dummy_handler)
- project = Project(basedir, context)
- return project.loader
+ with dummy_context() as context:
+ project = Project(basedir, context)
+ yield project.loader
##############################################################
@@ -32,21 +29,18 @@ def make_loader(basedir):
def test_one_file(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
+ with make_loader(basedir) as loader:
+ element = loader.load(['elements/onefile.bst'])[0]
- element = loader.load(['elements/onefile.bst'])[0]
-
- assert isinstance(element, MetaElement)
- assert element.kind == 'pony'
+ assert isinstance(element, MetaElement)
+ assert element.kind == 'pony'
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile'))
def test_missing_file(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
-
- with pytest.raises(LoadError) as exc:
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load(['elements/missing.bst'])
assert exc.value.reason == LoadErrorReason.MISSING_FILE
@@ -56,9 +50,7 @@ def test_missing_file(datafiles):
def test_invalid_reference(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
-
- with pytest.raises(LoadError) as exc:
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load(['elements/badreference.bst'])
assert exc.value.reason == LoadErrorReason.INVALID_YAML
@@ -68,9 +60,7 @@ def test_invalid_reference(datafiles):
def test_invalid_yaml(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
-
- with pytest.raises(LoadError) as exc:
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load(['elements/badfile.bst'])
assert exc.value.reason == LoadErrorReason.INVALID_YAML
@@ -82,8 +72,7 @@ def test_fail_fullpath_target(datafiles):
basedir = str(datafiles)
fullpath = os.path.join(basedir, 'elements', 'onefile.bst')
- with pytest.raises(LoadError) as exc:
- loader = make_loader(basedir)
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load([fullpath])
assert exc.value.reason == LoadErrorReason.INVALID_DATA
@@ -93,9 +82,7 @@ def test_fail_fullpath_target(datafiles):
def test_invalid_key(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
-
- with pytest.raises(LoadError) as exc:
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load(['elements/invalidkey.bst'])
assert exc.value.reason == LoadErrorReason.INVALID_DATA
@@ -105,9 +92,7 @@ def test_invalid_key(datafiles):
def test_invalid_directory_load(datafiles):
basedir = str(datafiles)
- loader = make_loader(basedir)
-
- with pytest.raises(LoadError) as exc:
+ with make_loader(basedir) as loader, pytest.raises(LoadError) as exc:
loader.load(['elements/'])
assert exc.value.reason == LoadErrorReason.LOADING_DIRECTORY