Skip to content

Commit

Permalink
refactors out common code.
Browse files Browse the repository at this point in the history
  • Loading branch information
birkin committed Jan 2, 2025
1 parent 66e1f45 commit 438f88f
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions lib_call_runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ def run_initial_tests(uv_path: Path, project_path: Path, project_email_addresses
## set the venv -------------------------------------------------
venv_tuple: tuple[Path, Path] = lib_common.determine_venv_paths(project_path) # these are resolved-paths
(venv_bin_path, venv_path) = venv_tuple
local_scoped_env = os.environ.copy()
local_scoped_env['PATH'] = f'{venv_bin_path}:{local_scoped_env["PATH"]}' # prioritizes venv-path
local_scoped_env['VIRTUAL_ENV'] = str(venv_path)
local_scoped_env = make_local_scoped_env(project_path, venv_bin_path, venv_path)
## prep the command ---------------------------------------------
python_path = (
venv_bin_path / 'python3'
) # don't resolve() -- venv_bin_path is already resolved; it'll use the system python-path and we want the venv python-path
log.debug(f'python_path, ``{python_path}``')
run_tests_path = project_path / 'run_tests.py' # no need to resolve; project_path is already resolved
command = [str(python_path), str(run_tests_path)]
command = make_run_tests_command(project_path, venv_bin_path)
log.debug(f'initial runtests command: ``{command}``')
## run the command ----------------------------------------------
try:
Expand Down Expand Up @@ -61,23 +54,44 @@ def run_followup_tests(uv_path: Path, project_path: Path, project_email_addresse
## set the venv -------------------------------------------------
venv_tuple: tuple[Path, Path] = lib_common.determine_venv_paths(project_path) # these are resolved-paths
(venv_bin_path, venv_path) = venv_tuple
local_scoped_env = os.environ.copy()
local_scoped_env['PATH'] = f'{venv_bin_path}:{local_scoped_env["PATH"]}' # prioritizes venv-path
local_scoped_env['VIRTUAL_ENV'] = str(venv_path)
local_scoped_env = make_local_scoped_env(project_path, venv_bin_path, venv_path)
## prep the command ---------------------------------------------
python_path = (
venv_bin_path / 'python3'
) # don't resolve() -- venv_bin_path is already resolved; it'll use the system python-path and we want the venv python-path
log.debug(f'python_path, ``{python_path}``')
run_tests_path = project_path / 'run_tests.py' # no need to resolve; project_path is already resolved
command = [str(python_path), str(run_tests_path)]
log.debug(f'followup runtests command: ``{command}``')
command = make_run_tests_command(project_path, venv_bin_path)
log.debug(f'initial runtests command: ``{command}``')
## run the command ----------------------------------------------
try:
subprocess.run(command, check=True)
subprocess.run(command, check=True, env=local_scoped_env)
return_val = None
except subprocess.CalledProcessError as e:
message = f'Error on followup run_tests() call: ``{e}``.'
log.exception(message)
return_val = message
return return_val


## helpers to the above main functions ------------------------------


def make_local_scoped_env(project_path: Path, venv_bin_path: Path, venv_path: Path) -> dict:
"""
Creates a local-scoped environment for use in subprocess.run() calls.
Called by run_initial_tests() and run_followup_tests().
"""
local_scoped_env = os.environ.copy()
local_scoped_env['PATH'] = f'{venv_bin_path}:{local_scoped_env["PATH"]}' # prioritizes venv-path
local_scoped_env['VIRTUAL_ENV'] = str(venv_path)
return local_scoped_env


def make_run_tests_command(project_path: Path, venv_bin_path: Path) -> list[str]:
"""
Prepares the run_tests command.
Called by run_initial_tests() and run_followup_tests().
"""
python_path = (
venv_bin_path / 'python3'
) # don't resolve() -- venv_bin_path is already resolved; it'll use the system python-path and we want the venv python-path
log.debug(f'python_path, ``{python_path}``')
run_tests_path = project_path / 'run_tests.py' # no need to resolve; project_path is already resolved
command = [str(python_path), str(run_tests_path)]
return command

0 comments on commit 438f88f

Please sign in to comment.