diff options
author | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2019-01-17 00:26:43 +0000 |
---|---|---|
committer | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2019-01-17 00:26:43 +0000 |
commit | d34a4fd17028e87d5b9402e59e84b803aa475635 (patch) | |
tree | 0ff39d5aaac961d3a5f226558dd8f9f61681e481 /tests/internals/loader.py | |
parent | 5e9598c51e2bf6bc0a5448ad69ea4757e9459c5e (diff) | |
parent | 3bf40cf335616bc970645524b913117efc975190 (diff) | |
download | buildstream-d34a4fd17028e87d5b9402e59e84b803aa475635.tar.gz |
Merge branch 'tristan/organizing-tests' into 'master'
General refactor in tests directory
See merge request BuildStream/buildstream!1062
Diffstat (limited to 'tests/internals/loader.py')
-rw-r--r-- | tests/internals/loader.py | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/internals/loader.py b/tests/internals/loader.py new file mode 100644 index 000000000..87a5c3b35 --- /dev/null +++ b/tests/internals/loader.py @@ -0,0 +1,112 @@ +import os +import pytest + +from buildstream._exceptions import LoadError, LoadErrorReason +from buildstream._context import Context +from buildstream._project import Project +from buildstream._loader import Loader, MetaElement + +DATA_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'loader', +) + + +def dummy_handler(message, context): + pass + + +def make_loader(basedir): + context = Context() + context.load(config=os.devnull) + context.set_message_handler(dummy_handler) + project = Project(basedir, context) + return project.loader + + +############################################################## +# Basics: Test behavior loading the simplest of projects # +############################################################## +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_one_file(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + element = loader.load(['elements/onefile.bst'])[0] + + assert(isinstance(element, MetaElement)) + assert(element.kind == 'pony') + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_missing_file(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + with pytest.raises(LoadError) as exc: + element = loader.load(['elements/missing.bst'])[0] + + assert (exc.value.reason == LoadErrorReason.MISSING_FILE) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_invalid_reference(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + with pytest.raises(LoadError) as exc: + element = loader.load(['elements/badreference.bst'])[0] + + assert (exc.value.reason == LoadErrorReason.INVALID_YAML) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_invalid_yaml(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + with pytest.raises(LoadError) as exc: + element = loader.load(['elements/badfile.bst'])[0] + + assert (exc.value.reason == LoadErrorReason.INVALID_YAML) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_fail_fullpath_target(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + fullpath = os.path.join(basedir, 'elements', 'onefile.bst') + + with pytest.raises(LoadError) as exc: + loader = make_loader(basedir) + loader.load([fullpath]) + + assert (exc.value.reason == LoadErrorReason.INVALID_DATA) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_invalid_key(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + with pytest.raises(LoadError) as exc: + element = loader.load(['elements/invalidkey.bst'])[0] + + assert (exc.value.reason == LoadErrorReason.INVALID_DATA) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'onefile')) +def test_invalid_directory_load(datafiles): + + basedir = os.path.join(datafiles.dirname, datafiles.basename) + loader = make_loader(basedir) + + with pytest.raises(LoadError) as exc: + element = loader.load(['elements/'])[0] + + assert (exc.value.reason == LoadErrorReason.LOADING_DIRECTORY) |