Skip to content

Commit caf24ce

Browse files
committed
tests: add test suite barebones
Signed-off-by: Filipe Laíns <[email protected]>
1 parent bd45ba3 commit caf24ce

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build.ninja
44
__pycache__
55
docs/_build
66
docs/out
7+
.nox/

config/families/testsuite.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
has-linker = false
2+
c_flags = [
3+
'-pipe',
4+
'-fno-plt',
5+
'-fPIC',
6+
'--coverage',
7+
]
8+
ld_flags = [
9+
'-Wl,--sort-common,--as-needed,-z,relro,-z,now',
10+
'--coverage',
11+
]
12+
source = []
13+
14+
[release]
15+
c_flags = [
16+
'-O2',
17+
]
18+
19+
[debug]
20+
c_flags = [
21+
'-g',
22+
'-fvar-tracking-assignments',
23+
]
24+
ld_flags = [
25+
'-g',
26+
]

config/targets/testsuite.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
family = 'testsuite'
2+
is-shared-library = true
3+
out-name = 'libtestsuite'

noxfile.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-License-Identifier: MIT
2+
3+
import glob
4+
import os
5+
import os.path
6+
import pickle
7+
import subprocess
8+
9+
import nox
10+
11+
12+
nox.options.sessions = ['test']
13+
nox.options.reuse_existing_virtualenvs = True
14+
15+
16+
def install_dependencies(session, dependencies):
17+
unmet = pickle.loads(
18+
subprocess.check_output([
19+
'python',
20+
os.path.join('tests', 'unmet-dependencies.py'),
21+
*dependencies,
22+
], env=session.env, text=False)
23+
)
24+
25+
if unmet:
26+
session.install(*unmet)
27+
else:
28+
print('no dependencies to install')
29+
30+
31+
@nox.session()
32+
def test(session):
33+
htmlcov_output = os.path.join(session.virtualenv.location, 'htmlcov')
34+
xmlcov_output = os.path.join(session.virtualenv.location, 'coverage.xml')
35+
36+
if not os.path.isdir(htmlcov_output):
37+
os.mkdir(htmlcov_output)
38+
39+
install_dependencies(session, {
40+
# build system
41+
'ninja_syntax',
42+
'toml',
43+
# tests
44+
'pytest',
45+
# coverage
46+
'gcovr',
47+
})
48+
49+
# cleanup .gcda (GCC coverage) files
50+
for file in glob.glob(os.path.join('**', '*.gcda'), recursive=True):
51+
os.remove(file)
52+
53+
# build testsuite
54+
session.run('python', 'configure.py', 'testsuite')
55+
session.run('ninja', external=True)
56+
57+
# run tests
58+
session.run(
59+
'python', '-m', 'pytest',
60+
'--showlocals', '-ra', '--durations=10', '--durations-min=1.0',
61+
'tests/', *session.posargs,
62+
)
63+
64+
# generate C coverage reports
65+
session.run(
66+
'gcovr', '-r', '.', '-b',
67+
'--xml', xmlcov_output,
68+
'--html-details', os.path.join(htmlcov_output, 'index.html'),
69+
)

src/targets/testsuite/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int test()
9+
{
10+
return 10;
11+
}

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: MIT
2+
# SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
3+
4+
import ctypes
5+
import os.path
6+
7+
import pytest
8+
9+
10+
testsuite_out = os.path.abspath(os.path.join(
11+
__file__, '..', '..', 'build', 'testsuite', 'out',
12+
))
13+
14+
15+
@pytest.fixture()
16+
def testlib():
17+
return ctypes.cdll.LoadLibrary(os.path.join(testsuite_out, 'testsuite.so'))

tests/test_protocol.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: MIT
2+
# SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
3+
4+
5+
def test_something(testlib):
6+
assert testlib.test() == 10

tests/unmet-dependencies.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: MIT
2+
# SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
3+
4+
import importlib.metadata
5+
import pickle
6+
import sys
7+
8+
9+
unmet = set()
10+
11+
for dep in sys.argv[1:]:
12+
try:
13+
importlib.metadata.distribution(dep)
14+
except importlib.metadata.PackageNotFoundError:
15+
unmet.add(dep)
16+
17+
sys.stdout.buffer.write(pickle.dumps(unmet))

0 commit comments

Comments
 (0)