Skip to content

Commit

Permalink
test(git-browse): add unit tests (#1127)
Browse files Browse the repository at this point in the history
* chore: add the dependency [email protected]

* test(git-browse): add unit tests

* fix: update origin name to `tj/git-extras`
  • Loading branch information
vanpipy authored Jan 5, 2024
1 parent 9049967 commit b54d8c7
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 15 additions & 1 deletion tests/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
231 changes: 231 additions & 0 deletions tests/test_git_browse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import os, subprocess
from testpath import MockCommand, modified_env

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")

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])

0 comments on commit b54d8c7

Please sign in to comment.