user14333482
user14333482

Reputation:

Creating temp GIT repo for testing is not working

Hi guys my function (get_commit_sha) gets the commit sha from the latest commit. I now have to test this function. For that I have to create different test szenarios and a couple temp git repositories only for testing which will be created in the test functions. In this repository I want to push "fake", "senseles" commits just for testing the function.

Upvotes: 1

Views: 773

Answers (1)

Roman Pavelka
Roman Pavelka

Reputation: 4191

Just create temporary directory using tempfile standard library:

https://docs.python.org/3/library/tempfile.html

Change working directory to the new temp directory: https://docs.python.org/3/library/os.html#os.chdir

Then either use os.system("git init && touch file && git add file && git commit -m Test") or use git python library:

https://gitpython.readthedocs.io/en/stable/tutorial.html#tutorial-label

Cleanup by deleting the temp directory:

Easiest way to rm -rf in Python

E.g.: Create test repo like this:

import os
import tempfile

def test_repo():
    """Creates test repository with single commit and return its path."""
    temporary_dir = tempfile.mkdtemp()
    os.chdir(temporary_dir)

    os.system("git init")
    os.system("touch file1")
    os.system("git add file1")
    os.system("git commit -m Test")

    return temporary_dir

print(test_repo())

Upvotes: 1

Related Questions