diff --git a/tests/README.md b/tests/README.md index 75b859b3c..c8271ef71 100644 --- a/tests/README.md +++ b/tests/README.md @@ -12,7 +12,7 @@ The test part depends on: So the versions are higher than above is recommended. -# How to test +# How to run the tests 1. Install `poetry` 2. Install the dependencies via `poetry install --no-root` 3. Run `poetry run pytest` @@ -26,6 +26,18 @@ It is done or go without `poetry`, The second way maybe blocked the some missing dependencies at someday, so the first one is recommended. +# What and how to create a unit test +One command has a unit test, because one `git-*` command is just do one thing, so we can eat a piece of `git-*` command in one time. + +For example, + +1. The `git-alias` should have a test suite, so create `test_git_alias.py` in the directory `test` +2. Create a test class `TestGitAlias` in the `test_git_alias.py` +3. Create a test case `test_init`, and some test fixtures can be used, `temp_repo`, `named_temp_repo` etc. + * `temp_repo` is module scoped fixture which create a temporary directory and available in the test suite `test_git_alias.py`. + * `named_temp_repo` is just same as `temp_repo` except the custom directory renaming. +4. Loop the third step until the 100% coverage of the function of the `git-alias` + # References * [poetry](https://github.com/python-poetry/poetry) * [pytest](https://github.com/pytest-dev/pytest/) diff --git a/tests/helper.py b/tests/helper.py index 9ed50cca7..1c9349f9e 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -1,10 +1,15 @@ import os, subprocess, shutil, tempfile -from git import Repo +from git import Repo, GitCommandError +from testpath import MockCommand, modified_env CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) GIT_EXTRAS_BIN = os.path.abspath(os.path.join(CURRENT_DIR, "..", "bin")) GIT_EXTRAS_HELPER = os.path.abspath(os.path.join(CURRENT_DIR, "..", "helper")) +GITHUB_ORIGIN = "https://github.com/tj/git-extras.git" +GITLAB_ORIGIN = "https://gitlab.com/tj/git-extras.git" +BITBUCKET_ORIGIN = "https://bitbucket.org/tj/git-extras.git" + class TempRepository: def __init__(self, repo_work_dir = None): self._system_tmpdir = tempfile.gettempdir() @@ -16,6 +21,7 @@ def __init__(self, repo_work_dir = None): self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:] self._git_repo = Repo.init(repo_work_dir, b="default") self._files = [] + self.change_origin_to_github() def switch_cwd_under_repo(self): os.chdir(self._cwd) @@ -33,6 +39,10 @@ def get_repo_git(self): def get_file(self, index): return self._files[index] + def get_filename(self, index): + file = self._files[index] + return file[1:] + def get_files(self): return self._files @@ -98,3 +108,19 @@ def invoke_installed_extras_command(self, name, *params): script = [temp_extras_command, *params] print(f"Run the script \"{script}\"") return subprocess.run(script, capture_output=True) + + def change_origin(self, origin_url): + try: + self._git_repo.git.remote("add", "origin", origin_url) + except GitCommandError as err: + print(err) + self._git_repo.git.remote("set-url", "origin", origin_url) + + def change_origin_to_github(self): + self.change_origin(GITHUB_ORIGIN) + + def change_origin_to_gitlab(self): + self.change_origin(GITLAB_ORIGIN) + + def change_origin_to_bitbucket(self): + self.change_origin(BITBUCKET_ORIGIN) diff --git a/tests/test_authors.py b/tests/test_git_authors.py similarity index 100% rename from tests/test_authors.py rename to tests/test_git_authors.py diff --git a/tests/test_git_browse.py b/tests/test_git_browse.py index b2e2d70fb..ff4b4db9c 100644 --- a/tests/test_git_browse.py +++ b/tests/test_git_browse.py @@ -1,231 +1,195 @@ 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" +UNKNOWN_SITE_ORIGIN = "https://unknown-site.com/tj/git-extras.git" -def set_origin_url(git, url): - git.remote("set-url", "origin", url + ".git") - -def create_expected_filename(git, origin, mode, filename): +def get_file_uri(mode, filename, git): commit_hash = git.rev_parse("HEAD") - connector = "" if mode == "github": - connector = "/blob/" + return "https://github.com/tj/git-extras/blob/" + commit_hash + '/' + filename if mode == "gitlab": - connector = "/-/blob/" + return "https://gitlab.com/tj/git-extras/-/blob/" + commit_hash + '/' + filename if mode == "bitbucket": - connector = "/src/" - return origin + connector + commit_hash + filename + return "https://bitbucket.org/tj/git-extras/src/" + 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called([expected_url]) def test_browse_gitlab_file_on_mac(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called([expected_url]) def test_browse_bitbucket_file_on_mac(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called([expected_url]) def test_browse_github_file_on_git_bash_on_window(self, temp_repo): + temp_repo.change_origin_to_github() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called([expected_url]) def test_browse_gitlab_file_on_git_bash_on_window(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called([expected_url]) def test_browse_bitbucket_file_on_git_bash_on_window(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called([expected_url]) def test_browse_github_file_on_WSL_with_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_github() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_gitlab_file_on_WSL_with_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_bitbucket_file_on_WSL_with_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_github_file_on_WSL_without_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_github() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called([expected_url]) def test_browse_gitlab_file_on_WSL_without_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called([expected_url]) def test_browse_bitbucket_file_on_WSL_without_microsoft_key(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called([expected_url]) def test_browse_github_file_not_mac_or_msys_or_linux(self, temp_repo): + temp_repo.change_origin_to_github() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called([expected_url]) def test_browse_gitlab_file_not_mac_or_msys_or_linux(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called([expected_url]) def test_browse_bitbucket_file_not_mac_or_msys_or_linux(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename) + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called([expected_url]) def test_browse_github_file_with_line_number(self, temp_repo): + temp_repo.change_origin_to_github() 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"]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename, "10", "20") + expected_url = get_file_uri("github", filename, git) + openCommand.assert_called([expected_url + "#L10-L20"]) def test_browse_gitlab_file_with_line_number(self, temp_repo): + temp_repo.change_origin_to_gitlab() 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"]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename, "10", "20") + expected_url = get_file_uri("gitlab", filename, git) + openCommand.assert_called([expected_url + "#L10-20"]) - def test_browse_github_file_with_line_number(self, temp_repo): + def test_browse_bitbucket_file_with_line_number(self, temp_repo): + temp_repo.change_origin_to_bitbucket() 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"]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename, "10", "20") + expected_url = get_file_uri("bitbucket", filename, git) + openCommand.assert_called([expected_url + "#lines-10:20"]) def test_browse_unknown_site_file(self, temp_repo): + temp_repo.change_origin(UNKNOWN_SITE_ORIGIN) 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]) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin") + openCommand.assert_called([UNKNOWN_SITE_ORIGIN[0:-4]]) 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]) + filename = temp_repo.get_filename(0) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse", "origin", filename, "10", "20") + openCommand.assert_called([UNKNOWN_SITE_ORIGIN[0:-4]]) diff --git a/tests/test_git_browse_ci.py b/tests/test_git_browse_ci.py index 0a840e6c1..761d86d64 100644 --- a/tests/test_git_browse_ci.py +++ b/tests/test_git_browse_ci.py @@ -1,174 +1,129 @@ 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" +UNKNOWN_SITE_ORIGIN = "https://unknown-site.com/tj/git-extras.git" -def set_origin_url(git, url): - git.remote("set-url", "origin", url + ".git") - -def create_expected_action_url(git, mode): +def get_ci_uri_by_domain(mode): if mode == "github": - return github_origin + '/actions' + return "https://github.com/tj/git-extras/actions" if mode == "gitlab": - return gitlab_origin + '/-/pipelines' + return "https://gitlab.com/tj/git-extras/-/pipelines" if mode == "bitbucket": - return bitbucket_origin + '/addon/pipelines/home' + return "https://bitbucket.org/tj/git-extras/addon/pipelines/home" class TestGitBrowse: def test_browse_github_ci_on_mac(self, temp_repo): - git = temp_repo.get_repo_git() - git.remote("add", "origin", github_origin + ".git") - with modified_env({ "OSTYPE": "darwin" }): - with MockCommand("open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "github") - openCommand.assert_called([expected_url]) + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("github") + openCommand.assert_called([expected_url]) def test_browse_gitlab_ci_on_mac(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, gitlab_origin) - with modified_env({ "OSTYPE": "darwin" }): - with MockCommand("open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "gitlab") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_gitlab() + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("gitlab") + openCommand.assert_called([expected_url]) def test_browse_bitbucket_ci_on_mac(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, bitbucket_origin) - with modified_env({ "OSTYPE": "darwin" }): - with MockCommand("open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "bitbucket") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_bitbucket() + with modified_env({ "OSTYPE": "darwin" }), MockCommand("open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("bitbucket") + openCommand.assert_called([expected_url]) def test_browse_github_ci_on_git_bash_on_window(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, github_origin) - with modified_env({ "OSTYPE": "msys" }): - with MockCommand("start") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "github") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_github() + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("github") + openCommand.assert_called([expected_url]) def test_browse_gitlab_ci_on_git_bash_on_window(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, gitlab_origin) - with modified_env({ "OSTYPE": "msys" }): - with MockCommand("start") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "gitlab") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_gitlab() + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("gitlab") + openCommand.assert_called([expected_url]) def test_browse_bitbucket_ci_on_git_bash_on_window(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, bitbucket_origin) - with modified_env({ "OSTYPE": "msys" }): - with MockCommand("start") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "bitbucket") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_bitbucket() + with modified_env({ "OSTYPE": "msys" }), MockCommand("start") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("bitbucket") + openCommand.assert_called([expected_url]) def test_browse_github_ci_on_WSL_with_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, github_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "github") - openCommand.assert_called(["-NoProfile", "start", expected_url]) + temp_repo.change_origin_to_github() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("github") + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_gitlab_ci_on_WSL_with_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, gitlab_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "gitlab") - openCommand.assert_called(["-NoProfile", "start", expected_url]) + temp_repo.change_origin_to_gitlab() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("gitlab") + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_bitbucket_ci_on_WSL_with_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, bitbucket_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "bitbucket") - openCommand.assert_called(["-NoProfile", "start", expected_url]) + temp_repo.change_origin_to_bitbucket() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "microsoft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("powershell.exe") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("bitbucket") + openCommand.assert_called(["-NoProfile", "start", expected_url]) def test_browse_github_ci_on_WSL_without_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, github_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "github") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_github() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("github") + openCommand.assert_called([expected_url]) def test_browse_gitlab_ci_on_WSL_without_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, gitlab_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "gitlab") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_gitlab() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("gitlab") + openCommand.assert_called([expected_url]) def test_browse_bitbucket_ci_on_WSL_without_microsoft_key(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, bitbucket_origin) - 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-ci", "origin") - expected_url = create_expected_action_url(git, "bitbucket") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_bitbucket() + with modified_env({ "OSTYPE": "linux" }), MockCommand.fixed_output("uname", "no-micro-soft"), \ + MockCommand.fixed_output("command", "/powershell.exe"), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("bitbucket") + openCommand.assert_called([expected_url]) def test_browse_github_ci_not_mac_or_msys_or_linux(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, github_origin) - with modified_env({ "OSTYPE": "unique-system" }): - with MockCommand("xdg-open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "github") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_github() + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("github") + openCommand.assert_called([expected_url]) def test_browse_gitlab_ci_not_mac_or_msys_or_linux(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, gitlab_origin) - with modified_env({ "OSTYPE": "unique-system" }): - with MockCommand("xdg-open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "gitlab") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_gitlab() + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("gitlab") + openCommand.assert_called([expected_url]) def test_browse_bitbucket_ci_not_mac_or_msys_or_linux(self, temp_repo): - git = temp_repo.get_repo_git() - set_origin_url(git, bitbucket_origin) - with modified_env({ "OSTYPE": "unique-system" }): - with MockCommand("xdg-open") as openCommand: - temp_repo.invoke_extras_command("browse-ci", "origin") - expected_url = create_expected_action_url(git, "bitbucket") - openCommand.assert_called([expected_url]) + temp_repo.change_origin_to_bitbucket() + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + expected_url = get_ci_uri_by_domain("bitbucket") + openCommand.assert_called([expected_url]) 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-ci", "origin") - openCommand.assert_called(['']) + temp_repo.change_origin(UNKNOWN_SITE_ORIGIN) + with modified_env({ "OSTYPE": "unique-system" }), MockCommand("xdg-open") as openCommand: + temp_repo.invoke_extras_command("browse-ci", "origin") + openCommand.assert_called([''])