Skip to content

Commit

Permalink
Merge pull request #23 from tjtharrison/16-variable-for-additional-gi…
Browse files Browse the repository at this point in the history
…t-commands

feat: Added --branch flag to provide custom branch
  • Loading branch information
tjtharrison authored Apr 23, 2023
2 parents 17eb7dc + 018fb07 commit 13f4e70
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 99 deletions.
15 changes: 14 additions & 1 deletion gpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
action="store",
help="[Default: None] Override message prompt and use the provided message",
)
parser.add_argument(
"--branch",
action="store",
default="current",
help="[Default: current] Override using the current branch and use the provided branch",
)


args = parser.parse_args()

Expand Down Expand Up @@ -80,8 +87,14 @@ def git_push():
:return: True/False
"""
try:
if str(args.branch) != "current":
branch_name = str(args.branch)
else:
repo = Repo(search_parent_directories=True)
branch_name = repo.active_branch
repo = Repo(search_parent_directories=True)
repo.git.push("--set-upstream", "origin", repo.active_branch)
repo.create_head(branch_name)
repo.git.push("--set-upstream", "origin", branch_name)
print("Pushed successfully")
except Exception as error_message:
print("Some error occurred while pushing the code:")
Expand Down
11 changes: 0 additions & 11 deletions tests/bin/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ def cleanup_local():
print("The directory " + test_repo_name + " does not exist")
return True

def create_branch():
"""Create a branch on a repo."""
print("Creating branch " + test_repo_name)
repo = api.repos.create_branch(
repo=test_repo_name,
owner=get_user().login,
branch="test-branch",
)
print("Cloned repo " + test_repo_name)
return repo

# Use python git to clone repository
def clone_repo():
"""Clone the repository."""
Expand Down
59 changes: 59 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest
import tests.bin.github as github
from gpush import git_commit
import subprocess


@pytest.fixture(autouse=True)
def my_fixture():
"""
Wrapper for config unit tests to back up and restore configuration to test field manipulation.
:return:
"""
github.create_repo()
github.create_readme()
github.clone_repo()
yield
github.delete_repo()
github.cleanup_local()


def test_git_commit_and_push():
"""
Function to test the git_commit function in gpush.py with no-push.
:return:
"""
try:
commands = [
"cd gpush-test",
"git checkout -b test-branch",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Branch setup locally")
except Exception as error_message:
print("Some error occurred while setting up the branch locally")
print(str(error_message))
raise

try:
commands = [
"cd gpush-test",
"touch test",
"git add test",
"../gpush.py --message 'fix: test commit'",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Commit made successfully")
last_commit = subprocess.run("cd gpush-test; git log --pretty=oneline | head -n1", capture_output=True, shell=True)
last_commit_message = (str(last_commit.stdout))
except Exception as error_message:
print("Some error occurred while committing to the branch locally")
print(str(error_message))
raise

if last_commit_message.endswith(" fix: test commit\\n'"):
assert True
else:
print("Commit message not as expected")
print("Last commit message: " + last_commit_message)
assert False
87 changes: 0 additions & 87 deletions tests/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,6 @@ def my_fixture():
github.cleanup_local()


def test_git_commit_and_push():
"""
Function to test the git_commit function in gpush.py with no-push.
:return:
"""
try:
commands = [
"cd gpush-test",
"git checkout -b test-branch",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Branch setup locally")
except Exception as error_message:
print("Some error occurred while setting up the branch locally")
print(str(error_message))
raise

try:
commands = [
"cd gpush-test",
"touch test",
"git add test",
"../gpush.py --message 'fix: test commit'",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Commit made successfully")
last_commit = subprocess.run("cd gpush-test; git log --pretty=oneline | head -n1", capture_output=True, shell=True)
last_commit_message = (str(last_commit.stdout))
except Exception as error_message:
print("Some error occurred while committing to the branch locally")
print(str(error_message))
raise

if last_commit_message.endswith(" fix: test commit\\n'"):
assert True
else:
print("Commit message not as expected")
print("Last commit message: " + last_commit_message)
assert False


def test_git_push_only():
"""
Function to test the git_commit function in gpush.py with no-commit.
:return:
"""
try:
commands = [
"cd gpush-test",
"git checkout -b test-branch",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Branch setup locally")
except Exception as error_message:
print("Some error occurred while setting up the branch locally")
print(str(error_message))
raise

try:
commands = [
"cd gpush-test",
"touch test-push",
"git add test-push",
"git commit -m \"Test manual commit\"",
"../gpush.py --no-commit",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Code pushed successfully")
except Exception as error_message:
print("Some error occurred while committing to the branch locally")
print(str(error_message))
raise

try:
last_commit_message = github.get_last_commit_message("test-branch")
except Exception as error_message:
print("Some error occurred while getting the last commit message")
print(str(error_message))
raise

if last_commit_message == "Test manual commit":
assert True
else:
print("Commit message not as expected")
print("Last commit message: " + last_commit_message)
assert False

def test_git_commit_only():
"""
Function to test the git_commit function in gpush.py with no-push.
Expand Down
112 changes: 112 additions & 0 deletions tests/test_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import pytest
import tests.bin.github as github
from gpush import git_commit
import subprocess


@pytest.fixture(autouse=True)
def my_fixture():
"""
Wrapper for config unit tests to back up and restore configuration to test field manipulation.
:return:
"""
github.create_repo()
github.create_readme()
github.clone_repo()
yield
github.delete_repo()
github.cleanup_local()


def test_git_push_only():
"""
Function to test the git_commit function in gpush.py with no-commit.
:return:
"""
try:
commands = [
"cd gpush-test",
"git checkout -b test-branch",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Branch setup locally")
except Exception as error_message:
print("Some error occurred while setting up the branch locally")
print(str(error_message))
raise

try:
commands = [
"cd gpush-test",
"touch test-push",
"git add test-push",
"git commit -m \"Test manual commit\"",
"../gpush.py --no-commit",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Code pushed successfully")
except Exception as error_message:
print("Some error occurred while committing to the branch locally")
print(str(error_message))
raise

try:
last_commit_message = github.get_last_commit_message("test-branch")
except Exception as error_message:
print("Some error occurred while getting the last commit message")
print(str(error_message))
raise

if last_commit_message == "Test manual commit":
assert True
else:
print("Commit message not as expected")
print("Last commit message: " + last_commit_message)
assert False


def test_git_push_only_custom_branch():
"""
Function to test the git_commit function in gpush.py with no-commit and specifying custom branch.
:return:
"""
try:
commands = [
"cd gpush-test",
"git checkout -b test-branch",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Branch setup locally")
except Exception as error_message:
print("Some error occurred while setting up the branch locally")
print(str(error_message))
raise

try:
commands = [
"cd gpush-test",
"touch test-push",
"git add test-push",
"git commit -m \"Test manual commit\"",
"../gpush.py --no-commit --branch test-branch-2",
]
ret = subprocess.run(";".join(commands), capture_output=True, shell=True)
print("Code pushed successfully")
except Exception as error_message:
print("Some error occurred while committing to the branch locally")
print(str(error_message))
raise

try:
last_commit_message = github.get_last_commit_message("test-branch-2")
except Exception as error_message:
print("Some error occurred while getting the last commit message")
print(str(error_message))
raise

if last_commit_message == "Test manual commit":
assert True
else:
print("Commit message not as expected")
print("Last commit message: " + last_commit_message)
assert False

0 comments on commit 13f4e70

Please sign in to comment.