From 559cdfc1bff2be0320b799f6d204dddad3762bfe Mon Sep 17 00:00:00 2001 From: Anders Lindh Olsson <anders.lindholsson@ess.eu> Date: Fri, 20 Dec 2024 17:10:48 +0100 Subject: [PATCH] Add tests for make init --- .gitlab-ci.yml | 2 + .../build_system/test_convenience_features.py | 6 -- tests/build_system/test_make_clean.py | 0 tests/build_system/test_make_init.py | 85 +++++++++++++++++++ tests/conftest.py | 28 ++++++ 5 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 tests/build_system/test_make_clean.py create mode 100644 tests/build_system/test_make_init.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2760b50c..c25b2900 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -63,6 +63,8 @@ test require: stage: test before_script: - source $(pwd)/epics/base-*/require/*/bin/activate + # We need to allow file:// protocol for all of the tests to work + - git config --global procotol.file.allow always script: - make test needs: diff --git a/tests/build_system/test_convenience_features.py b/tests/build_system/test_convenience_features.py index 290aca1c..ebac137a 100644 --- a/tests/build_system/test_convenience_features.py +++ b/tests/build_system/test_convenience_features.py @@ -17,9 +17,3 @@ def test_warning_msg_if_building_from_sitemods(wrapper): assert ( "It is ill-advised to build a module from the module install location." in errs ) - - -def test_info_msg_if_local_mode(wrapper): - rc, outs, _ = wrapper.run_make("init") - assert rc == 0 - assert "You are in the local source mode" in outs diff --git a/tests/build_system/test_make_clean.py b/tests/build_system/test_make_clean.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/build_system/test_make_init.py b/tests/build_system/test_make_init.py new file mode 100644 index 00000000..6dd83ad6 --- /dev/null +++ b/tests/build_system/test_make_init.py @@ -0,0 +1,85 @@ +import shutil +import subprocess + +import pytest + + +def set_up_git_submodule(main, sub) -> None: + # remove local dir with dbd-file and add fake repo as submodule + shutil.rmtree(main.module_dir) + subprocess.run( + ["git", "init"], + cwd=main.wrapper_dir, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + # we allow file:// as protocol in order to not get a fatal error like + # fatal: transport 'file' not allowed + # see https://lore.kernel.org/git/000001d91c8b$6a26cd60$3e746820$@nexbridge.com/T/ + subprocess.run( + [ + "git", + "-c", + "protocol.file.allow=always", + "submodule", + "add", + f"file://{sub.module_dir.as_posix()}", + main.name, + ], + cwd=main.wrapper_dir, + ) + main.write_var_to_makefile( + main.config_module, + "EPICS_MODULE_TAG", + "fake-tag", + ) + + +def test_error_if_epics_module_tag_set_but_no_submodule(wrapper): + wrapper.write_var_to_makefile( + wrapper.config_module, + "EPICS_MODULE_TAG", + "whatever", + ) + rc, _, stderr = wrapper.run_make("init") + assert rc != 0 + assert stderr + + +@pytest.mark.skipif( + subprocess.run( + ["git", "config", "get", "protocol.file.allow"], stdout=subprocess.PIPE + ) + .stdout.decode() + .strip() + != "always", + reason="file protocol must be allowed", +) +def test_init_inits_without_error_if_defined(wrapper, module): + set_up_git_submodule(wrapper, module) + + rc, *_ = wrapper.run_make("init") + assert rc == 0 + + +def test_init_fetches_submodule_files(wrapper, module): + set_up_git_submodule(wrapper, module) + + _ = wrapper.run_make("init") + assert len(list((wrapper.wrapper_dir / wrapper.name).iterdir())) > 0 + + +def test_init_checks_out_version_in_epics_module_tag(wrapper, module): + set_up_git_submodule(wrapper, module) + + _ = wrapper.run_make("init") + res = subprocess.run( + ["git", "describe", "--tags"], cwd=wrapper.module_dir, stdout=subprocess.PIPE + ) + assert res.stdout.decode().strip() == "fake-tag" + + +def test_info_msg_if_local_mode(wrapper): + rc, outs, _ = wrapper.run_make("init") + assert rc == 0 + assert "You are in the local source mode" in outs diff --git a/tests/conftest.py b/tests/conftest.py index 4d62bf0f..9fdf3851 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,6 +22,34 @@ def wrapper(wrapper_factory): return wrapper_factory.create() +@pytest.fixture() +def module(tmp_path): + return Module(tmp_path) + + +class Module: + def __init__(self, tmp_test_dir: Path): + self.name = "module-" + "".join(choice(ascii_lowercase) for _ in range(16)) + self.module_dir = tmp_test_dir / self.name + self.module_dir.mkdir() + + # set up fake repo + fake_file = self.module_dir / "random-file-to-commit" + fake_file.write_text("whatever") + for cmd in [ + "git init", + "git add .", + "git commit -m message -q", + "git tag fake-tag", + ]: + subprocess.run( + cmd.split(), + cwd=self.module_dir, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + class Wrapper: def __init__( self, -- GitLab