summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Dawson <phil.dawson@codethink.co.uk>2019-03-26 09:32:14 +0000
committerPhil Dawson <phil.dawson@codethink.co.uk>2019-04-12 16:06:19 +0100
commite00d1b6cd8f0131a6989693085049d0adc1ec596 (patch)
tree7a24090bc719d4bd14ded9ed908b782a25cf6a79
parentec0f0757b4b06c0c3055c2dd1c5e6a1052ce05d5 (diff)
downloadbuildstream-e00d1b6cd8f0131a6989693085049d0adc1ec596.tar.gz
plugintestutils: Add copy of testutils.junction.py
Copy tests.testutils.junction into plugintestutils._utils. This is needed for use by the templated source tests.
-rw-r--r--buildstream/plugintestutils/_utils/__init__.py1
-rw-r--r--buildstream/plugintestutils/_utils/junction.py83
-rw-r--r--tests/sources/generic/fetch.py2
-rw-r--r--tests/sources/generic/mirror.py2
-rw-r--r--tests/sources/generic/track.py2
-rw-r--r--tests/sources/generic/track_cross_junction.py2
6 files changed, 88 insertions, 4 deletions
diff --git a/buildstream/plugintestutils/_utils/__init__.py b/buildstream/plugintestutils/_utils/__init__.py
new file mode 100644
index 000000000..5938e6a5e
--- /dev/null
+++ b/buildstream/plugintestutils/_utils/__init__.py
@@ -0,0 +1 @@
+from .junction import generate_junction
diff --git a/buildstream/plugintestutils/_utils/junction.py b/buildstream/plugintestutils/_utils/junction.py
new file mode 100644
index 000000000..530a191ac
--- /dev/null
+++ b/buildstream/plugintestutils/_utils/junction.py
@@ -0,0 +1,83 @@
+import subprocess
+import pytest
+
+from .._utils.site import HAVE_GIT, GIT, GIT_ENV
+from buildstream.plugintestutils import Repo
+from buildstream import _yaml
+
+
+# generate_junction()
+#
+# Generates a junction element with a git repository
+#
+# Args:
+# tmpdir: The tmpdir fixture, for storing the generated git repo
+# subproject_path: The path for the subproject, to add to the git repo
+# junction_path: The location to store the generated junction element
+# store_ref: Whether to store the ref in the junction.bst file
+#
+# Returns:
+# (str): The ref
+#
+def generate_junction(tmpdir, subproject_path, junction_path, *, store_ref=True):
+ # Create a repo to hold the subproject and generate
+ # a junction element for it
+ #
+ repo = _SimpleGit(str(tmpdir))
+ source_ref = ref = repo.create(subproject_path)
+ if not store_ref:
+ source_ref = None
+
+ element = {
+ 'kind': 'junction',
+ 'sources': [
+ repo.source_config(ref=source_ref)
+ ]
+ }
+ _yaml.dump(element, junction_path)
+
+ return ref
+
+
+# A barebones Git Repo class to use for generating junctions
+class _SimpleGit(Repo):
+ def __init__(self, directory, subdir='repo'):
+ if not HAVE_GIT:
+ pytest.skip('git is not available')
+ super().__init__(directory, subdir)
+
+ def create(self, directory):
+ self.copy_directory(directory, self.repo)
+ self._run_git('init', '.')
+ self._run_git('add', '.')
+ self._run_git('commit', '-m', 'Initial commit')
+ return self.latest_commit()
+
+ def latest_commit(self):
+ return self._run_git(
+ 'rev-parse', 'HEAD',
+ stdout=subprocess.PIPE,
+ universal_newlines=True,
+ ).stdout.strip()
+
+ def source_config(self, ref=None, checkout_submodules=None):
+ config = {
+ 'kind': 'git',
+ 'url': 'file://' + self.repo,
+ 'track': 'master'
+ }
+ if ref is not None:
+ config['ref'] = ref
+ if checkout_submodules is not None:
+ config['checkout-submodules'] = checkout_submodules
+
+ return config
+
+ def _run_git(self, *args, **kwargs):
+ argv = [GIT]
+ argv.extend(args)
+ if 'env' not in kwargs:
+ kwargs['env'] = dict(GIT_ENV, PWD=self.repo)
+ kwargs.setdefault('cwd', self.repo)
+ kwargs.setdefault('check', True)
+ return subprocess.run(argv, **kwargs)
diff --git a/tests/sources/generic/fetch.py b/tests/sources/generic/fetch.py
index f8c6b5557..52c853398 100644
--- a/tests/sources/generic/fetch.py
+++ b/tests/sources/generic/fetch.py
@@ -22,7 +22,7 @@
import os
import pytest
-from tests.testutils import generate_junction
+from .._utils import generate_junction
from buildstream.plugintestutils import create_repo, ALL_REPO_KINDS
from tests.frontend import configure_project
diff --git a/tests/sources/generic/mirror.py b/tests/sources/generic/mirror.py
index 9cbb19fe4..5315d184f 100644
--- a/tests/sources/generic/mirror.py
+++ b/tests/sources/generic/mirror.py
@@ -22,7 +22,7 @@
import os
import pytest
-from tests.testutils import generate_junction
+from .._utils import generate_junction
from buildstream.plugintestutils import create_repo, ALL_REPO_KINDS
from buildstream.plugintestutils import cli # pylint: disable=unused-import
diff --git a/tests/sources/generic/track.py b/tests/sources/generic/track.py
index cbc826eae..334a8f679 100644
--- a/tests/sources/generic/track.py
+++ b/tests/sources/generic/track.py
@@ -22,8 +22,8 @@
import os
import pytest
-from tests.testutils import generate_junction
from tests.frontend import configure_project
+from .._utils import generate_junction
from buildstream.plugintestutils import create_repo, ALL_REPO_KINDS
from buildstream.plugintestutils import cli # pylint: disable=unused-import
diff --git a/tests/sources/generic/track_cross_junction.py b/tests/sources/generic/track_cross_junction.py
index 7cf049634..68db91756 100644
--- a/tests/sources/generic/track_cross_junction.py
+++ b/tests/sources/generic/track_cross_junction.py
@@ -22,7 +22,7 @@
import os
import pytest
-from tests.testutils import generate_junction
+from .._utils import generate_junction
from buildstream.plugintestutils import create_repo, ALL_REPO_KINDS
from buildstream.plugintestutils import cli # pylint: disable=unused-import