41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
import os
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from chunk_driver import cycle_start
|
|
from failure import Failure
|
|
from git_cli.git_repo import git
|
|
|
|
|
|
class SetupVersion(unittest.TestCase):
|
|
def test_names_the_release_and_the_commit_or_why_git_cannot_tell(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
isolated = {"GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CEILING_DIRECTORIES": root}
|
|
with unittest.mock.patch.dict(os.environ, isolated):
|
|
gone = cycle_start.setup_version(pathlib.Path(root), git)
|
|
self.assertRegex(gone, r"^unknown: [^\n]*not a git repository")
|
|
for step in (["init", "-q"], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "one"]):
|
|
self.assertIsInstance(git(*step, cwd=root), str)
|
|
head = git("rev-parse", "HEAD", cwd=root).strip()
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"{head[:7]} ({head})")
|
|
git("tag", "v0.1.0", cwd=root)
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0 ({head})")
|
|
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "two", cwd=root)
|
|
head = git("rev-parse", "HEAD", cwd=root).strip()
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path(root), git), f"v0.1.0-1-g{head[:7]} ({head})")
|
|
|
|
def test_names_why_git_cannot_name_the_commit_on_one_line(self):
|
|
def fake_git(*args, cwd=None):
|
|
return Failure("fatal: bad HEAD\nhint: repair it") if args[0] == "rev-parse" else "v0.1.0\n"
|
|
|
|
self.assertEqual(cycle_start.setup_version(pathlib.Path("/r"), fake_git), "unknown: fatal: bad HEAD hint: repair it")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|