Skip to content

Commit cadbfe6

Browse files
hanneskaeuflervkresch
authored andcommitted
Hk improve tests (#360)
* Improve error message When forgetting to install the protobuf compiler, an error message is shown which had a missing space between two words. * Remove trailing whitespace * Read file in one go There is no reason to traverse the file line by line if all we care about is the last character. This also uses unittests subTest to show the file which fails the test. * Fix header comment * Use subTest to show all failing files When one file is failing, due to the fact that all tests contain for loops, the other failures are swallowed because python stops running the test on the first assertion. With subTest one can introduce a subcontext which allows showing all of the failed files as well a diagnostic as to which file failed. * Use enumeration index for line count Python provides mechanisms for that, no reason to manually track a count. * Remove unused imports, only glob once The globs where repeated a whole bunch of times which is both information duplication as well as unneccessary work. * Remove needless conditionals in front of assert There is no reason to guard the assertions in the conditional. * Switch build to python 3 Python2 is end of life in just a few days and should not be recommended anymore. * Improve assertion by using regex match. The error message is quite good when using the provided regex matcher. * Only deploy on master * Remove trailing whitespace
1 parent 66501d4 commit cadbfe6

13 files changed

+133
-210
lines changed

.travis.yml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ install:
3838
PROTOBUF_URL="https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-cpp-3.2.0.tar.gz"
3939
if [ ! -f ${DEPS_DIR}/protobuf/install/bin/protoc ] ; then mkdir -p protobuf ; travis_retry wget --no-check-certificate --quiet -O - ${PROTOBUF_URL} | tar --strip-components=1 -xz -C protobuf ; cd protobuf ; ./configure --prefix=${DEPS_DIR}/protobuf/install ; make ; make install ; fi
4040
export PATH=${DEPS_DIR}/protobuf/install/bin:${PATH}
41-
41+
4242
# Install proto2cpp for doxygen
4343
- |
4444
PROTO2CPP_URL="https://github.com/OpenSimulationInterface/proto2cpp.git"
@@ -50,7 +50,7 @@ env:
5050
- GH_REPO_ORG=`echo $TRAVIS_REPO_SLUG | cut -d "/" -f 1`
5151
- GH_REPO_NAME=`echo $TRAVIS_REPO_SLUG | cut -d "/" -f 2`
5252
- GH_REPO_REF="github.com/$GH_REPO_ORG/$GH_REPO_NAME.git"
53-
53+
5454
# Addons necessary for documentation.
5555
addons:
5656
apt:
@@ -60,10 +60,11 @@ addons:
6060
- doxygen-latex
6161
- doxygen-gui
6262
- graphviz
63+
- python3-setuptools
6364

6465

6566
jobs:
66-
include:
67+
include:
6768
# Run the build script and generate documentation.
6869
- stage: test OSI protobuf
6970

@@ -75,8 +76,8 @@ jobs:
7576
- cmake -D CMAKE_PREFIX_PATH:PATH=${DEPS_DIR}/protobuf/install -D CMAKE_INSTALL_PREFIX:PATH=${TESTINST_DIR} ..
7677
- cmake --build .
7778
- cd ..
78-
- python setup.py build
79-
- python setup.py sdist
79+
- python3 setup.py build
80+
- python3 setup.py sdist
8081
- cd build
8182
- cmake --build . --target install
8283
- cd ..
@@ -89,15 +90,15 @@ jobs:
8990
- echo "GENERATE_TREEVIEW = YES" >> Doxyfile
9091
- doxygen
9192
- cd ..
92-
- python -m unittest discover tests
93+
- python3 -m unittest discover tests
9394
- mv VERSION.SAVED VERSION
9495
- sh convert-to-proto3.sh
9596
- mkdir -p build3
9697
- cd build3
9798
- cmake -D CMAKE_PREFIX_PATH:PATH=${DEPS_DIR}/protobuf/install ..
9899
- cmake --build .
99100
- cd ..
100-
101+
101102
- script:
102103
- cd "${TRAVIS_BUILD_DIR}"
103104
- bash convert-to-proto3.sh
@@ -107,8 +108,8 @@ jobs:
107108
- cmake -D CMAKE_PREFIX_PATH:PATH=${DEPS_DIR}/protobuf/install -D CMAKE_INSTALL_PREFIX:PATH=${TESTINST_DIR} ..
108109
- cmake --build .
109110
- cd ..
110-
- python setup.py build
111-
- python setup.py sdist
111+
- python3 setup.py build
112+
- python3 setup.py sdist
112113
- cd build
113114
- cmake --build . --target install
114115
- cd ..
@@ -121,7 +122,7 @@ jobs:
121122
- echo "GENERATE_TREEVIEW = YES" >> Doxyfile
122123
- doxygen
123124
- cd ..
124-
- python -m unittest discover tests
125+
- python3 -m unittest discover tests
125126
- mv VERSION.SAVED VERSION
126127
- sh convert-to-proto3.sh
127128
- mkdir -p build3
@@ -139,8 +140,8 @@ jobs:
139140
- cmake -D CMAKE_PREFIX_PATH:PATH=${DEPS_DIR}/protobuf/install -D CMAKE_INSTALL_PREFIX:PATH=${TESTINST_DIR} ..
140141
- cmake --build .
141142
- cd ..
142-
- python setup.py build
143-
- python setup.py sdist
143+
- python3 setup.py build
144+
- python3 setup.py sdist
144145
- cd build
145146
- cmake --build . --target install
146147
- cd ..
@@ -153,7 +154,7 @@ jobs:
153154
- echo "GENERATE_TREEVIEW = YES" >> Doxyfile
154155
- doxygen
155156
- cd ..
156-
- python -m unittest discover tests
157+
- python3 -m unittest discover tests
157158
- mv VERSION.SAVED VERSION
158159
- sh convert-to-proto3.sh
159160
- mkdir -p build3
@@ -163,11 +164,11 @@ jobs:
163164
- cd ..
164165

165166
# Deploy the documentation on github (only for master branch).
166-
deploy:
167+
deploy:
167168
provider: pages
168169
skip_cleanup: true
169170
local_dir: doc/html
170171
keep-history: true
171172
github_token: ${GH_REPO_TOKEN}
172-
on:
173-
branch: master
173+
on:
174+
branch: master

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ $ sudo apt-get install cmake
8585
```
8686
Install `pip3` and missing python packages:
8787
```bash
88-
$ sudo apt-get install python3-pip python-setuptools
88+
$ sudo apt-get install python3-pip python3-setuptools
8989
```
9090
Install `protobuf` 3.0.0:
9191
```bash
@@ -109,10 +109,10 @@ Local:
109109
```bash
110110
$ git clone https://github.com/OpenSimulationInterface/open-simulation-interface.git
111111
$ cd open-simulation-interface
112-
$ sudo pip3 install virtualenv
112+
$ sudo pip3 install virtualenv
113113
$ virtualenv -p python3 venv
114114
$ source venv/bin/activate
115-
$ pip install .
115+
$ python3 -m pip install .
116116
```
117117

118118
Global:

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! python
1+
#!/usr/local/env python3
22
# -*- coding: UTF-8 -*-
33

44
import os
@@ -39,8 +39,8 @@ def find_protoc():
3939
if protoc is None:
4040
sys.stderr.write(
4141
'protoc not found. Is protobuf-compiler installed? \n'
42-
'Alternatively, you can point the PROTOC environment variable'
43-
'at a local version.')
42+
'Alternatively, you can point the PROTOC environment variable '
43+
'to a local version.')
4444
sys.exit(1)
4545
return protoc
4646

