Skip to content

Commit 5f19424

Browse files
authored
test(browse-ci): add unit tests (#1130)
1 parent b54d8c7 commit 5f19424

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

tests/test_git_browse_ci.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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_action_url(git, mode):
13+
if mode == "github":
14+
return github_origin + '/actions'
15+
if mode == "gitlab":
16+
return gitlab_origin + '/-/pipelines'
17+
if mode == "bitbucket":
18+
return bitbucket_origin + '/addon/pipelines/home'
19+
20+
class TestGitBrowse:
21+
def test_browse_github_ci_on_mac(self, temp_repo):
22+
git = temp_repo.get_repo_git()
23+
git.remote("add", "origin", github_origin + ".git")
24+
with modified_env({ "OSTYPE": "darwin" }):
25+
with MockCommand("open") as openCommand:
26+
temp_repo.invoke_extras_command("browse-ci", "origin")
27+
expected_url = create_expected_action_url(git, "github")
28+
openCommand.assert_called([expected_url])
29+
30+
def test_browse_gitlab_ci_on_mac(self, temp_repo):
31+
git = temp_repo.get_repo_git()
32+
set_origin_url(git, gitlab_origin)
33+
with modified_env({ "OSTYPE": "darwin" }):
34+
with MockCommand("open") as openCommand:
35+
temp_repo.invoke_extras_command("browse-ci", "origin")
36+
expected_url = create_expected_action_url(git, "gitlab")
37+
openCommand.assert_called([expected_url])
38+
39+
def test_browse_bitbucket_ci_on_mac(self, temp_repo):
40+
git = temp_repo.get_repo_git()
41+
set_origin_url(git, bitbucket_origin)
42+
with modified_env({ "OSTYPE": "darwin" }):
43+
with MockCommand("open") as openCommand:
44+
temp_repo.invoke_extras_command("browse-ci", "origin")
45+
expected_url = create_expected_action_url(git, "bitbucket")
46+
openCommand.assert_called([expected_url])
47+
48+
def test_browse_github_ci_on_git_bash_on_window(self, temp_repo):
49+
git = temp_repo.get_repo_git()
50+
set_origin_url(git, github_origin)
51+
with modified_env({ "OSTYPE": "msys" }):
52+
with MockCommand("start") as openCommand:
53+
temp_repo.invoke_extras_command("browse-ci", "origin")
54+
expected_url = create_expected_action_url(git, "github")
55+
openCommand.assert_called([expected_url])
56+
57+
def test_browse_gitlab_ci_on_git_bash_on_window(self, temp_repo):
58+
git = temp_repo.get_repo_git()
59+
set_origin_url(git, gitlab_origin)
60+
with modified_env({ "OSTYPE": "msys" }):
61+
with MockCommand("start") as openCommand:
62+
temp_repo.invoke_extras_command("browse-ci", "origin")
63+
expected_url = create_expected_action_url(git, "gitlab")
64+
openCommand.assert_called([expected_url])
65+
66+
def test_browse_bitbucket_ci_on_git_bash_on_window(self, temp_repo):
67+
git = temp_repo.get_repo_git()
68+
set_origin_url(git, bitbucket_origin)
69+
with modified_env({ "OSTYPE": "msys" }):
70+
with MockCommand("start") as openCommand:
71+
temp_repo.invoke_extras_command("browse-ci", "origin")
72+
expected_url = create_expected_action_url(git, "bitbucket")
73+
openCommand.assert_called([expected_url])
74+
75+
def test_browse_github_ci_on_WSL_with_microsoft_key(self, temp_repo):
76+
git = temp_repo.get_repo_git()
77+
set_origin_url(git, github_origin)
78+
with modified_env({ "OSTYPE": "linux" }):
79+
with MockCommand.fixed_output("uname", "microsoft"):
80+
with MockCommand.fixed_output("command", "/powershell.exe"):
81+
with MockCommand("powershell.exe") as openCommand:
82+
temp_repo.invoke_extras_command("browse-ci", "origin")
83+
expected_url = create_expected_action_url(git, "github")
84+
openCommand.assert_called(["-NoProfile", "start", expected_url])
85+
86+
def test_browse_gitlab_ci_on_WSL_with_microsoft_key(self, temp_repo):
87+
git = temp_repo.get_repo_git()
88+
set_origin_url(git, gitlab_origin)
89+
with modified_env({ "OSTYPE": "linux" }):
90+
with MockCommand.fixed_output("uname", "microsoft"):
91+
with MockCommand.fixed_output("command", "/powershell.exe"):
92+
with MockCommand("powershell.exe") as openCommand:
93+
temp_repo.invoke_extras_command("browse-ci", "origin")
94+
expected_url = create_expected_action_url(git, "gitlab")
95+
openCommand.assert_called(["-NoProfile", "start", expected_url])
96+
97+
def test_browse_bitbucket_ci_on_WSL_with_microsoft_key(self, temp_repo):
98+
git = temp_repo.get_repo_git()
99+
set_origin_url(git, bitbucket_origin)
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-ci", "origin")
105+
expected_url = create_expected_action_url(git, "bitbucket")
106+
openCommand.assert_called(["-NoProfile", "start", expected_url])
107+
108+
def test_browse_github_ci_on_WSL_without_microsoft_key(self, temp_repo):
109+
git = temp_repo.get_repo_git()
110+
set_origin_url(git, github_origin)
111+
with modified_env({ "OSTYPE": "linux" }):
112+
with MockCommand.fixed_output("uname", "no-micro-soft"):
113+
with MockCommand.fixed_output("command", "/powershell.exe"):
114+
with MockCommand("xdg-open") as openCommand:
115+
temp_repo.invoke_extras_command("browse-ci", "origin")
116+
expected_url = create_expected_action_url(git, "github")
117+
openCommand.assert_called([expected_url])
118+
119+
def test_browse_gitlab_ci_on_WSL_without_microsoft_key(self, temp_repo):
120+
git = temp_repo.get_repo_git()
121+
set_origin_url(git, gitlab_origin)
122+
with modified_env({ "OSTYPE": "linux" }):
123+
with MockCommand.fixed_output("uname", "no-micro-soft"):
124+
with MockCommand.fixed_output("command", "/powershell.exe"):
125+
with MockCommand("xdg-open") as openCommand:
126+
temp_repo.invoke_extras_command("browse-ci", "origin")
127+
expected_url = create_expected_action_url(git, "gitlab")
128+
openCommand.assert_called([expected_url])
129+
130+
def test_browse_bitbucket_ci_on_WSL_without_microsoft_key(self, temp_repo):
131+
git = temp_repo.get_repo_git()
132+
set_origin_url(git, bitbucket_origin)
133+
with modified_env({ "OSTYPE": "linux" }):
134+
with MockCommand.fixed_output("uname", "no-micro-soft"):
135+
with MockCommand.fixed_output("command", "/powershell.exe"):
136+
with MockCommand("xdg-open") as openCommand:
137+
temp_repo.invoke_extras_command("browse-ci", "origin")
138+
expected_url = create_expected_action_url(git, "bitbucket")
139+
openCommand.assert_called([expected_url])
140+
141+
def test_browse_github_ci_not_mac_or_msys_or_linux(self, temp_repo):
142+
git = temp_repo.get_repo_git()
143+
set_origin_url(git, github_origin)
144+
with modified_env({ "OSTYPE": "unique-system" }):
145+
with MockCommand("xdg-open") as openCommand:
146+
temp_repo.invoke_extras_command("browse-ci", "origin")
147+
expected_url = create_expected_action_url(git, "github")
148+
openCommand.assert_called([expected_url])
149+
150+
def test_browse_gitlab_ci_not_mac_or_msys_or_linux(self, temp_repo):
151+
git = temp_repo.get_repo_git()
152+
set_origin_url(git, gitlab_origin)
153+
with modified_env({ "OSTYPE": "unique-system" }):
154+
with MockCommand("xdg-open") as openCommand:
155+
temp_repo.invoke_extras_command("browse-ci", "origin")
156+
expected_url = create_expected_action_url(git, "gitlab")
157+
openCommand.assert_called([expected_url])
158+
159+
def test_browse_bitbucket_ci_not_mac_or_msys_or_linux(self, temp_repo):
160+
git = temp_repo.get_repo_git()
161+
set_origin_url(git, bitbucket_origin)
162+
with modified_env({ "OSTYPE": "unique-system" }):
163+
with MockCommand("xdg-open") as openCommand:
164+
temp_repo.invoke_extras_command("browse-ci", "origin")
165+
expected_url = create_expected_action_url(git, "bitbucket")
166+
openCommand.assert_called([expected_url])
167+
168+
def test_browse_unknown_site_file(self, temp_repo):
169+
git = temp_repo.get_repo_git()
170+
set_origin_url(git, unknown_site_origin)
171+
with modified_env({ "OSTYPE": "unique-system" }):
172+
with MockCommand("xdg-open") as openCommand:
173+
temp_repo.invoke_extras_command("browse-ci", "origin")
174+
openCommand.assert_called([''])

0 commit comments

Comments
 (0)