Skip to content

Commit

Permalink
refactors run-followup-tests call.
Browse files Browse the repository at this point in the history
  • Loading branch information
birkin committed Feb 1, 2025
1 parent 6d39ecf commit 3476dca
Showing 1 changed file with 60 additions and 49 deletions.
109 changes: 60 additions & 49 deletions lib_call_runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,47 +67,11 @@ def run_initial_tests(uv_path: Path, project_path: Path, project_email_addresses
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info('ok / initial tests passed')
return


def run_run_tests_command(command: list, project_path: Path, local_scoped_env) -> tuple[bool, dict]:
"""
Runs subprocess command and returns tuple (ok, data_dict).
(Based on similar to `Go` style convention (err, data).)
"""
result: subprocess.CompletedProcess = subprocess.run(
command, cwd=str(project_path), env=local_scoped_env, capture_output=True, text=True
)
log.debug(f'result: {result}')
ok = True if result.returncode == 0 else False
if ok is True:
log.info('ok / run_tests() passed')
output = {'stdout': f'{result.stdout}', 'stderr': f'{result.stderr}'}
return_val = (ok, output)
log.debug(f'return_val: {return_val}')
return return_val


# def run_git_push(project_path: Path) -> tuple[bool, dict]:
# """
# Runs `git push` and return the output.
# """
# log.info('::: running git push ----------')
# command = ['git', 'push', 'origin', 'main']
# result: subprocess.CompletedProcess = subprocess.run(command, cwd=str(project_path), capture_output=True, text=True)
# log.debug(f'result: {result}')
# ok = True if result.returncode == 0 else False
# if ok is True:
# if 'Everything up-to-date' in result.stderr:
# log.info('ok / git push showed "Everything up-to-date"')
# else:
# log.info('ok / git push successful')
# output = {'stdout': f'{result.stdout}', 'stderr': f'{result.stderr}'}
# return_val = (ok, output)
# log.debug(f'return_val: {return_val}')
# return return_val


def run_followup_tests(uv_path: Path, project_path: Path, project_email_addresses: list[list[str, str]]) -> None | str:
"""
Runs followup tests on the updated venv.
Expand All @@ -122,22 +86,52 @@ 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 = make_local_scoped_env(project_path, venv_bin_path, venv_path)
local_scoped_env: dict = make_local_scoped_env(project_path, venv_bin_path, venv_path)
## prep the command ---------------------------------------------
command = make_run_tests_command(project_path, venv_bin_path)
command: list[str] = make_run_tests_command(project_path, venv_bin_path)
## run the command ----------------------------------------------
try:
subprocess.run(command, check=True, env=local_scoped_env)
command_result: tuple[bool, dict] = run_run_tests_command(command, project_path, local_scoped_env)
(ok, output) = command_result
if not ok:
return_val = f'Error on followup run_tests() call: ``{output}``. Continuing processing to update permissions.'
log.exception(return_val)
else:
log.info('ok / followup tests passed')
return_val = None
except subprocess.CalledProcessError as e:
message = f'Error on followup run_tests() call: ``{e}``.'
log.exception(message)
return_val = message
log.debug(f'return_val, ``{return_val}``')
return return_val


# def run_followup_tests(uv_path: Path, project_path: Path, project_email_addresses: list[list[str, str]]) -> None | str:
# """
# Runs followup tests on the updated venv.

# If tests pass returns None.

# If tests fail:
# - returns "tests failed" message (to be add to the diff email)
# - does not exit, so that diffs can be emailed and permissions updated
# """
# log.info('::: running followup tests ----------')
# ## 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 = make_local_scoped_env(project_path, venv_bin_path, venv_path)
# ## prep the command ---------------------------------------------
# command = make_run_tests_command(project_path, venv_bin_path)
# ## run the command ----------------------------------------------
# try:
# subprocess.run(command, check=True, env=local_scoped_env)
# log.info('ok / followup tests passed')
# return_val = None
# except subprocess.CalledProcessError as e:
# message = f'Error on followup run_tests() call: ``{e}``.'
# log.exception(message)
# return_val = message
# log.debug(f'return_val, ``{return_val}``')
# return return_val


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


Expand All @@ -157,12 +151,29 @@ 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().
Note: we're NOT calling resolve() on the python_path.
The venv_bin_path is already resolved, so this will use the venv python-path, which we want.
Resolving again would use the system python-path, which we do not want.
"""
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
python_path = venv_bin_path / 'python3' # no need to resolve; see docstring note above
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'command, ``{command}``')
return command


def run_run_tests_command(command: list, project_path: Path, local_scoped_env) -> tuple[bool, dict]:
"""
Runs subprocess command and returns tuple (ok, data_dict).
(Based on similar to `Go` style convention (err, data).)
"""
result: subprocess.CompletedProcess = subprocess.run(
command, cwd=str(project_path), env=local_scoped_env, capture_output=True, text=True
)
log.debug(f'result: {result}')
ok = True if result.returncode == 0 else False
output = {'stdout': f'{result.stdout}', 'stderr': f'{result.stderr}'}
return_val = (ok, output)
log.debug(f'return_val: {return_val}')
return return_val

0 comments on commit 3476dca

Please sign in to comment.