Skip to content

Commit 4fd5b13

Browse files
committed
Convert existing test.bsh into a regression test
The existing test.bash script has been converted to a regression test that: - Verifies that in default mode (GCOV) we calcualte the correct coverage for a simple c binary - Verifies that in lcov mode we correctly parse the .info file for a simple c binary Additional tests can be created by copying the "test-src/simple" directory and: - Updating the source file & Makefile with the new test code - Updating the expected.json with the expected coverage. NOTE: If gcov and lcov provide different results, this can be configured in the config.sh by specifying separate expected.json files - [optional] Providing a config.sh file in the directory to configure optional flags. (See valid options listed at the top of test-utils/testDir.sh) - Adding the directory to test.bash
1 parent ff7af7e commit 4fd5b13

File tree

8 files changed

+451
-26
lines changed

8 files changed

+451
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pip-log.txt
2525
.coverage
2626
.tox
2727
nosetests.xml
28+
*_test_output
29+
*.gcov
30+
2831

2932
# Translations
3033
*.mo

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ python:
77
- "3.4"
88
- "3.5"
99

10+
addons:
11+
apt:
12+
packages:
13+
- lcov
14+
1015
install:
1116
- python setup.py --quiet install
1217

test-src/simple/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
foo: foo.c
2+
gcc --coverage -o foo foo.c
3+
4+
clean:
5+
rm -f foo *.gcno *.gcda output *.gcov output.json
6+
7+
test: foo
8+
./foo

test-src/simple/expected.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"source_files": [{
3+
"name": "foo.c",
4+
"coverage": [1, 1, 1, 0, null, null, null, null, 0, null, 1, 0, null, null, 1, null],
5+
"source_digest": "500a03d2484de8cc2b9ed06615d7f02a"}]
6+
}

test-src/simple/foo.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
int main() {
2+
int a = 1;
3+
if(a == 2) {
4+
a = 3;
5+
/* LCOV_EXCL_START */
6+
a = 4;
7+
a = 5;
8+
/* LCOV_EXCL_STOP */
9+
a = 6;
10+
}
11+
if(a == 7) {
12+
a = 8;
13+
a = 9; /* LCOV_EXCL_LINE */
14+
}
15+
return 0;
16+
}

test-utils/check_output.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/python
2+
3+
from __future__ import absolute_import
4+
from __future__ import print_function
5+
6+
import json
7+
import sys
8+
9+
expectedJson="expected.json"
10+
if sys.argv[1] == "-e":
11+
expectedJson = sys.argv[2]
12+
13+
14+
expected=json.loads(open(expectedJson).read())
15+
output=json.loads(open("output.json").read())
16+
17+
if 'source_files' not in expected:
18+
print ("expected.json does not contain a 'source_files' array")
19+
print(expected)
20+
exit (1)
21+
22+
if 'source_files' not in output:
23+
print ("output.json does not contain a 'source_files' array")
24+
print(output)
25+
exit (1)
26+
27+
expected_files={}
28+
output_files={}
29+
30+
for f in expected['source_files']:
31+
expected_files[f['name']] = f
32+
for f in output['source_files']:
33+
output_files[f['name']] = f
34+
35+
#
36+
# Check for missing files
37+
#
38+
for f in expected_files:
39+
if f not in output_files:
40+
print ("File '%s' is missing from output.json" %f)
41+
exit(1)
42+
#
43+
# Check for rogue files
44+
#
45+
for f in output_files:
46+
if f not in expected_files:
47+
print ("File '%s' is reported in output.json, but was not expected!" %f)
48+
exit(1)
49+
50+
#
51+
# Finally check each file has the expected output...
52+
#
53+
for f in expected_files:
54+
exp_file = expected_files[f]
55+
out_file = output_files[f]
56+
57+
exp_lines=exp_file['coverage']
58+
out_lines=out_file['coverage']
59+
if len(exp_lines) != len(out_lines):
60+
print ("File '%s': expected %d lines, but actually has %s" %(f, len(exp_lines), len(out_lines)))
61+
exit(1)
62+
63+
for i in range(len(out_lines)):
64+
if exp_lines[i] != out_lines[i]:
65+
print ("File '%s':%d, expected %s hits, but actually has %s" %(f, i+1, exp_lines[i], out_lines[i]))
66+
exit(1)
67+
68+
69+

0 commit comments

Comments
 (0)