-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(git-browse): add unit tests #1127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/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]) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use
tj/git-extrasin the github test case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update