From 04bf403861805f9e5caaed0adbc908ab1d3871b9 Mon Sep 17 00:00:00 2001 From: Leroy Date: Tue, 17 Oct 2023 13:54:09 +0800 Subject: [PATCH] test(git-archive-file): add unit test (#1084) --- tests/conftest.py | 22 +++++++++--- tests/helper.py | 54 +++++++++++++++++++++++------ tests/test_archive_file.py | 70 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 tests/test_archive_file.py diff --git a/tests/conftest.py b/tests/conftest.py index 40033e8..384b987 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,15 +4,29 @@ import pytest from helper import TempRepository -@pytest.fixture(scope="module") -def temp_repo(): - repo = TempRepository() - git = repo.get_repo_git() +def create_repo(dirname = None): + repo = TempRepository(dirname) tmp_file_a = repo.create_tmp_file() tmp_file_b = repo.create_tmp_file() repo.switch_cwd_under_repo() + return repo + +def init_repo_git_status(repo): + git = repo.get_repo_git() git.add(".") git.config("--local", "user.name", "test") git.config("--local", "user.email", "test@git-extras.com") git.commit("-m", "chore: initial commit") + +@pytest.fixture(scope="module") +def temp_repo(): + repo = create_repo() + init_repo_git_status(repo) + return repo + +@pytest.fixture(scope="module") +def named_temp_repo(request): + dirname = request.param + repo = create_repo(dirname) + init_repo_git_status(repo) return repo diff --git a/tests/helper.py b/tests/helper.py index 0852c67..aec6cbc 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -1,21 +1,23 @@ -import os -import subprocess -import shutil -import tempfile -import git +import os, subprocess, stat, shutil, tempfile, git + +CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) +GIT_EXTRAS_BIN = os.path.join(CURRENT_DIR, "..", "bin") +GIT_EXTRAS_HELPER = os.path.join(CURRENT_DIR, "..", "helper") def invoke_git_extras_command(name, *params): - current_dir = os.path.dirname(os.path.abspath(__file__)) - git_extras_bin = os.path.join(current_dir, "..", "bin") - script = [os.path.join(git_extras_bin, name), *params] + script = [os.path.join(GIT_EXTRAS_BIN, name), *params] print(f"Run the script \"{script}\"") return subprocess.run(script, capture_output=True) class TempRepository: def __init__(self, repo_work_dir = None): + self._system_tmpdir = tempfile.gettempdir() if repo_work_dir == None: repo_work_dir = tempfile.mkdtemp() + else: + repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir) self._cwd = repo_work_dir + self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:] self._git_repo = git.Repo.init(repo_work_dir) self._files = [] @@ -26,6 +28,9 @@ class TempRepository: def get_cwd(self): return self._cwd + def get_repo_dirname(self): + return self._tempdirname + def get_repo_git(self): return self._git_repo.git @@ -63,6 +68,33 @@ class TempRepository: print(f"The temp directory {self._cwd} has been removed") def invoke_extras_command(self, name, *params): - command = "git-" + name - print(f"Invoke the git-extras command - {command}") - return invoke_git_extras_command(command, *params) + command_name = "git-" + name + print(f"Invoke the git-extras command - {command_name}") + return invoke_git_extras_command(command_name, *params) + + def invoke_installed_extras_command(self, name, *params): + command_name = "git-" + name + print(f"Invoke the git-extras command - {command_name}") + origin_extras_command = os.path.join(GIT_EXTRAS_BIN, command_name) + temp_extras_command = os.path.join(self._cwd, command_name) + helpers = [ + os.path.join(GIT_EXTRAS_HELPER, "git-extra-utility"), + os.path.join(GIT_EXTRAS_HELPER, "is-git-repo")] + + if not os.path.exists(temp_extras_command): + whole = [] + with open(temp_extras_command, "w") as t: + for helper in helpers: + with open(helper) as h: + content = h.read() + whole.extend(content.splitlines()) + with open(origin_extras_command) as o: + content = o.read() + first, *rest = content.splitlines() + whole.extend(rest) + whole.insert(0, first) + t.write("\n".join(whole)) + print("Update file {temp_extras_command}:\n{t.read()}") + os.chmod(temp_extras_command, 0o775) + + return subprocess.run([temp_extras_command, *params], capture_output=True) diff --git a/tests/test_archive_file.py b/tests/test_archive_file.py new file mode 100644 index 0000000..5ca4860 --- /dev/null +++ b/tests/test_archive_file.py @@ -0,0 +1,70 @@ +import os, pytest + +class TestGitArchiveFile: + def test_init(self, temp_repo): + git = temp_repo.get_repo_git() + tmp_file = temp_repo.get_file(0) + temp_repo.writefile(tmp_file, "data") + git.add(".") + git.commit("-m", "test: add data") + git.tag("0.1.0", "-m", "bump: 0.1.0") + + def test_archive_file_on_tags_branch(self, temp_repo): + git = temp_repo.get_repo_git() + git.checkout("-b", "tags0.1.0") + temp_repo.invoke_installed_extras_command("archive-file") + filename = "{0}.{1}.zip".format(temp_repo.get_repo_dirname(), git.describe()) + assert filename in os.listdir() + + def test_archive_file_on_any_not_tags_branch_without_default_branch(self, temp_repo): + git = temp_repo.get_repo_git() + git.checkout("-b", "not-tags-branch") + temp_repo.invoke_installed_extras_command("archive-file") + filename = "{0}.{1}.{2}.zip".format( + temp_repo.get_repo_dirname(), + git.describe("--always", "--long"), + "not-tags-branch") + assert filename in os.listdir() + + def test_archive_file_on_any_not_tags_branch_with_default_branch(self, temp_repo): + git = temp_repo.get_repo_git() + git.checkout("master") + git.config("git-extras.default-branch", "master") + temp_repo.invoke_installed_extras_command("archive-file") + filename = "{0}.{1}.zip".format( + temp_repo.get_repo_dirname(), + git.describe("--always", "--long")) + assert filename in os.listdir() + + def test_archive_file_on_branch_name_has_slash(self, temp_repo): + git = temp_repo.get_repo_git() + git.checkout("-b", "feature/slash") + temp_repo.invoke_installed_extras_command("archive-file") + filename = "{0}.{1}.{2}.zip".format( + temp_repo.get_repo_dirname(), + git.describe("--always", "--long"), + "feature-slash") + assert filename in os.listdir() + + @pytest.mark.parametrize("named_temp_repo", ["backslash\\dir"], indirect=True) + def test_archive_file_on_dirname_has_backslash(self, named_temp_repo): + named_temp_repo.invoke_installed_extras_command("archive-file") + git = named_temp_repo.get_repo_git() + filename = "{0}.{1}.{2}.zip".format( + "backslash-dir", + git.describe("--always", "--long"), + "master") + assert filename in os.listdir() + + def test_archive_file_on_tag_name_has_slash(self, temp_repo): + temp_repo.switch_cwd_under_repo() + git = temp_repo.get_repo_git() + git.checkout("master") + git.tag("--delete", "0.1.0") + git.tag("0.1.0/slash", "-m", "bump: 0.1.0") + temp_repo.invoke_installed_extras_command("archive-file") + description_include_version = git.describe("--always", "--long") + filename = "{0}.{1}.zip".format( + temp_repo.get_repo_dirname(), + description_include_version.replace("/", "-")) + assert filename in os.listdir()