summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Maat <tristan.maat@codethink.co.uk>2017-09-13 11:19:17 +0100
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2017-09-14 14:36:32 -0400
commit65dcf0a5c05ea98f485cf849e2172aceb292fce9 (patch)
tree8c9f2fe11ecd3fd5566543b6bb33976acc0c9051
parent28456348c01b6c949f9e9139bc75cc2e2e3f6f08 (diff)
downloadbuildstream-65dcf0a5c05ea98f485cf849e2172aceb292fce9.tar.gz
Add tests
-rw-r--r--tests/plugins/third_party.py50
-rw-r--r--tests/plugins/third_party/third_party_element/__init__.py0
-rw-r--r--tests/plugins/third_party/third_party_element/foop.py9
-rw-r--r--tests/plugins/third_party/third_party_source/__init__.py0
-rw-r--r--tests/plugins/third_party/third_party_source/foop.py9
-rw-r--r--tests/testutils/setuptools.py43
6 files changed, 111 insertions, 0 deletions
diff --git a/tests/plugins/third_party.py b/tests/plugins/third_party.py
new file mode 100644
index 000000000..b82f112fe
--- /dev/null
+++ b/tests/plugins/third_party.py
@@ -0,0 +1,50 @@
+import os
+import pytest
+import pkg_resources
+
+from pluginbase import PluginBase
+from buildstream._elementfactory import ElementFactory
+from buildstream._sourcefactory import SourceFactory
+
+from tests.testutils.setuptools import entry_fixture
+
+DATA_DIR = os.path.join(
+ os.path.dirname(os.path.realpath(__file__)),
+ 'third_party'
+)
+
+
+# Simple fixture to create a PluginBase object that
+# we use for loading plugins.
+@pytest.fixture()
+def plugin_fixture():
+ return {
+ 'base': PluginBase(package='buildstream.plugins')
+ }
+
+
+##################################################################
+# Tests #
+##################################################################
+# Test that external element plugin loading works.
+@pytest.mark.datafiles(os.path.join(DATA_DIR, 'third_party_element'))
+def test_custom_pip_element(plugin_fixture, entry_fixture, datafiles):
+ factory = ElementFactory(plugin_fixture['base'], [])
+ assert(isinstance(factory, ElementFactory))
+
+ entry_fixture(datafiles, 'buildstream.plugins', 'third_party_element:foop')
+
+ foo_type, _ = factory.lookup('third_party_element:foop')
+ assert(foo_type.__name__ == 'FooElement')
+
+
+# Test that external source plugin loading works.
+@pytest.mark.datafiles(os.path.join(DATA_DIR, 'third_party_source'))
+def test_custom_pip_source(plugin_fixture, entry_fixture, datafiles):
+ factory = SourceFactory(plugin_fixture['base'], [])
+ assert(isinstance(factory, SourceFactory))
+
+ entry_fixture(datafiles, 'buildstream.plugins', 'third_party_source:foop')
+
+ foo_type, _ = factory.lookup('third_party_source:foop')
+ assert(foo_type.__name__ == 'FooSource')
diff --git a/tests/plugins/third_party/third_party_element/__init__.py b/tests/plugins/third_party/third_party_element/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/plugins/third_party/third_party_element/__init__.py
diff --git a/tests/plugins/third_party/third_party_element/foop.py b/tests/plugins/third_party/third_party_element/foop.py
new file mode 100644
index 000000000..260de8b27
--- /dev/null
+++ b/tests/plugins/third_party/third_party_element/foop.py
@@ -0,0 +1,9 @@
+from buildstream import Element
+
+
+class FooElement(Element):
+ pass
+
+
+def setup():
+ return FooElement
diff --git a/tests/plugins/third_party/third_party_source/__init__.py b/tests/plugins/third_party/third_party_source/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/plugins/third_party/third_party_source/__init__.py
diff --git a/tests/plugins/third_party/third_party_source/foop.py b/tests/plugins/third_party/third_party_source/foop.py
new file mode 100644
index 000000000..de78a00ce
--- /dev/null
+++ b/tests/plugins/third_party/third_party_source/foop.py
@@ -0,0 +1,9 @@
+from buildstream import Source
+
+
+class FooSource(Source):
+ pass
+
+
+def setup():
+ return FooSource
diff --git a/tests/testutils/setuptools.py b/tests/testutils/setuptools.py
new file mode 100644
index 000000000..5bf4d2532
--- /dev/null
+++ b/tests/testutils/setuptools.py
@@ -0,0 +1,43 @@
+import os
+import pytest
+import pkg_resources
+
+
+# A mock setuptools dist object.
+class MockDist():
+ def __init__(self, datafiles, module_name):
+ self.datafiles = datafiles
+ self.module_name = module_name
+
+ def get_resource_filename(self, *args, **kwargs):
+ return os.path.join(self.datafiles.dirname,
+ self.datafiles.basename,
+ self.module_name)
+
+
+# A mock setuptools entry object.
+class MockEntry():
+ def __init__(self, datafiles, module_name):
+ self.dist = MockDist(datafiles, module_name)
+ self.module_name = module_name
+
+
+# Patch setuptools.get_entry_info
+#
+# Use result = entry_fixture(datafiles, entry_point, lookup_string) to
+# patch setuptools for external plugin loading.
+#
+@pytest.fixture()
+def entry_fixture(monkeypatch):
+ def patch(datafiles, entry_point, lookup_string):
+ dist, package = lookup_string.split(':')
+
+ def mock_entry(pdist, pentry_point, ppackage):
+ assert(pdist == dist)
+ assert(pentry_point == entry_point)
+ assert(ppackage == package)
+
+ return MockEntry(datafiles, package)
+ monkeypatch.setattr(pkg_resources, 'get_entry_info', mock_entry)
+
+ return patch