Skip to content
Snippets Groups Projects

force `subprocess.run` to check return code is 0

Merged Hans Fangohr requested to merge force-checking-of-error-code into linux-debian11
+ 40
4
@@ -9,6 +9,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):
# create a temporary directory for testing
temp_dir = tmp_path / "test_os_chdir"
@@ -31,18 +60,25 @@ def test_prepare_environment(tmp_path):
# prepare_env is run when cmd is not specified, we can test cmd='prepare'
# and cmd=None to check both cases
script_dir = tmp_path / "test_prepare_env"
script_dir = tmp_path / "mpsd_opt" / "linux_debian_11"
spack_environments = "spack-environments"
mpsd_release_to_test = "dev-23a"
release_base_dir = script_dir / mpsd_release_to_test
# check that the test directory does not exist
assert not script_dir.exists()
# prepare_environment expects to be executed in git repository
# (mpsd-software-environments). It queries the commit on which we are to
# 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_mock_git_repository(script_dir)
# now call the function we want to test
result = mod.prepare_environment(
mpsd_release=mpsd_release_to_test, script_dir=(script_dir)
mpsd_release=mpsd_release_to_test, script_dir=script_dir
)
# wait for 20 seconds for the git clone to finish
# time.sleep(20)
# check if the directory now is created
assert release_base_dir.exists()
# check for spack-environments directory
Loading