Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards4b committed Feb 9, 2024
1 parent ac3e6dd commit 96ac371
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
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
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 96ac371

Please sign in to comment.