Skip to content

Commit

Permalink
test(TempRepository): support to change origin and get filename
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpipy committed Feb 21, 2024
1 parent 6914e81 commit 285f228
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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:
pass
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)

0 comments on commit 285f228

Please sign in to comment.