diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml new file mode 100644 index 0000000000..4809a2787f --- /dev/null +++ b/.github/workflows/pytest.yaml @@ -0,0 +1,74 @@ +# Run this job on pushes to `main`, and for pull requests. If you don't specify +# `branches: [main], then this actions runs _twice_ on pull requests, which is +# annoying. + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # If you wanted to use multiple Python versions, you'd have specify a matrix in the job and + # reference the matrixe python version here. + - uses: actions/setup-python@v5 + with: + python-version: '3.9' + + # Cache the installation of Poetry itself, e.g. the next step. This prevents the workflow + # from installing Poetry every time, which can be slow. Note the use of the Poetry version + # number in the cache key, and the "-0" suffix: this allows you to invalidate the cache + # manually if/when you want to upgrade Poetry, or if something goes wrong. This could be + # mildly cleaner by using an environment variable, but I don't really care. + - name: cache poetry install + uses: actions/cache@v4 + with: + path: ~/.local + key: poetry-1.7.1 + + # Install Poetry. You could do this manually, or there are several actions that do this. + # `snok/install-poetry` seems to be minimal yet complete, and really just calls out to + # Poetry's default install script, which feels correct. I pin the Poetry version here + # because Poetry does occasionally change APIs between versions and I don't want my + # actions to break if it does. + # + # The key configuration value here is `virtualenvs-in-project: true`: this creates the + # venv as a `.venv` in your testing directory, which allows the next step to easily + # cache it. + - uses: snok/install-poetry@v1 + with: + version: 1.7.1 + virtualenvs-create: true + virtualenvs-in-project: true + + # Cache your dependencies (i.e. all the stuff in your `pyproject.toml`). Note the cache + # key: if you're using multiple Python versions, or multiple OSes, you'd need to include + # them in the cache key. I'm not, so it can be simple and just depend on the poetry.lock. + - name: cache deps + id: cache-deps + uses: actions/cache@v4 + with: + path: .venv + key: pydeps-${{ hashFiles('**/poetry.lock') }} + + # Install dependencies. `--no-root` means "install all dependencies but not the project + # itself", which is what you want to avoid caching _your_ code. The `if` statement + # ensures this only runs on a cache miss. + - run: poetry install --no-interaction --no-root + if: steps.cache-deps.outputs.cache-hit != 'true' + + # Now install _your_ project. This isn't necessary for many types of projects -- particularly + # things like Django apps don't need this. But it's a good idea since it fully-exercises the + # pyproject.toml and makes that if you add things like console-scripts at some point that + # they'll be installed and working. + - run: poetry install --no-interaction + + # And finally run tests. I'm using pytest and all my pytest config is in my `pyproject.toml` + # so this line is super-simple. But it could be as complex as you need. + - run: poetry run pytest + diff --git a/git_fleximod/gitinterface.py b/git_fleximod/gitinterface.py index eff155d54d..4d4c4f4b9e 100644 --- a/git_fleximod/gitinterface.py +++ b/git_fleximod/gitinterface.py @@ -1,4 +1,5 @@ import os +import sys from . import utils class GitInterface: @@ -26,7 +27,10 @@ def __init__(self, repo_path, logger): def _git_command(self, operation, *args): self.logger.info(operation) if self._use_module and operation != "submodule": - return getattr(self.repo.git, operation)(*args) + try: + return getattr(self.repo.git, operation)(*args) + except Exception as e: + sys.exit(e) else: return ["git", "-C", self.repo_path, operation] + list(args) @@ -42,7 +46,10 @@ def git_operation(self, operation, *args, **kwargs): command = self._git_command(operation, *args) self.logger.info(command) if isinstance(command, list): - return utils.execute_subprocess(command, output_to_caller=True) + try: + return utils.execute_subprocess(command, output_to_caller=True) + except Exception as e: + sys.exit(e) else: return command diff --git a/poetry.lock b/poetry.lock index 6c31fac48a..b59ed3942c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -404,6 +404,17 @@ files = [ dev = ["pre-commit", "tox"] testing = ["pytest", "pytest-benchmark"] +[[package]] +name = "pyfakefs" +version = "5.3.5" +description = "pyfakefs implements a fake file system that mocks the Python file system modules." +optional = false +python-versions = ">=3.7" +files = [ + {file = "pyfakefs-5.3.5-py3-none-any.whl", hash = "sha256:751015c1de94e1390128c82b48cdedc3f088bbdbe4bc713c79d02a27f0f61e69"}, + {file = "pyfakefs-5.3.5.tar.gz", hash = "sha256:7cdc500b35a214cb7a614e1940543acc6650e69a94ac76e30f33c9373bd9cf90"}, +] + [[package]] name = "pygments" version = "2.17.2" @@ -679,4 +690,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "f20e29bada880bbb0aad20d2a7ac282d09c3e89d4aac06072cb82a9157f4023a" +content-hash = "25ee2ae1d74abedde3a6637a60d4a3095ea5cf9731960875741bbc2ba84a475d" diff --git a/pyproject.toml b/pyproject.toml index f5828a58e7..6d0fa42157 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ packages = [ ] [tool.poetry.scripts] -git-fleximod = "git_fleximod.git-fleximod:main" +git-fleximod = "git_fleximod.git_fleximod:main" fsspec = "fsspec.fuse:main" [tool.poetry.dependencies] @@ -24,6 +24,7 @@ sphinx = "^5.0.0" fsspec = "^2023.12.2" wheel = "^0.42.0" pytest = "^8.0.0" +pyfakefs = "^5.3.5" [tool.poetry.urls] "Bug Tracker" = "https://github.com/jedwards4b/git-fleximod/issues" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000000..1cb059dac6 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,51 @@ +import pytest +from git_fleximod.gitinterface import GitInterface +import os +import subprocess +import logging +from pathlib import Path + +@pytest.fixture(scope='session') +def logger(): + logging.basicConfig( + level=logging.INFO, format="%(name)s - %(levelname)s - %(message)s", handlers=[logging.StreamHandler()] + ) + logger = logging.getLogger(__name__) + return logger + +@pytest.fixture +def test_repo_base(tmp_path, logger): + test_dir = tmp_path / "test_repo" + test_dir.mkdir() + str_path = str(test_dir) + gitp = GitInterface(str_path, logger) + #subprocess.run(["git", "init"], cwd=test_dir) + assert test_dir.joinpath(".git").is_dir() + return test_dir + +@pytest.fixture(params=["modules/test"]) +def test_repo(request, test_repo_base, logger): + subrepo_path = request.param + gitp = GitInterface(str(test_repo_base), logger) + gitp.git_operation("submodule", "add", "--depth","1","--name","test_submodule", "https://github.com/ESMCI/mpi-serial.git", subrepo_path) + # subprocess.run( + assert test_repo_base.joinpath(".gitmodules").is_file() + return test_repo_base + +@pytest.fixture +def git_fleximod(test_repo): + def _run_fleximod(args, input=None): + cmd = ["git", "fleximod"] + args.split() + result = subprocess.run(cmd, cwd=test_repo, input=input, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + text=True) + return result + return _run_fleximod + + +@pytest.fixture +def deinit_submodule(test_repo, logger): + def _deinit(submodule_path): + gitp = GitInterface(str(test_repo), logger) + gitp.git_operation( "submodule", "deinit", "-f", submodule_path) + yield _deinit diff --git a/tests/test_import.py b/tests/test_a_import.py similarity index 100% rename from tests/test_import.py rename to tests/test_a_import.py diff --git a/tests/test_checkout.py b/tests/test_checkout.py new file mode 100644 index 0000000000..791587628b --- /dev/null +++ b/tests/test_checkout.py @@ -0,0 +1,32 @@ +import pytest +from pathlib import Path + +def test_basic_checkout(git_fleximod, test_repo): + # Prepare a simple .gitmodules + + gitmodules_content = """ + [submodule "test_submodule"] + path = modules/test + url = https://github.com/ESMCI/mpi-serial.git + fxtag = MPIserial_2.4.0 + fxurl = https://github.com/ESMCI/mpi-serial.git + fxrequired = T:T + """ + (test_repo / ".gitmodules").write_text(gitmodules_content) + + # Run the command + result = git_fleximod("checkout") + + # Assertions + assert result.returncode == 0 + assert Path(test_repo / "modules/test").exists() # Did the submodule directory get created? + + status = git_fleximod("status") + + assert "test_submodule d82ce7c is out of sync with .gitmodules MPIserial_2.4.0" in status.stdout + + result = git_fleximod("update") + assert result.returncode == 0 + + status = git_fleximod("status") + assert "test_submodule at tag MPIserial_2.4.0" in status.stdout diff --git a/tests/test_sparse_checkout.py b/tests/test_sparse_checkout.py new file mode 100644 index 0000000000..0633802cb0 --- /dev/null +++ b/tests/test_sparse_checkout.py @@ -0,0 +1,38 @@ +import pytest +from shutil import rmtree +from pathlib import Path +from git_fleximod.gitinterface import GitInterface + +def test_sparse_checkout(git_fleximod, test_repo_base): + # Prepare a simple .gitmodules + gitmodules_content = (test_repo_base / ".gitmodules").read_text() + """ + [submodule "test_sparse_submodule"] + path = modules/sparse_test + url = https://github.com/ESMCI/mpi-serial.git + fxtag = MPIserial_2.5.0 + fxurl = https://github.com/ESMCI/mpi-serial.git + fxsparse = ../.sparse_file_list + """ + (test_repo_base / ".gitmodules").write_text(gitmodules_content) + + # Add the sparse checkout file + sparse_content = """m4 +""" + (test_repo_base / "modules" / ".sparse_file_list").write_text(sparse_content) + + result = git_fleximod("checkout") + + # Assertions + assert result.returncode == 0 + assert Path(test_repo_base / "modules/sparse_test").exists() # Did the submodule directory get created? + assert Path(test_repo_base / "modules/sparse_test/m4").exists() # Did the submodule sparse directory get created? + assert not Path(test_repo_base / "modules/sparse_test/README").exists() # Did only the submodule sparse directory get created? + status = git_fleximod("status test_sparse_submodule") + + assert "test_sparse_submodule at tag MPIserial_2.5.0" in status.stdout + + result = git_fleximod("update") + assert result.returncode == 0 + + status = git_fleximod("status test_sparse_submodule") + assert "test_sparse_submodule at tag MPIserial_2.5.0" in status.stdout