|
| 1 | +import os, subprocess |
| 2 | +from testpath import MockCommand, modified_env |
| 3 | + |
| 4 | +github_origin = "https://github.com/tj/git-extras" |
| 5 | +gitlab_origin = "https://gitlab.com/tj/git-extras" |
| 6 | +bitbucket_origin = "https://bitbucket.org/tj/git-extras" |
| 7 | +unknown_site_origin = "https://unknown-site.com/tj/git-extras" |
| 8 | + |
| 9 | +def set_origin_url(git, url): |
| 10 | + git.remote("set-url", "origin", url + ".git") |
| 11 | + |
| 12 | +def create_expected_filename(git, origin, mode, filename): |
| 13 | + commit_hash = git.rev_parse("HEAD") |
| 14 | + connector = "" |
| 15 | + if mode == "github": |
| 16 | + connector = "/blob/" |
| 17 | + if mode == "gitlab": |
| 18 | + connector = "/-/blob/" |
| 19 | + if mode == "bitbucket": |
| 20 | + connector = "/src/" |
| 21 | + return origin + connector + commit_hash + filename |
| 22 | + |
| 23 | +class TestGitBrowse: |
| 24 | + def test_browse_github_file_on_mac(self, temp_repo): |
| 25 | + git = temp_repo.get_repo_git() |
| 26 | + git.remote("add", "origin", github_origin + ".git") |
| 27 | + tmp_file = temp_repo.get_file(0) |
| 28 | + with modified_env({ "OSTYPE": "darwin" }): |
| 29 | + with MockCommand("open") as openCommand: |
| 30 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 31 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 32 | + openCommand.assert_called([expected_url]) |
| 33 | + |
| 34 | + def test_browse_gitlab_file_on_mac(self, temp_repo): |
| 35 | + git = temp_repo.get_repo_git() |
| 36 | + set_origin_url(git, gitlab_origin) |
| 37 | + tmp_file = temp_repo.get_file(0) |
| 38 | + with modified_env({ "OSTYPE": "darwin" }): |
| 39 | + with MockCommand("open") as openCommand: |
| 40 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 41 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 42 | + openCommand.assert_called([expected_url]) |
| 43 | + |
| 44 | + def test_browse_bitbucket_file_on_mac(self, temp_repo): |
| 45 | + git = temp_repo.get_repo_git() |
| 46 | + set_origin_url(git, bitbucket_origin) |
| 47 | + tmp_file = temp_repo.get_file(0) |
| 48 | + with modified_env({ "OSTYPE": "darwin" }): |
| 49 | + with MockCommand("open") as openCommand: |
| 50 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 51 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 52 | + openCommand.assert_called([expected_url]) |
| 53 | + |
| 54 | + def test_browse_github_file_on_git_bash_on_window(self, temp_repo): |
| 55 | + git = temp_repo.get_repo_git() |
| 56 | + set_origin_url(git, github_origin) |
| 57 | + tmp_file = temp_repo.get_file(0) |
| 58 | + with modified_env({ "OSTYPE": "msys" }): |
| 59 | + with MockCommand("start") as openCommand: |
| 60 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 61 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 62 | + openCommand.assert_called([expected_url]) |
| 63 | + |
| 64 | + def test_browse_gitlab_file_on_git_bash_on_window(self, temp_repo): |
| 65 | + git = temp_repo.get_repo_git() |
| 66 | + set_origin_url(git, gitlab_origin) |
| 67 | + tmp_file = temp_repo.get_file(0) |
| 68 | + with modified_env({ "OSTYPE": "msys" }): |
| 69 | + with MockCommand("start") as openCommand: |
| 70 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 71 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 72 | + openCommand.assert_called([expected_url]) |
| 73 | + |
| 74 | + def test_browse_bitbucket_file_on_git_bash_on_window(self, temp_repo): |
| 75 | + git = temp_repo.get_repo_git() |
| 76 | + set_origin_url(git, bitbucket_origin) |
| 77 | + tmp_file = temp_repo.get_file(0) |
| 78 | + with modified_env({ "OSTYPE": "msys" }): |
| 79 | + with MockCommand("start") as openCommand: |
| 80 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 81 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 82 | + openCommand.assert_called([expected_url]) |
| 83 | + |
| 84 | + def test_browse_github_file_on_WSL_with_microsoft_key(self, temp_repo): |
| 85 | + git = temp_repo.get_repo_git() |
| 86 | + set_origin_url(git, github_origin) |
| 87 | + tmp_file = temp_repo.get_file(0) |
| 88 | + with modified_env({ "OSTYPE": "linux" }): |
| 89 | + with MockCommand.fixed_output("uname", "microsoft"): |
| 90 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 91 | + with MockCommand("powershell.exe") as openCommand: |
| 92 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 93 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 94 | + openCommand.assert_called(["-NoProfile", "start", expected_url]) |
| 95 | + |
| 96 | + def test_browse_gitlab_file_on_WSL_with_microsoft_key(self, temp_repo): |
| 97 | + git = temp_repo.get_repo_git() |
| 98 | + set_origin_url(git, gitlab_origin) |
| 99 | + tmp_file = temp_repo.get_file(0) |
| 100 | + with modified_env({ "OSTYPE": "linux" }): |
| 101 | + with MockCommand.fixed_output("uname", "microsoft"): |
| 102 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 103 | + with MockCommand("powershell.exe") as openCommand: |
| 104 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 105 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 106 | + openCommand.assert_called(["-NoProfile", "start", expected_url]) |
| 107 | + |
| 108 | + def test_browse_bitbucket_file_on_WSL_with_microsoft_key(self, temp_repo): |
| 109 | + git = temp_repo.get_repo_git() |
| 110 | + set_origin_url(git, bitbucket_origin) |
| 111 | + tmp_file = temp_repo.get_file(0) |
| 112 | + with modified_env({ "OSTYPE": "linux" }): |
| 113 | + with MockCommand.fixed_output("uname", "microsoft"): |
| 114 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 115 | + with MockCommand("powershell.exe") as openCommand: |
| 116 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 117 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 118 | + openCommand.assert_called(["-NoProfile", "start", expected_url]) |
| 119 | + |
| 120 | + def test_browse_github_file_on_WSL_without_microsoft_key(self, temp_repo): |
| 121 | + git = temp_repo.get_repo_git() |
| 122 | + set_origin_url(git, github_origin) |
| 123 | + tmp_file = temp_repo.get_file(0) |
| 124 | + with modified_env({ "OSTYPE": "linux" }): |
| 125 | + with MockCommand.fixed_output("uname", "no-micro-soft"): |
| 126 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 127 | + with MockCommand("xdg-open") as openCommand: |
| 128 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 129 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 130 | + openCommand.assert_called([expected_url]) |
| 131 | + |
| 132 | + def test_browse_gitlab_file_on_WSL_without_microsoft_key(self, temp_repo): |
| 133 | + git = temp_repo.get_repo_git() |
| 134 | + set_origin_url(git, gitlab_origin) |
| 135 | + tmp_file = temp_repo.get_file(0) |
| 136 | + with modified_env({ "OSTYPE": "linux" }): |
| 137 | + with MockCommand.fixed_output("uname", "no-micro-soft"): |
| 138 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 139 | + with MockCommand("xdg-open") as openCommand: |
| 140 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 141 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 142 | + openCommand.assert_called([expected_url]) |
| 143 | + |
| 144 | + def test_browse_bitbucket_file_on_WSL_without_microsoft_key(self, temp_repo): |
| 145 | + git = temp_repo.get_repo_git() |
| 146 | + set_origin_url(git, bitbucket_origin) |
| 147 | + tmp_file = temp_repo.get_file(0) |
| 148 | + with modified_env({ "OSTYPE": "linux" }): |
| 149 | + with MockCommand.fixed_output("uname", "no-micro-soft"): |
| 150 | + with MockCommand.fixed_output("command", "/powershell.exe"): |
| 151 | + with MockCommand("xdg-open") as openCommand: |
| 152 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 153 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 154 | + openCommand.assert_called([expected_url]) |
| 155 | + |
| 156 | + def test_browse_github_file_not_mac_or_msys_or_linux(self, temp_repo): |
| 157 | + git = temp_repo.get_repo_git() |
| 158 | + set_origin_url(git, github_origin) |
| 159 | + tmp_file = temp_repo.get_file(0) |
| 160 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 161 | + with MockCommand("xdg-open") as openCommand: |
| 162 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 163 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 164 | + openCommand.assert_called([expected_url]) |
| 165 | + |
| 166 | + def test_browse_gitlab_file_not_mac_or_msys_or_linux(self, temp_repo): |
| 167 | + git = temp_repo.get_repo_git() |
| 168 | + set_origin_url(git, gitlab_origin) |
| 169 | + tmp_file = temp_repo.get_file(0) |
| 170 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 171 | + with MockCommand("xdg-open") as openCommand: |
| 172 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 173 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 174 | + openCommand.assert_called([expected_url]) |
| 175 | + |
| 176 | + def test_browse_bitbucket_file_not_mac_or_msys_or_linux(self, temp_repo): |
| 177 | + git = temp_repo.get_repo_git() |
| 178 | + set_origin_url(git, bitbucket_origin) |
| 179 | + tmp_file = temp_repo.get_file(0) |
| 180 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 181 | + with MockCommand("xdg-open") as openCommand: |
| 182 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:]) |
| 183 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 184 | + openCommand.assert_called([expected_url]) |
| 185 | + |
| 186 | + def test_browse_github_file_with_line_number(self, temp_repo): |
| 187 | + git = temp_repo.get_repo_git() |
| 188 | + set_origin_url(git, github_origin) |
| 189 | + tmp_file = temp_repo.get_file(0) |
| 190 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 191 | + with MockCommand("xdg-open") as openCommand: |
| 192 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") |
| 193 | + expected_url = create_expected_filename(git, github_origin, "github", tmp_file) |
| 194 | + openCommand.assert_called([expected_url + "#L10-20"]) |
| 195 | + |
| 196 | + def test_browse_gitlab_file_with_line_number(self, temp_repo): |
| 197 | + git = temp_repo.get_repo_git() |
| 198 | + set_origin_url(git, gitlab_origin) |
| 199 | + tmp_file = temp_repo.get_file(0) |
| 200 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 201 | + with MockCommand("xdg-open") as openCommand: |
| 202 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") |
| 203 | + expected_url = create_expected_filename(git, gitlab_origin, "gitlab", tmp_file) |
| 204 | + openCommand.assert_called([expected_url + "#L10-20"]) |
| 205 | + |
| 206 | + def test_browse_github_file_with_line_number(self, temp_repo): |
| 207 | + git = temp_repo.get_repo_git() |
| 208 | + set_origin_url(git, bitbucket_origin) |
| 209 | + tmp_file = temp_repo.get_file(0) |
| 210 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 211 | + with MockCommand("xdg-open") as openCommand: |
| 212 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") |
| 213 | + expected_url = create_expected_filename(git, bitbucket_origin, "bitbucket", tmp_file) |
| 214 | + openCommand.assert_called([expected_url + "#lines-10:20"]) |
| 215 | + |
| 216 | + def test_browse_unknown_site_file(self, temp_repo): |
| 217 | + git = temp_repo.get_repo_git() |
| 218 | + set_origin_url(git, unknown_site_origin) |
| 219 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 220 | + with MockCommand("xdg-open") as openCommand: |
| 221 | + temp_repo.invoke_extras_command("browse", "origin") |
| 222 | + openCommand.assert_called([unknown_site_origin]) |
| 223 | + |
| 224 | + def test_browse_unknown_site_file_with_line_number(self, temp_repo): |
| 225 | + git = temp_repo.get_repo_git() |
| 226 | + set_origin_url(git, unknown_site_origin) |
| 227 | + tmp_file = temp_repo.get_file(0) |
| 228 | + with modified_env({ "OSTYPE": "unique-system" }): |
| 229 | + with MockCommand("xdg-open") as openCommand: |
| 230 | + temp_repo.invoke_extras_command("browse", "origin", tmp_file[1:], "10", "20") |
| 231 | + openCommand.assert_called([unknown_site_origin]) |
0 commit comments