From b6e89621dd4ced8fc568eb83ea614f4be51ade84 Mon Sep 17 00:00:00 2001 From: Mark Jacobson <52427991+marksparkza@users.noreply.github.com> Date: Wed, 26 Oct 2022 15:19:10 +0200 Subject: [PATCH] Ensure coverage reporting on unexecuted files Since `odp` is now a namespace package, coverage cannot discover and report on code that is not actually imported during a test run (see https://github.com/nedbat/coveragepy/issues/1024 for further discussion around this issue). This was giving us an inflated coverage score. --- odp/identity/__init__.py | 47 +++++++++++++++++++++------------------- test/.coveragerc | 16 ++++++++++++-- test/conftest.py | 22 +++++++++++++++++++ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/odp/identity/__init__.py b/odp/identity/__init__.py index 7d6b77c..e81e401 100644 --- a/odp/identity/__init__.py +++ b/odp/identity/__init__.py @@ -5,28 +5,31 @@ from odp.config import config from odp.lib.hydra_admin import HydraAdminClient -mail = Mail() +if config.ODP.ENV != 'testing': + # TODO: test odp.identity... -hydra_admin = HydraAdminClient( - server_url=config.HYDRA.ADMIN.URL, - verify_tls=config.ODP.ENV != 'development', - remember_login_for=config.ODP.IDENTITY.LOGIN_EXPIRY, -) + mail = Mail() -redis_cache = redis.Redis( - host=config.REDIS.HOST, - port=config.REDIS.PORT, - db=config.REDIS.DB, - decode_responses=True, -) + hydra_admin = HydraAdminClient( + server_url=config.HYDRA.ADMIN.URL, + verify_tls=config.ODP.ENV != 'development', + remember_login_for=config.ODP.IDENTITY.LOGIN_EXPIRY, + ) -google_oauth2 = OAuth(cache=redis_cache) -google_oauth2.register( - name='google', - authorize_url=config.GOOGLE.AUTH_URI, - access_token_url=config.GOOGLE.TOKEN_URI, - server_metadata_url=config.GOOGLE.OPENID_URI, - client_id=config.GOOGLE.CLIENT_ID, - client_secret=config.GOOGLE.CLIENT_SECRET, - client_kwargs={'scope': ' '.join(config.GOOGLE.SCOPE)}, -) + redis_cache = redis.Redis( + host=config.REDIS.HOST, + port=config.REDIS.PORT, + db=config.REDIS.DB, + decode_responses=True, + ) + + google_oauth2 = OAuth(cache=redis_cache) + google_oauth2.register( + name='google', + authorize_url=config.GOOGLE.AUTH_URI, + access_token_url=config.GOOGLE.TOKEN_URI, + server_metadata_url=config.GOOGLE.OPENID_URI, + client_id=config.GOOGLE.CLIENT_ID, + client_secret=config.GOOGLE.CLIENT_SECRET, + client_kwargs={'scope': ' '.join(config.GOOGLE.SCOPE)}, + ) diff --git a/test/.coveragerc b/test/.coveragerc index e5d1f8d..712fead 100644 --- a/test/.coveragerc +++ b/test/.coveragerc @@ -1,7 +1,19 @@ [run] -source = odp +source = + # odp-core + odp.config + odp.const + odp.schema + odp.logfile + odp.version + # odp-server + odp.api + odp.catalog + odp.db + odp.identity + odp.lib + branch = True -omit = */odp/ui/* [report] exclude_lines = diff --git a/test/conftest.py b/test/conftest.py index ac88701..8483bab 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -7,6 +7,28 @@ from odp.config import config +# noinspection PyUnresolvedReferences +@pytest.fixture(scope='session', autouse=True) +def ensure_coverage(): + """Since the codebase now defines `odp` as a namespace package, + coverage cannot discover subpackages and modules that are not + imported during a test run, preventing reporting on unexecuted + files and hence inflating the coverage score. So, we import + everything we want covered here.""" + # odp-core + import odp.config + import odp.const + import odp.schema + import odp.logfile + import odp.version + # odp-server + import odp.api + import odp.catalog + import odp.db + import odp.identity + import odp.lib + + @pytest.fixture(scope='session', autouse=True) def database(): """An auto-use, run-once fixture that provides a clean