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

fix(pytest+unittest): add safety check for Windows paths #7627

Merged
merged 18 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions ddtrace/contrib/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def pytest_runtest_protocol(item, nextitem):
_CIVisibility.set_codeowners_of(item.location[0], span=span)
if hasattr(item, "_obj"):
test_method_object = item._obj
_add_start_end_source_file_path_data_to_span(span, test_method_object, test_name)
_add_start_end_source_file_path_data_to_span(span, test_method_object, test_name, item.config.rootdir)

# We preemptively set FAIL as a status, because if pytest_runtest_makereport is not called
# (where the actual test status is set), it means there was a pytest error
Expand Down Expand Up @@ -806,7 +806,14 @@ def pytest_ddtrace_get_item_suite_name(item):
if test_module_path:
if not pytest_module_item.nodeid.startswith(test_module_path):
log.warning("Suite path is not under module path: '%s' '%s'", pytest_module_item.nodeid, test_module_path)
suite_path = os.path.relpath(pytest_module_item.nodeid, start=test_module_path)
try:
suite_path = os.path.relpath(pytest_module_item.nodeid, start=test_module_path)
except ValueError:
log.debug(
"Tried to collect suite path but it is using different drive paths on Windows, "
"using absolute path instead",
)
return os.path.abspath(pytest_module_item.nodeid)
return suite_path
return pytest_module_item.nodeid

Expand Down
34 changes: 30 additions & 4 deletions ddtrace/contrib/unittest/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,22 @@ def _extract_test_file_name(item) -> str:

def _extract_module_file_path(item) -> str:
if _is_test(item):
return os.path.relpath(inspect.getfile(item.__class__))
try:
test_module_object = inspect.getfile(item.__class__)
except TypeError:
log.debug(
"Tried to collect module file path but it is a built-in Python function",
)
return ""
try:
module_file_path = os.path.relpath(test_module_object, start=os.getcwd())
return module_file_path
except ValueError:
log.debug(
"Tried to collect module file path but it is using different drive paths on Windows, "
"using absolute path instead"
)
return os.path.abspath(test_module_object)
brettlangdon marked this conversation as resolved.
Show resolved Hide resolved

return ""

Expand Down Expand Up @@ -537,14 +552,25 @@ def add_xpass_test_wrapper(func, instance, args: tuple, kwargs: dict):
def _mark_test_as_unskippable(obj):
test_name = obj.__name__
test_suite_name = str(obj).split(".")[0].split()[1]
test_module_path = os.path.relpath(obj.__code__.co_filename)
try:
test_module_path = os.path.relpath(obj.__code__.co_filename, start=os.getcwd())
except ValueError:
log.debug(
"Tried to collect unskippable decorator but it is using different drive paths on Windows, "
"using absolute path instead"
)
test_module_path = os.path.abspath(obj.__code__.co_filename)
test_module_suite_name = _generate_module_suite_test_path(test_module_path, test_suite_name, test_name)
_CIVisibility._unittest_data["unskippable_tests"].add(test_module_suite_name)
return obj


def _using_unskippable_decorator(args, kwargs):
return args[0] is False and _extract_skip_if_reason(args, kwargs) == ITR_UNSKIPPABLE_REASON


def skip_if_decorator(func, instance, args: tuple, kwargs: dict):
if args[0] is False and _extract_skip_if_reason(args, kwargs) == ITR_UNSKIPPABLE_REASON:
if _using_unskippable_decorator(args, kwargs):
return _mark_test_as_unskippable
return func(*args, **kwargs)

Expand Down Expand Up @@ -789,7 +815,7 @@ def _start_test_span(instance, test_suite_span: ddtrace.Span) -> ddtrace.Span:

_CIVisibility.set_codeowners_of(_extract_test_file_name(instance), span=span)

_add_start_end_source_file_path_data_to_span(span, test_method_object, test_name)
_add_start_end_source_file_path_data_to_span(span, test_method_object, test_name, os.getcwd())

_store_test_span(instance, span)
return span
Expand Down
25 changes: 19 additions & 6 deletions ddtrace/internal/ci_visibility/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
log = get_logger(__name__)


def get_source_file_path_for_test_method(test_method_object) -> typing.Union[str, None]:
def get_source_file_path_for_test_method(test_method_object, repo_directory: str) -> typing.Union[str, None]:
try:
source_file_path = os.path.relpath(inspect.getfile(test_method_object))
file_object = inspect.getfile(test_method_object)
except TypeError:
return None
return ""
try:
source_file_path = os.path.relpath(file_object, start=repo_directory)
except ValueError:
log.debug(
"Tried to collect source file path but it is using different drive paths on Windows, "
"using absolute path instead",
)
return os.path.abspath(file_object)
return source_file_path


Expand All @@ -30,16 +38,21 @@ def get_source_lines_for_test_method(
return start_line, end_line


def _add_start_end_source_file_path_data_to_span(span: ddtrace.Span, test_method_object, test_name: str):
def _add_start_end_source_file_path_data_to_span(
span: ddtrace.Span, test_method_object, test_name: str, repo_directory: str
):
if not test_method_object:
log.debug(
"Tried to collect source start/end lines for test method %s but test method object could not be found",
test_name,
)
return
source_file_path = get_source_file_path_for_test_method(test_method_object)
source_file_path = get_source_file_path_for_test_method(test_method_object, repo_directory)
if not source_file_path:
log.debug("Tried to collect file path for test %s but it is a built-in Python function", test_name)
log.debug(
"Tried to collect file path for test %s but it is a built-in Python function",
test_name,
)
return
start_line, end_line = get_source_lines_for_test_method(test_method_object)
if not start_line or not end_line:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
CI Visibility: Fixes an issue where a ``ValueError`` was raised when using different path drives on Windows