Skip to content

Commit

Permalink
Merge pull request ESCOMP#15 from jedwards4b/add_tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
jedwards4b authored Feb 10, 2024
2 parents ac3e6dd + e8b6d3c commit d103bc6
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 4 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
@@ -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

11 changes: 9 additions & 2 deletions git_fleximod/gitinterface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from . import utils

class GitInterface:
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
51 changes: 51 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/test_checkout.py
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions tests/test_sparse_checkout.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d103bc6

Please sign in to comment.