Skip to content

Commit 4c6ab71

Browse files
author
neok-m4700
committed
rework testing under pytest & tox
1 parent 8e3a57b commit 4c6ab71

8 files changed

Lines changed: 52 additions & 17 deletions

File tree

.github/workflows/setup_test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ jobs:
1616
uses: actions/setup-python@v2
1717
with:
1818
python-version: ${{ matrix.python-version }}
19-
- name: Install tox
19+
- name: Install tox and pytest
2020
run: |
2121
python -m pip install --upgrade pip
22-
pip install tox
22+
pip install tox pytest
2323
- name: Test with tox
2424
run: |
2525
tox
26+
- name: Test with pytest
27+
run: |
28+
pytest

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ include setup_cptrace.py
2222

2323
# Tests
2424
include runtests.py
25-
include test_doc.py
25+
include check_doc.py
2626
include tests/test_*.py
2727
include tests/crash/*.c
2828
include tests/crash/BSDmakefile

test_doc.py renamed to check_doc.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from doctest import testfile, ELLIPSIS, testmod
33
from sys import exit, path as sys_path
44
from os.path import dirname
5+
import importlib
56

67

78
def testDoc(filename, name=None):
@@ -13,17 +14,9 @@ def testDoc(filename, name=None):
1314
print("--- %s: End of tests" % filename)
1415

1516

16-
def importModule(name):
17-
mod = __import__(name)
18-
components = name.split('.')
19-
for comp in components[1:]:
20-
mod = getattr(mod, comp)
21-
return mod
22-
23-
2417
def testModule(name):
2518
print("--- Test module %s" % name)
26-
module = importModule(name)
19+
module = importlib.import_module(name)
2720
failure, nb_test = testmod(module)
2821
if failure:
2922
exit(1)

doc/install.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Run tests manually
8484
Type::
8585

8686
python3 runtests.py
87-
python3 test_doc.py
87+
python3 check_doc.py
8888

8989
It's also possible to run a specific test::
9090

tests/crash/run.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
trace() {
4+
(
5+
set +e
6+
python ../../strace.py -e execve $1; ec=$?
7+
if [ $ec -gt 0 ]; then
8+
exit $(($ec - 128 - $2))
9+
fi
10+
)
11+
}
12+
13+
if command -v gcc && command -v make && command -v kill; then
14+
make || exit
15+
16+
# trace ./invalid_read $(kill -l SEGV) |& tee /dev/stderr | grep -q 'Invalid read from'
17+
trace ./invalid_read $(kill -l SEGV) # 2>&1 | grep -q 'Invalid read from'
18+
trace ./invalid_write $(kill -l SEGV) # 2>&1 | grep -q 'Invalid write to'
19+
trace ./stack_overflow $(kill -l SEGV) # 2>&1 | grep -q 'STACK OVERFLOW!'
20+
trace ./call_null $(kill -l SEGV)
21+
trace ./abort $(kill -l ABRT)
22+
trace ./div_zero $(kill -l FPE)
23+
trace ./socket_ipv4_tcp
24+
trace ./pthread
25+
trace ./execve
26+
trace ./fork
27+
fi

tests/crash/stack_overflow.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
char toto()
22
{
3-
char buffer[4096];
4-
buffer[0] = 0;
3+
volatile unsigned char buffer[4096];
4+
buffer[0] = 1;
5+
buffer[4095] = 0;
56
toto();
67
return buffer[0] + buffer[sizeof(buffer)-1];
78
}

tests/test_strace.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import tempfile
77
import unittest
88
import signal
9+
import shutil
10+
import platform
911

1012
STRACE = os.path.normpath(
1113
os.path.join(os.path.dirname(__file__), '..', 'strace.py'))
1214

1315
AARCH64 = (getattr(os.uname(), 'machine', None) == 'aarch64')
1416

17+
UNTESTED = platform.system() not in ('Linux', 'FreeBSD')
18+
1519

1620
class TestStrace(unittest.TestCase):
1721
def strace(self, *args):
@@ -106,6 +110,13 @@ def test_socket(self):
106110
"import socket; socket.socket(socket.AF_INET,socket.SOCK_STREAM).close()",
107111
br'^socket\(AF_INET, SOCK_STREAM(\|SOCK_CLOEXEC)?')
108112

113+
@unittest.skipIf(UNTESTED, 'Untested system/OS')
114+
def test_crash(self):
115+
dn = os.path.join(os.path.dirname(__file__), 'crash')
116+
shell = shutil.which('bash')
117+
if shell:
118+
self.assertEqual(subprocess.call([shell, '-e', 'run.sh'], cwd=dn), 0)
119+
109120

110121
if __name__ == "__main__":
111122
unittest.main()

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ envlist = py3, pep8
44
[testenv]
55
basepython = python3
66
commands=
7-
python test_doc.py
7+
python check_doc.py
88
python runtests.py -v
99

1010
[testenv:py3]
@@ -13,7 +13,7 @@ basepython = python3
1313
[testenv:pep8]
1414
deps = flake8
1515
commands =
16-
flake8 ptrace/ tests/ gdb.py runtests.py setup_cptrace.py setup.py strace.py SYSCALL_PROTOTYPES.codegen.py test_doc.py
16+
flake8 ptrace/ tests/ gdb.py runtests.py setup_cptrace.py setup.py strace.py SYSCALL_PROTOTYPES.codegen.py check_doc.py
1717

1818
[flake8]
1919
# E501 line too long (88 > 79 characters)

0 commit comments

Comments
 (0)