From 790ace306eecc844e2f4a6ef388804bcbfe8d4b2 Mon Sep 17 00:00:00 2001 From: vanpipy Date: Tue, 26 Dec 2023 19:05:41 +0800 Subject: [PATCH 1/3] chore: add the dependency testpath@0.6.0 --- .github/workflows/ci.yml | 3 +-- tests/poetry.lock | 16 +++++++++++++++- tests/pyproject.toml | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ecbcf5b1..5aede64e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,8 +50,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest==7.4.0 - pip install GitPython==3.1.40 + pip install pytest==7.4.0 GitPython==3.1.40 testpath==0.6.0 - name: Unit test run: make test diff --git a/tests/poetry.lock b/tests/poetry.lock index b1f19b2b8..7be8a7ebf 100644 --- a/tests/poetry.lock +++ b/tests/poetry.lock @@ -110,7 +110,21 @@ files = [ {file = "smmap-5.0.1.tar.gz", hash = "sha256:dceeb6c0028fdb6734471eb07c0cd2aae706ccaecab45965ee83f11c8d3b1f62"}, ] +[[package]] +name = "testpath" +version = "0.6.0" +description = "Test utilities for code working with files and commands" +optional = false +python-versions = ">= 3.5" +files = [ + {file = "testpath-0.6.0-py3-none-any.whl", hash = "sha256:8ada9f80a2ac6fb0391aa7cdb1a7d11cfa8429f693eda83f74dde570fe6fa639"}, + {file = "testpath-0.6.0.tar.gz", hash = "sha256:2f1b97e6442c02681ebe01bd84f531028a7caea1af3825000f52345c30285e0f"}, +] + +[package.extras] +test = ["pytest"] + [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "74f2edc4f13d417cf488f33b42f1e0ef99293b4ddf0bf951c22cf671793c203a" +content-hash = "b7e7874dbe3c45aaa4d22378531624f0900b4c86600fbcb4b323ad5eaf616ca1" diff --git a/tests/pyproject.toml b/tests/pyproject.toml index f62842cf4..7e3b10ade 100644 --- a/tests/pyproject.toml +++ b/tests/pyproject.toml @@ -10,6 +10,7 @@ readme = "README.md" python = "^3.11" pytest = "7.4" gitpython = "3.1.40" +testpath = "0.6.0" [tool.pytest.ini_options] minversion = "7.4" From 60d9262fe5cc8aaf51056641e2cc94ae143a7e14 Mon Sep 17 00:00:00 2001 From: vanpipy Date: Tue, 26 Dec 2023 19:07:15 +0800 Subject: [PATCH 2/3] test(git-browse): add unit tests --- tests/test_git_browse.py | 231 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 tests/test_git_browse.py diff --git a/tests/test_git_browse.py b/tests/test_git_browse.py new file mode 100644 index 000000000..1864d93f7 --- /dev/null +++ b/tests/test_git_browse.py @@ -0,0 +1,231 @@ +import os, subprocess +from testpath import MockCommand, modified_env + +github_origin = "https://github.com/vanpipy/git-extras" +gitlab_origin = "https://gitlab.com/vanpipy/git-extras" +bitbucket_origin = "https://bitbucket.org/vanpipy/git-extras" +unknown_site_origin = "https://unknown-site.com/vanpipy/git-extras" + +def set_origin_url(git, url): + git.remote("set-url", "origin", url + ".git") + +def create_expected_filename(git, origin, mode, filename): + commit_hash = git.rev_parse("HEAD") + connector = "" + if mode == "github": + connector = "/blob/" + if mode == "gitlab": + connector = "/-/blob/" + if mode == "bitbucket": + connector = "/src/" + return origin + connector + commit_hash + filename + +class TestGitBrowse: + def test_browse_github_file_on_mac(self, temp_repo): + git = temp_repo.get_repo_git() + git.remote("add", "origin", github_origin + ".git") + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "darwin" }): + with MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_gitlab_file_on_mac(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "darwin" }): + with MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_bitbucket_file_on_mac(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "darwin" }): + with MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_github_file_on_git_bash_on_window(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, github_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "msys" }): + with MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_gitlab_file_on_git_bash_on_window(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "msys" }): + with MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_bitbucket_file_on_git_bash_on_window(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "msys" }): + with MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_github_file_on_WSL_with_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, github_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "microsoft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called(["-NoProfile", "start", expected_url]) + + def test_browse_gitlab_file_on_WSL_with_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "microsoft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called(["-NoProfile", "start", expected_url]) + + def test_browse_bitbucket_file_on_WSL_with_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "microsoft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called(["-NoProfile", "start", expected_url]) + + def test_browse_github_file_on_WSL_without_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, github_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "no-micro-soft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_gitlab_file_on_WSL_without_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "no-micro-soft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_bitbucket_file_on_WSL_without_microsoft_key(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "linux" }): + with MockCommand.fixed_output("uname", "no-micro-soft"): + with MockCommand.fixed_output("command", "/powershell.exe"): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_github_file_not_mac_or_msys_or_linux(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, github_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_gitlab_file_not_mac_or_msys_or_linux(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_bitbucket_file_not_mac_or_msys_or_linux(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called([expected_url]) + + def test_browse_github_file_with_line_number(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, github_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) + openCommand.assert_called([expected_url + "#L10-20"]) + + def test_browse_gitlab_file_with_line_number(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, gitlab_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) + openCommand.assert_called([expected_url + "#L10-20"]) + + def test_browse_github_file_with_line_number(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, bitbucket_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) + openCommand.assert_called([expected_url + "#lines-10:20"]) + + def test_browse_unknown_site_file(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, unknown_site_origin) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin") + openCommand.assert_called([unknown_site_origin]) + + def test_browse_unknown_site_file_with_line_number(self, temp_repo): + git = temp_repo.get_repo_git() + set_origin_url(git, unknown_site_origin) + tmp_file = temp_repo.get_file(0) + with modified_env({ "OSTYPE": "unique-system" }): + with MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") + openCommand.assert_called([unknown_site_origin]) From 1ea943437a6f886f2c27ef9b38a76fd1f0c1570f Mon Sep 17 00:00:00 2001 From: vanpipy Date: Thu, 4 Jan 2024 15:09:46 +0800 Subject: [PATCH 3/3] fix: update origin name to `tj/git-extras` --- tests/test_git_browse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_git_browse.py b/tests/test_git_browse.py index 1864d93f7..b2e2d70fb 100644 --- a/tests/test_git_browse.py +++ b/tests/test_git_browse.py @@ -1,10 +1,10 @@ import os, subprocess from testpath import MockCommand, modified_env -github_origin = "https://github.com/vanpipy/git-extras" -gitlab_origin = "https://gitlab.com/vanpipy/git-extras" -bitbucket_origin = "https://bitbucket.org/vanpipy/git-extras" -unknown_site_origin = "https://unknown-site.com/vanpipy/git-extras" +github_origin = "https://github.com/tj/git-extras" +gitlab_origin = "https://gitlab.com/tj/git-extras" +bitbucket_origin = "https://bitbucket.org/tj/git-extras" +unknown_site_origin = "https://unknown-site.com/tj/git-extras" def set_origin_url(git, url): git.remote("set-url", "origin", url + ".git")