Skip to content

Commit

Permalink
test: integration test for git
Browse files Browse the repository at this point in the history
  • Loading branch information
fblanchetNaN committed Jul 2, 2022
1 parent 7ad3d4a commit 5c3bab9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
55 changes: 45 additions & 10 deletions incipyt/tools/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __init__(self):

signals.vcs_ignore.connect(self._slot)

self._set_git_credential_email = False
self._set_git_credential_name = False

def add_to_structure(self):
"""Add git configuration to `project.structure`.
Expand Down Expand Up @@ -49,23 +52,55 @@ def pre(self, workon):
"""
commands.run(["git", "init", os.fspath(workon)])

project.environ["AUTHOR_EMAIL"] = project.EnvValue(
commands.run(["git", "-C", os.fspath(workon), "config", "user.email"])
.stdout.decode()
.strip(),
update=True,
git_credential_email = commands.run(
["git", "-C", os.fspath(workon), "config", "user.email"], check=False
)
project.environ["AUTHOR_NAME"] = project.EnvValue(
commands.run(["git", "-C", os.fspath(workon), "config", "user.name"])
.stdout.decode()
.strip(),
update=True,
if git_credential_email.returncode == 0:
project.environ["AUTHOR_EMAIL"] = project.EnvValue(
git_credential_email.stdout.decode().strip(),
update=True,
)
else:
self._set_git_credential_email = True

git_credential_name = commands.run(
["git", "-C", os.fspath(workon), "config", "user.name"], check=False
)
if git_credential_name.returncode == 0:
project.environ["AUTHOR_NAME"] = project.EnvValue(
git_credential_name.stdout.decode().strip(),
update=True,
)
else:
self._set_git_credential_name = True

def post(self, workon):
"""Run `git add --all`.
:param workon: Work-on folder.
:type workon: :class:`pathlib.Path`
"""
if self._set_git_credential_email:
commands.run(
[
"git",
"-C",
os.fspath(workon),
"config",
"user.email",
project.environ["AUTHOR_EMAIL"],
]
)
if self._set_git_credential_name:
commands.run(
[
"git",
"-C",
os.fspath(workon),
"config",
"user.name",
project.environ["AUTHOR_NAME"],
]
)

commands.run(["git", "-C", os.fspath(workon), "add", "--all"])
Binary file added tests/test_tools/git.tar.gz
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/test_tools/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import filecmp
import os
import pathlib
import shutil

from tests.test_tools import testing

from incipyt import __main__


def make_archive():
runner = testing.IncipytRunner()
root_dir = pathlib.Path(__file__).parent
with runner.isolated_filesystem() as td:
runner.invoke(__main__.main, ["--vcs=git", "--check-build", "my_project"])
os.chdir(root_dir)
os.remove("git.tar.gz")
shutil.make_archive("git", "gztar", root_dir=td, base_dir="my_project")


def test_integration(tmp_path):
runner = testing.IncipytRunner(input_mapping={r"Repository": ""})
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(
__main__.main, ["--vcs=git", "--check-build", "my_project"]
)
shutil.unpack_archive(pathlib.Path(__file__).parent / "git.tar.gz", "archive")
diff = testing.diff_files(
filecmp.dircmp("my_project", pathlib.Path("archive/my_project"))
)

assert diff == set()
assert not result.exception
assert result.exit_code == 0

0 comments on commit 5c3bab9

Please sign in to comment.