Skip to content

Commit

Permalink
Update run-tests script
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Apr 27, 2024
1 parent b376f43 commit f5ef427
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions run_tests.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
import os
from os.path import dirname, join, abspath
import sqlite3
import sys
Expand All @@ -11,18 +12,17 @@ def is_cibuildwheel():
return 'CIBUILDWHEEL' in os.environ


def temp_rename_kernprof(repo_dir):
"""
Hacky workaround so kernprof.py doesn't get covered twice (installed and local).
This needed to combine the .coverage files, since file paths need to be unique.
"""
original_path = repo_dir + '/kernprof.py'
tmp_path = original_path + '.tmp'
if os.path.isfile(original_path):
os.rename(original_path, tmp_path)
elif os.path.isfile(tmp_path):
os.rename(tmp_path, original_path)
# def temp_rename_kernprof(repo_dir):
# """
# Hacky workaround so kernprof.py doesn't get covered twice (installed and local).
# This needed to combine the .coverage files, since file paths need to be unique.
# """
# original_path = repo_dir + '/kernprof.py'
# tmp_path = original_path + '.tmp'
# if os.path.isfile(original_path):
# os.rename(original_path, tmp_path)
# elif os.path.isfile(tmp_path):
# os.rename(tmp_path, original_path)


def replace_docker_path(path, runner_project_dir):
Expand Down Expand Up @@ -76,13 +76,13 @@ def copy_coverage_cibuildwheel_docker(runner_project_dir):
os.rename(coverage_path, '/output/.coverage.{}'.format(env_hash))


if __name__ == '__main__':
cwd = os.getcwd()
repo_dir = abspath(dirname(__file__))
test_dir = join(repo_dir, 'tests')
print('cwd = {!r}'.format(cwd))

def main():
import pytest
import pathlib
cwd = os.getcwd()
repo_dir = pathlib.Path(__file__).parent.absolute()
test_dir = repo_dir / 'tests'
print('[run_tests] cwd = {!r}'.format(cwd))

# Prefer testing the installed version, but fallback to testing the
# development version.
Expand All @@ -102,46 +102,51 @@ def copy_coverage_cibuildwheel_docker(runner_project_dir):
if modpath is not None:
# If it does, then import it. This should cause the installed version
# to be used on further imports even if the repo_dir is in the path.
print(f'Using installed version of {package_name}')
print(f'[run_tests] Using installed version of {package_name}')
module = ub.import_module_from_path(modpath, index=0)
print('Installed module = {!r}'.format(module))
print(f'[run_tests] Installed module = {module!r}')
else:
print(f'No installed version of {package_name} found')
print(f'[run_tests] No installed version of {package_name} found')

try:
# print('Changing dirs to test_dir={!r}'.format(test_dir))
# os.chdir(test_dir)
print('[run_tests] Changing dirs to test_dir={!r}'.format(test_dir))
os.chdir(test_dir)

import pathlib
modpath_contents = list(pathlib.Path(modpath).glob('*'))
print(f'modpath_contents = {ub.urepr(modpath_contents, nl=1)}')
testdir_contents = list(pathlib.Path(test_dir).glob('*'))
print(f'testdir_contents = {ub.urepr(testdir_contents, nl=1)}')

modpath_contents = list(pathlib.Path(modpath).glob('*'))
pyproject_fpath = join(repo_dir, 'pyproject.toml')
print(f'[run_tests] pyproject_fpath = {pyproject_fpath}')
print(f'[run_tests] modpath={modpath}')
print(f'[run_tests] test_dir={test_dir}')
print(f'[run_tests] modpath_contents = {ub.urepr(modpath_contents, nl=1)}')
print(f'[run_tests] testdir_contents = {ub.urepr(testdir_contents, nl=1)}')

pytest_args = [
'--cov-config', pyproject_fpath,
'--cov-config', os.fspath(pyproject_fpath),
'--cov-report', 'html',
'--cov-report', 'term',
'--cov-report', 'xml',
'--cov=' + package_name,
modpath, test_dir
os.fspath(modpath), os.fspath(test_dir)
]
if is_cibuildwheel():
pytest_args.append('--cov-append')

pytest_args = pytest_args + sys.argv[1:]
print(f'Exec pytest with args={pytest_args}')
ret = pytest.main(pytest_args)
print(f'pytest returned ret={ret}')
print(f'[run_tests] Exec pytest with args={pytest_args}')
retcode = pytest.main(pytest_args)
print(f'[run_tests] pytest returned ret={retcode}')
except Exception as ex:
print(f'pytest exception: {ex}')
ret = 1
print(f'[run_tests] pytest exception: {ex}')
retcode = 1
finally:
# os.chdir(cwd)
os.chdir(cwd)
if is_cibuildwheel():
# for CIBW under linux
copy_coverage_cibuildwheel_docker(f'/home/runner/work/{package_name}/{package_name}')
print('Restoring cwd = {!r}'.format(cwd))
sys.exit(ret)
print('[run_tests] Restoring cwd = {!r}'.format(cwd))
return retcode


if __name__ == '__main__':
sys.exit(main())

0 comments on commit f5ef427

Please sign in to comment.