diff --git a/gpush.py b/gpush.py index 485c8e5..a7ffd70 100755 --- a/gpush.py +++ b/gpush.py @@ -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() @@ -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:") diff --git a/tests/bin/github.py b/tests/bin/github.py index 2dcb0b2..1a45920 100644 --- a/tests/bin/github.py +++ b/tests/bin/github.py @@ -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.""" diff --git a/tests/test_all.py b/tests/test_all.py new file mode 100644 index 0000000..45d5f9c --- /dev/null +++ b/tests/test_all.py @@ -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 diff --git a/tests/test_commit.py b/tests/test_commit.py index 1bb4512..cb926e5 100644 --- a/tests/test_commit.py +++ b/tests/test_commit.py @@ -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. diff --git a/tests/test_push.py b/tests/test_push.py new file mode 100644 index 0000000..8f1d4db --- /dev/null +++ b/tests/test_push.py @@ -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