summaryrefslogtreecommitdiff
path: root/buildstream
diff options
context:
space:
mode:
authorPhil Dawson <phil.dawson@codethink.co.uk>2019-03-25 18:30:01 +0000
committerPhil Dawson <phil.dawson@codethink.co.uk>2019-04-12 16:06:19 +0100
commitec0f0757b4b06c0c3055c2dd1c5e6a1052ce05d5 (patch)
tree6e045d4601eb2e92c3901997a327551503a3814d /buildstream
parent92c9a04747a9773c0036578db9cb3fa9acb12247 (diff)
downloadbuildstream-ec0f0757b4b06c0c3055c2dd1c5e6a1052ce05d5.tar.gz
testutils: move repo.py into buildstream.plugintestutils
This needs to be exposed as part of the plugin author facing API so that plugin authors can define custom repo types which will can be passed to the set of tests which iterate over multiple source types. Part of the work towards #944
Diffstat (limited to 'buildstream')
-rw-r--r--buildstream/plugintestutils/__init__.py63
-rw-r--r--buildstream/plugintestutils/repo.py109
2 files changed, 172 insertions, 0 deletions
diff --git a/buildstream/plugintestutils/__init__.py b/buildstream/plugintestutils/__init__.py
index 9ec18df19..a052e4780 100644
--- a/buildstream/plugintestutils/__init__.py
+++ b/buildstream/plugintestutils/__init__.py
@@ -15,7 +15,9 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+from collections import OrderedDict
+from .repo import Repo
from .runcli import cli, cli_integration, cli_remote_execution
# To make use of these test utilities it is necessary to have pytest
@@ -28,3 +30,64 @@ except ImportError:
msg = "Could not import pytest:\n" \
"To use the {} module, you must have pytest installed.".format(module_name)
raise ImportError(msg)
+
+
+ALL_REPO_KINDS = OrderedDict()
+
+
+def create_repo(kind, directory, subdir='repo'):
+ """Convenience method for creating a Repo
+
+ Args:
+ kind (str): The kind of repo to create (a source plugin basename). This
+ must have previously been registered using
+ `register_repo_kind`
+ directory (str): The path where the repo will keep a cache
+
+ Returns:
+ (Repo): A new Repo object
+ """
+ try:
+ constructor = ALL_REPO_KINDS[kind]
+ except KeyError as e:
+ raise AssertionError("Unsupported repo kind {}".format(kind)) from e
+
+ return constructor(directory, subdir=subdir)
+
+
+def register_repo_kind(kind, cls):
+ """Register a new repo kind.
+
+ Registering a repo kind will allow the use of the `create_repo`
+ method for that kind and include that repo kind in ALL_REPO_KINDS
+
+ In addition, repo_kinds registred prior to
+ `sourcetests_collection_hook` being called will be automatically
+ used to test the basic behaviour of their associated source
+ plugins using the tests in `plugintestutils._sourcetests`.
+
+ Args:
+ kind (str): The kind of repo to create (a source plugin basename)
+ cls (cls) : A class derived from Repo.
+
+ """
+ ALL_REPO_KINDS[kind] = cls
+
+
+def sourcetests_collection_hook(session):
+ """ Used to hook the templated source plugin tests into a pyest test suite.
+
+ This should be called via the `pytest_sessionstart
+ hook <https://docs.pytest.org/en/latest/reference.html#collection-hooks>`_.
+ The tests in the _sourcetests package will be collected as part of
+ whichever test package this hook is called from.
+
+ Args:
+ session (pytest.Session): The current pytest session
+ """
+ SOURCE_TESTS_PATH = os.path.dirname(_sourcetests.__file__)
+ # Add the location of the source tests to the session's
+ # python_files config. Without this, pytest may filter out these
+ # tests during collection.
+ session.config.addinivalue_line("python_files", os.path.join(SOURCE_TESTS_PATH, "/**.py"))
+ session.config.args.append(SOURCE_TESTS_PATH)
diff --git a/buildstream/plugintestutils/repo.py b/buildstream/plugintestutils/repo.py
new file mode 100644
index 000000000..e3293ea75
--- /dev/null
+++ b/buildstream/plugintestutils/repo.py
@@ -0,0 +1,109 @@
+#
+# Copyright (C) 2016-2018 Codethink Limited
+# Copyright (C) 2019 Bloomberg Finance LP
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+Repo - Utility class for testing source plugins
+===============================================
+
+
+"""
+import os
+import shutil
+
+
+class Repo():
+ """Repo()
+
+ Abstract class providing scaffolding for generating data to be
+ used with various sources. Subclasses of Repo may be registered to
+ run through the suite of generic source plugin tests provided in
+ buildstream.plugintestutils.
+
+ Args:
+ directory (str): The base temp directory for the test
+ subdir (str): The subdir for the repo, in case there is more than one
+
+ """
+ def __init__(self, directory, subdir='repo'):
+
+ # The working directory for the repo object
+ #
+ self.directory = os.path.abspath(directory)
+
+ # The directory the actual repo will be stored in
+ self.repo = os.path.join(self.directory, subdir)
+
+ os.makedirs(self.repo, exist_ok=True)
+
+ def create(self, directory):
+ """Create a repository in self.directory and add the initial content
+
+ Args:
+ directory: A directory with content to commit
+
+ Returns:
+ (smth): A new ref corresponding to this commit, which can
+ be passed as the ref in the Repo.source_config() API.
+ """
+ raise NotImplementedError("create method has not been implemeted")
+
+ def source_config(self, ref=None):
+ """
+ Args:
+ ref (smth): An optional abstract ref object, usually a string.
+
+ Returns:
+ (dict): A configuration which can be serialized as a
+ source when generating an element file on the fly
+
+ """
+ raise NotImplementedError("source_config method has not been implemeted")
+
+ def copy_directory(self, src, dest):
+ """ Copies the content of src to the directory dest
+
+ Like shutil.copytree(), except dest is expected
+ to exist.
+
+ Args:
+ src (str): The source directory
+ dest (str): The destination directory
+ """
+ for filename in os.listdir(src):
+ src_path = os.path.join(src, filename)
+ dest_path = os.path.join(dest, filename)
+ if os.path.isdir(src_path):
+ shutil.copytree(src_path, dest_path)
+ else:
+ shutil.copy2(src_path, dest_path)
+
+ def copy(self, dest):
+ """Creates a copy of this repository in the specified destination.
+
+ Args:
+ dest (str): The destination directory
+
+ Returns:
+ (Repo): A Repo object for the new repository.
+ """
+ subdir = self.repo[len(self.directory):].lstrip(os.sep)
+ new_dir = os.path.join(dest, subdir)
+ os.makedirs(new_dir, exist_ok=True)
+ self.copy_directory(self.repo, new_dir)
+ repo_type = type(self)
+ new_repo = repo_type(dest, subdir)
+ return new_repo