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.
Asked
Active
Viewed 508 times
1
-
1What about this: Copy the whole codebase to another folder, init a new git repo there, test your code, and if it seems to be working, just copy and paste the whole codebase back to the original repo and commit the changes. Be careful and do not copy the `.git` folder, just the code. – gazdagergo Oct 29 '20 at 11:16
-
1Or test it on your original repo, and if it works, squash all your "test" commits. – gazdagergo Oct 29 '20 at 11:18
1 Answers
1
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())

Roman Pavelka
- 3,736
- 2
- 11
- 28
-
Hi Roman, thanks for your answer :) I have to give an input parameter (repo) to my function get_commit_sha(repo). What should I put in my case? I mean if I do it with your answer. That confused me a little. – Oct 29 '20 at 12:20
-
I changed my test function, could you have a look? @Roman cause I cant write testrepo as an input parameter – Oct 29 '20 at 12:29
-
Do have any idea for good assertions in this test? What I already did is checking the length of the commit sha. I should do more tests, (false positive tests) @Roman – Oct 30 '20 at 08:51
-
1Well you can get the commit sha using git command line tools, using popen: https://stackoverflow.com/a/16561182/12118546 Also you can create empty repo without commit and test what your tools what do then. Also you can test what will happen, when path is entirely invalid, zero length and also when it points to directory without git repo... You can test what it does when there is more commits in the repo etc... – Roman Pavelka Oct 30 '20 at 11:35
-
Thanks I did first commit, more than one commit and empty repo :) Could you upvote my question if you think it was a helpful question? – Oct 30 '20 at 13:44
-
1Is there any way to do this in a loop? creatinf 10 files adding them and commiting? It does not word cause the command in os.system is a string @roman – Nov 02 '20 at 12:21
-
1for file in range(0, 10): test_file = "touch file " + str(file) os.system(test_file) os.system("git add .") os.system("git commit -m Test") – Shalomi90 Nov 02 '20 at 15:03