Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nx-cugraph: use coverage to ensure all algorithms were run #4108

Merged
merged 18 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ci/test_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ pushd python/nx-cugraph
_coverage=$(coverage report|grep "^TOTAL")
echo "nx-cugraph coverage from networkx tests: $_coverage"
echo $_coverage | awk '{ if ($NF == "0.0%") exit 1 }'
# Ensure all algorithms were called by comparing covered lines to function lines.
# Run our tests again (they're fast enough) to add their coverage, then create coverage.json
pytest --cov=nx_cugraph --cov-append --cov-report=
coverage json
python nx_cugraph/tests/ensure_algos_covered.py
popd

rapids-logger "pytest cugraph-service (single GPU)"
Expand Down
82 changes: 82 additions & 0 deletions python/nx-cugraph/nx_cugraph/tests/ensure_algos_covered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Ensure that all functions wrapped by @networkx_algorithm were called.

This file is run by CI and should not normally be run manually.
"""
import inspect
import json
from pathlib import Path

from nx_cugraph.interface import BackendInterface
from nx_cugraph.utils import networkx_algorithm

with Path("coverage.json").open() as f:
coverage = json.load(f)

filenames_to_executed_lines = {
filename: set(coverage_info["executed_lines"])
for filename, coverage_info in coverage["files"].items()
}


def unwrap(func):
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
return func


def get_func_filename(func):
return "nx_cugraph" + inspect.getfile(unwrap(func)).rsplit("nx_cugraph", 1)[-1]


def get_func_linenos(func):
lines, lineno = inspect.getsourcelines(unwrap(func))
for i, line in enumerate(lines, lineno):
if ":\n" in line:
return set(range(i + 1, lineno + len(lines)))
raise RuntimeError(f"Could not determine line numbers for function {func}")


def has_any_coverage(func):
return bool(
filenames_to_executed_lines[get_func_filename(func)] & get_func_linenos(func)
)


def main():
no_coverage = set()
for attr, func in vars(BackendInterface).items():
if not isinstance(func, networkx_algorithm):
continue
if not has_any_coverage(func):
no_coverage.add(attr)
if no_coverage:
msg = "The following algorithms have no coverage: " + ", ".join(
sorted(no_coverage)
)
# Create a border of "!"
msg = (
"\n\n"
+ "!" * (len(msg) + 6)
+ "\n!! "
+ msg
+ " !!\n"
+ "!" * (len(msg) + 6)
+ "\n"
)
raise AssertionError(msg)


if __name__ == "__main__":
main()
8 changes: 4 additions & 4 deletions python/nx-cugraph/run_nx_tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#
# Copyright (c) 2023, NVIDIA CORPORATION.
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
#
# NETWORKX_GRAPH_CONVERT=cugraph
# Used by networkx versions 3.0 and 3.1
Expand Down Expand Up @@ -30,7 +30,7 @@ NETWORKX_TEST_BACKEND=cugraph \
NETWORKX_FALLBACK_TO_NX=True \
pytest \
--pyargs networkx \
--cov=nx_cugraph.algorithms \
--cov-report term-missing \
--no-cov-on-fail \
--cov=nx_cugraph \
--cov-report= \
"$@"
coverage report --include="nx_cugraph/algorithms/*" --omit=__init__.py --show-missing
Loading