From 8495e193d4cac6f61d17e88137ae329d2237b773 Mon Sep 17 00:00:00 2001 From: Hans Fangohr <hans.fangohr@mpsd.mpg.de> Date: Tue, 30 May 2023 10:05:06 +0200 Subject: [PATCH] refactor: we may need this function to create a git repo again. --- tests.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/tests.py b/tests.py index d50667d..b2b8f6e 100644 --- a/tests.py +++ b/tests.py @@ -11,6 +11,35 @@ import pytest mod = importlib.import_module("mpsd-software-environment") +def create_mock_git_repository(target_directory, create_directory=True): + """ + Create a git repository in the directory `target_directory`. + + Arguments + --------- + target_directory : pathlib.Path + - path at which the root of the repository should be located (i.e. `.git` folder) + + create_directory : bool + - create `target_directory` and parent directories if True + + """ + # create directory first + if create_directory: + target_directory.mkdir(parents=True) + + # then create git repository: + with mod.os_chdir(str(target_directory)): + subprocess.run("git init .", shell=True, check=True) + subprocess.run("echo 'fake content' > readme.txt", shell=True, check=True) + subprocess.run("git add readme.txt", shell=True, check=True) + subprocess.run("pwd", shell=True) + subprocess.run( + 'git commit -m "first commit" readme.txt', shell=True, check=True + ) + + + def test_os_chdir(tmp_path): """Test the os_chdir context manager.""" # create a temporary directory for testing @@ -48,18 +77,7 @@ def test_prepare_environment(tmp_path): # log that information. For this to work, we need to execute the command # within a directory tree that has a git repository at the same or high # level. Let's create one: - - # create directory first - script_dir.mkdir(parents=True) - # then create git repository: - with mod.os_chdir(str(script_dir)): - subprocess.run("git init .", shell=True, check=True) - subprocess.run("echo 'fake content' > readme.txt", shell=True, check=True) - subprocess.run("git add readme.txt", shell=True, check=True) - subprocess.run("pwd", shell=True) - subprocess.run( - 'git commit -m "first commit" readme.txt', shell=True, check=True - ) + create_mock_git_repository(script_dir) # now call the function we want to test result = mod.prepare_environment( -- GitLab