This is an automated email from the ASF dual-hosted git repository. tvb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/buildstream-plugins.git
commit 8bd6f6bcec763b795260eac09e6d8ddaadcbaf4e Author: Tristan van Berkom <[email protected]> AuthorDate: Sun Mar 20 16:53:50 2022 +0900 tests: Adding initial tests Adding the Repo scaffolding and hooks into the private BuildStream provided general test cases for sources. --- tests/__init__.py | 0 tests/conftest.py | 32 +++++++++++ tests/testutils/__init__.py | 0 tests/testutils/repo/__init__.py | 2 + tests/testutils/repo/bzrrepo.py | 54 +++++++++++++++++++ tests/testutils/repo/gitrepo.py | 114 +++++++++++++++++++++++++++++++++++++++ tests/testutils/site.py | 45 ++++++++++++++++ 7 files changed, 247 insertions(+) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..833c9bb --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,32 @@ +import pytest + +from buildstream._testing import sourcetests_collection_hook +from buildstream._testing import register_repo_kind + +from .testutils.repo import Bzr, Git + + +################################################# +# Implement pytest option # +################################################# +def pytest_addoption(parser): + parser.addoption( + "--integration", action="store_true", default=False, help="Run integration tests", + ) + + +def pytest_runtest_setup(item): + # Without --integration: skip tests not marked with 'integration' + if not item.config.getvalue("integration"): + if item.get_closest_marker("integration"): + pytest.skip("skipping integration test") + + +register_repo_kind("bzr", Bzr, "buildstream_plugins") +register_repo_kind("git", Git, "buildstream_plugins") + + +# This hook enables pytest to collect the templated source tests from +# buildstream._testing +def pytest_sessionstart(session): + sourcetests_collection_hook(session) diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/testutils/repo/__init__.py b/tests/testutils/repo/__init__.py new file mode 100644 index 0000000..8046c89 --- /dev/null +++ b/tests/testutils/repo/__init__.py @@ -0,0 +1,2 @@ +from .bzrrepo import Bzr +from .gitrepo import Git diff --git a/tests/testutils/repo/bzrrepo.py b/tests/testutils/repo/bzrrepo.py new file mode 100644 index 0000000..324244c --- /dev/null +++ b/tests/testutils/repo/bzrrepo.py @@ -0,0 +1,54 @@ +import os +import subprocess +import pytest + +from buildstream._testing import Repo + +from ..site import BZR, BZR_ENV, HAVE_BZR + + +class Bzr(Repo): + def __init__(self, directory, subdir="repo"): + if not HAVE_BZR: + pytest.skip("bzr is not available") + super().__init__(directory, subdir) + self.bzr = BZR + + self.env = os.environ.copy() + self.env.update(BZR_ENV) + + def create(self, directory): + # Work around race condition in bzr's creation of ~/.bazaar in + # ensure_config_dir_exists() when running tests in parallel. + bazaar_config_dir = os.path.expanduser("~/.bazaar") + os.makedirs(bazaar_config_dir, exist_ok=True) + + branch_dir = os.path.join(self.repo, "trunk") + + subprocess.call([self.bzr, "init-repo", self.repo], env=self.env) + subprocess.call([self.bzr, "init", branch_dir], env=self.env) + self.copy_directory(directory, branch_dir) + subprocess.call([self.bzr, "add", "."], env=self.env, cwd=branch_dir) + subprocess.call( + [self.bzr, "commit", '--message="Initial commit"'], env=self.env, cwd=branch_dir, + ) + + return self.latest_commit() + + def source_config(self, ref=None): + config = { + "kind": "bzr", + "url": "file://" + self.repo, + "track": "trunk", + } + if ref is not None: + config["ref"] = ref + + return config + + def latest_commit(self): + return subprocess.check_output( + [self.bzr, "version-info", "--custom", "--template={revno}", os.path.join(self.repo, "trunk"),], + env=self.env, + universal_newlines=True, + ).strip() diff --git a/tests/testutils/repo/gitrepo.py b/tests/testutils/repo/gitrepo.py new file mode 100644 index 0000000..9cb3c9a --- /dev/null +++ b/tests/testutils/repo/gitrepo.py @@ -0,0 +1,114 @@ +import os +import shutil +import subprocess + +import pytest + +from buildstream._testing import Repo + +from ..site import GIT, GIT_ENV, HAVE_GIT + + +class Git(Repo): + def __init__(self, directory, subdir="repo"): + if not HAVE_GIT: + pytest.skip("git is not available") + + self.submodules = {} + + super().__init__(directory, subdir) + + self.env = os.environ.copy() + self.env.update(GIT_ENV) + + def _run_git(self, *args, **kwargs): + argv = [GIT] + argv.extend(args) + if "env" not in kwargs: + kwargs["env"] = dict(self.env, PWD=self.repo) + kwargs.setdefault("cwd", self.repo) + kwargs.setdefault("check", True) + return subprocess.run(argv, **kwargs) # pylint: disable=subprocess-run-check + + 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 add_tag(self, tag): + self._run_git("tag", tag) + + def add_annotated_tag(self, tag, message): + self._run_git("tag", "-a", tag, "-m", message) + + def add_commit(self): + self._run_git("commit", "--allow-empty", "-m", "Additional commit") + return self.latest_commit() + + def add_file(self, filename): + shutil.copy(filename, self.repo) + self._run_git("add", os.path.basename(filename)) + self._run_git("commit", "-m", "Added {}".format(os.path.basename(filename))) + return self.latest_commit() + + def modify_file(self, new_file, path): + shutil.copy(new_file, os.path.join(self.repo, path)) + self._run_git("commit", path, "-m", "Modified {}".format(os.path.basename(path))) + return self.latest_commit() + + def add_submodule(self, subdir, url=None, checkout=None): + submodule = {} + if checkout is not None: + submodule["checkout"] = checkout + if url is not None: + submodule["url"] = url + self.submodules[subdir] = submodule + self._run_git("submodule", "add", url, subdir) + self._run_git("commit", "-m", "Added the submodule") + return self.latest_commit() + + # This can also be used to a file or a submodule + def remove_path(self, path): + self._run_git("rm", path) + self._run_git("commit", "-m", "Removing {}".format(path)) + return self.latest_commit() + + def source_config(self, ref=None): + return self.source_config_extra(ref) + + def source_config_extra(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 + + if self.submodules: + config["submodules"] = dict(self.submodules) + + return config + + def latest_commit(self): + return self._run_git("rev-parse", "HEAD", stdout=subprocess.PIPE, universal_newlines=True,).stdout.strip() + + def branch(self, branch_name): + self._run_git("checkout", "-b", branch_name) + + def delete_tag(self, tag_name): + self._run_git("tag", "-d", tag_name) + + def checkout(self, commit): + self._run_git("checkout", commit) + + def merge(self, commit): + self._run_git("merge", "-m", "Merge", commit) + return self.latest_commit() + + def rev_parse(self, rev): + return self._run_git("rev-parse", rev, stdout=subprocess.PIPE, universal_newlines=True,).stdout.strip() diff --git a/tests/testutils/site.py b/tests/testutils/site.py new file mode 100644 index 0000000..c60df1e --- /dev/null +++ b/tests/testutils/site.py @@ -0,0 +1,45 @@ +import subprocess + +from typing import Optional + +from buildstream import utils, ProgramNotFoundError + +GIT: Optional[str] +BZR: Optional[str] + +try: + GIT = utils.get_host_tool("git") + HAVE_GIT = True + + out = str(subprocess.check_output(["git", "--version"]), "utf-8") + # e.g. on Git for Windows we get "git version 2.21.0.windows.1". + # e.g. on Mac via Homebrew we get "git version 2.19.0". + version = tuple(int(x) for x in out.split(" ")[2].split(".")[:3]) + HAVE_OLD_GIT = version < (1, 8, 5) + + GIT_ENV = { + "GIT_AUTHOR_DATE": "1320966000 +0200", + "GIT_AUTHOR_NAME": "tomjon", + "GIT_AUTHOR_EMAIL": "[email protected]", + "GIT_COMMITTER_DATE": "1320966000 +0200", + "GIT_COMMITTER_NAME": "tomjon", + "GIT_COMMITTER_EMAIL": "[email protected]", + } +except ProgramNotFoundError: + GIT = None + HAVE_GIT = False + HAVE_OLD_GIT = False + GIT_ENV = {} + +try: + BZR = utils.get_host_tool("bzr") + HAVE_BZR = True + # Breezy 3.0 supports `BRZ_EMAIL` but not `BZR_EMAIL` + BZR_ENV = { + "BZR_EMAIL": "Testy McTesterson <[email protected]>", + "BRZ_EMAIL": "Testy McTesterson <[email protected]>", + } +except ProgramNotFoundError: + BZR = None + HAVE_BZR = False + BZR_ENV = {}