tests/test_comment_type.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import sys
22
import unicodedata
33
import re
4-
from glob import *
4+
import glob
55
import unittest
66

7+
PROTO_FILES = glob.glob("*.proto")
78

89
class TestCommentType(unittest.TestCase):
9-
''' Test class for mandatory new line. '''
10+
''' Test class for mandatory comments. '''
1011

1112
def test_brief_necessity(self):
1213
''' Test the necessity of "brief" comment. '''
1314

14-
for file in glob("*.proto"):
15-
with open(file, "rt") as fin:
16-
i = 0
15+
for file in PROTO_FILES:
16+
with open(file, "rt") as fin, self.subTest(file=file):
1717
noMessage = 0
1818
noComment = 0
1919
hasBrief = False
2020
saveStatement = ""
2121

22-
for line in fin:
23-
i += 1
22+
for i, line in enumerate(fin, start=1):
2423

2524
# Divide statement and comment. Concatenate multi line statements.
2625

@@ -57,7 +56,7 @@ def test_brief_necessity(self):
5756
noComment += 1;
5857
if comment.find("\\brief") != -1:
5958
hasBrief = True
60-
59+
6160
elif len(saveStatement) == 0:
6261
if re.search(r"\bmessage\b", statement) is not None or re.search(r"\bextend\b",statement) is not None:
6362
self.assertTrue(hasBrief, file + " in line " + str(i - 1) + ": \\brief section in comment is missing for: '" + statement + "'")
@@ -73,18 +72,15 @@ def test_brief_necessity(self):
7372
def test_min_two_lines(self):
7473
''' Test to check if short comment is of minimum two lines. '''
7574

76-
for file in glob("*.proto"):
77-
with open(file, "rt") as fin:
78-
i = 0
75+
for file in PROTO_FILES:
76+
with open(file, "rt") as fin, self.subTest(file=file):
7977
isEnum = False
8078
noMessage = 0
8179
noComment = 0
8280
hasBrief = False
8381
saveStatement = ""
8482

85-
for line in fin:
86-
i += 1
87-
83+
for i, line in enumerate(fin, start=1):
8884
# Divide statement and comment. Concatenate multi line statements.
8985

9086
# Search for comment ("//").
@@ -129,19 +125,15 @@ def test_min_two_lines(self):
129125
def test_comment_existence(self):
130126
''' Test to check if every message, extend , statement or enum has a comment. '''
131127

132-
for file in glob("*.proto"):
133-
134-
with open(file, "rt") as fin:
135-
i = 0
128+
for file in PROTO_FILES:
129+
with open(file, "rt") as fin, self.subTest(file=file):
136130
isEnum = False
137131
noMessage = 0
138132
noComment = 0
139133
hasBrief = False
140134
saveStatement = ""
141135

