Skip to content

Add additional debugging tools #41

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

Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ else()
endif()

# A single command to compile and run the tests
add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -u -m pytest ${PYBIND11_PYTEST_FILES}
DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL})

if(PYBIND11_TEST_OVERRIDE)
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import pytest
import textwrap
import difflib
import functools
import re
import sys
import trace
import contextlib
import platform
import gc
Expand Down Expand Up @@ -190,6 +192,25 @@ def gc_collect():
gc.collect()


def traced(f):
"""Decorator to trace the output of a given function (or test case).

Example:

@pytest.traced
def test_something():
print("Hello")

"""
tracer = trace.Trace(trace=1, count=0, ignoredirs=["/usr", sys.prefix])

@functools.wraps(f)
def wrapped(*args, **kwargs):
return tracer.runfunc(f, *args, **kwargs)

return wrapped


def pytest_configure():
"""Add import suppression and test requirements to `pytest` namespace"""
try:
Expand Down Expand Up @@ -218,6 +239,7 @@ def pytest_configure():
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
reason="unsupported on Python 2.x")
pytest.gc_collect = gc_collect
pytest.traced = traced


def _test_import_pybind11():
Expand Down
13 changes: 9 additions & 4 deletions tools/check-style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
#
# Invoke as: tools/check-style.sh
#
set -euo pipefail

# TODO(eacousineau): Is there a way to have grep return 0 if there's no
# matches, but report an error on other things (like syntax)?

check_style_errors=0
IFS=$'\n'

found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always || :)"
if [ -n "$found" ]; then
# The mt=41 sets a red background for matched tabs:
echo -e '\033[31;01mError: found tab characters in the following files:\033[0m'
Expand All @@ -27,22 +31,22 @@ if [ -n "$found" ]; then
fi


found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )"
found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always || : )"
if [ -n "$found" ]; then
echo -e '\033[31;01mError: found CRLF characters in the following files:\033[0m'
check_style_errors=1
echo "$found" | sed -e 's/^/ /'
fi

found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always || :)"
if [ -n "$found" ]; then
# The mt=41 sets a red background for matched trailing spaces
echo -e '\033[31;01mError: found trailing spaces in the following files:\033[0m'
check_style_errors=1
echo "$found" | sed -e 's/^/ /'
fi

found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)"
found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always || :)"
if [ -n "$found" ]; then
echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
check_style_errors=1
Expand All @@ -67,4 +71,5 @@ if [ -n "$found" ]; then
echo "$found"
fi

echo "Success - no errors found!"
exit $check_style_errors