142-
for line in fin:
143-
i += 1
144-
136+
for i, line in enumerate(fin, start=1):
145137
# Divide statement and comment. Concatenate multi line statements.
146138

147139
# Search for comment ("//").

tests/test_doxygen_output.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
1-
import sys
2-
import unicodedata
31
import re
4-
from glob import *
2+
import glob
53
import unittest
64

5+
DOC_FILES = glob.glob("doc/html/*.htm*")
76

87
class TestDoxygenOutput(unittest.TestCase):
98
""" Test class for the doxygen output. """
109

1110
def test_hash(self):
1211
''' Test case is checking if there are illegal hash chars in the documentation. -> doxygen link not found. '''
13-
for file in glob("doc/html/*.htm*"):
14-
with open(file, "rt") as fin:
15-
i = 0
16-
17-
for line in fin:
18-
i += 1
19-
matchHash = re.search(r"([\s>]|^)#\w(\S)*", line)
20-
21-
if matchHash is not None:
22-
self.assertIsNone(matchHash, file + " in line " + str(i) + ": not permitted hash found. Search for: '"+ line[matchHash.start():matchHash.end()])
12+
for file in DOC_FILES:
13+
with open(file, "rt") as fin, self.subTest(file=file):
14+
for i, line in enumerate(fin, start=1):
15+
self.assertNotRegex(line, r"([\s>]|^)#\w(\S)*", file + " in line " + str(i) + ": not permitted hash found.")
2316

2417

2518
def test_slash_triplet(self):
2619
''' Test case is checking if there are slash triplets in the documentation. -> doxygen didn't interpret something properly. '''
2720

28-
for file in glob("doc/html/*.htm*"):
29-
with open(file, "rt") as fin:
30-
i = 0
31-
32-
for line in fin:
33-
i += 1
34-
matchHash = re.search(r"([\s>]|^)///\s*",line)
35-
36-
if matchHash is not None:
37-
self.assertIsNone(matchHash, file + " in line " + str(i) + ": not permitted slash triplet found. Search for: '"+line[matchHash.start():matchHash.end()])
21+
for file in DOC_FILES:
22+
with open(file, "rt") as fin, self.subTest(file=file):
23+
for i, line in enumerate(fin, start=1):
24+
self.assertNotRegex(line, r"([\s>]|^)///\s*", file + " in line " + str(i) + ": not permitted slash triplet found.")
3825

3926

4027
def test_backslash_triplet(self):
4128
''' Test case is checking if there are backslash triplets in the documentation. -> doxygen didn't interpret something properly. '''
42-
for file in glob("doc/html/*.htm*"):
43-
with open(file, "rt") as fin:
44-
i = 0
45-
46-
for line in fin:
47-
i += 1
48-
matchHash = re.search(r"([\s>]|^)\\\\\\\s*",line)
49-
50-
if matchHash is not None:
51-
self.assertIsNone(matchHash, file + " in line " + str(i) + ": not permitted backslash triplet found. Search for: '"+line[matchHash.start():matchHash.end()])
29+
for file in DOC_FILES:
30+
with open(file, "rt") as fin, self.subTest(file=file):
31+
for i, line in enumerate(fin, start=1):
32+
self.assertNotRegex(line, r"([\s>]|^)\\\\\\\s*", file + " in line " + str(i) + ": not permitted backslash triplet found.")

tests/test_invalid_comment.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
import sys
2-
import unicodedata
3-
import re
4-
from glob import *
1+
import glob
52
import unittest
63

4+
PROTO_FILES = glob.glob("*.proto")
5+
76
class TestInvalidCommentType(unittest.TestCase):
87
"""Test class for invalid comment types"""
98

109
def test_triple_slash(self):
1110
''' Test to check if more than two forward slash('/') are present in comment section of proto file. '''
12-
for file in glob("*.proto"):
13-
with open(file, "rt") as fin:
14-
i = 0
15-
16-
for line in fin:
17-
i += 1
11+
for file in PROTO_FILES:
12+
with open(file, "rt") as fin, self.subTest(file=file):
13+
for i, line in enumerate(fin, start=1):
1814
self.assertEqual(line.find("///"), -1, file + " in line " + str(i) + ": not permitted use of '///' ")
1915

2016
def test_comments_invalid_syntax(self):
2117
''' Test to check if comments are given using invalid syntax '/*' or '*/' '''
22-
for file in glob("*.proto"):
23-
with open(file, "rt") as fin:
24-
i = 0
25-
26-
for line in fin:
27-
i += 1
18+
for file in PROTO_FILES:
19+
with open(file, "rt") as fin, self.subTest(file=file):
20+
for i, line in enumerate(fin, start=1):
2821
self.assertEqual(line.find("/*"), -1, file + " in line " + str(i) + ": not permitted use of '/*' ")
2922
self.assertEqual(line.find("*/"), -1, file + " in line " + str(i) + ": not permitted use of '*/' ")

0 commit comments

Comments
 (0)