Skip to content

Commit e460c13

Browse files
committed
For cpplint#79, improve clitests running with different VCS roots
1 parent dbffa6c commit e460c13

File tree

1 file changed

+99
-29
lines changed

1 file changed

+99
-29
lines changed

cpplint_clitest.py

Lines changed: 99 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import sys
3636
import subprocess
3737
import unittest
38+
import shutil
39+
import tempfile
3840

3941
BASE_CMD = sys.executable + ' ' + os.path.abspath('./cpplint.py ')
4042

@@ -66,8 +68,7 @@ def testHelp(self):
6668
self.assertEqual(b'', out)
6769
self.assertTrue(err.startswith(b'\nSyntax: cpplint'))
6870

69-
70-
class SignatureTests(unittest.TestCase):
71+
class TemporaryFolderClassSetup(object):
7172
"""
7273
Regression tests: The test starts a filetreewalker scanning for files name *.def
7374
Such files are expected to have as first line the argument
@@ -76,23 +77,34 @@ class SignatureTests(unittest.TestCase):
7677
lines at end).
7778
"""
7879

79-
def testChromiumSample(self):
80-
self.checkAllInFolder('./samples/chromium-sample', 1)
81-
82-
def testVlcSample(self):
83-
self.checkAllInFolder('./samples/vlc-sample', 1)
84-
85-
def testSillySample(self):
86-
self.checkAllInFolder('./samples/silly-sample', 2)
87-
88-
def testBoostSample(self):
89-
self.checkAllInFolder('./samples/boost-sample', 2)
90-
91-
def testProtobufSample(self):
92-
self.checkAllInFolder('./samples/protobuf-sample', 1)
93-
94-
def testCodeliteSample(self):
95-
self.checkAllInFolder('./samples/codelite-sample', 1)
80+
@classmethod
81+
def setUpClass(cls):
82+
"""setup tmp folder for testing with samples and custom additions by subclasses"""
83+
try:
84+
cls._root = tempfile.mkdtemp()
85+
shutil.copytree('samples', os.path.join(cls._root, 'samples'))
86+
cls.prepare_directory(cls._root)
87+
except Exception as e:
88+
try:
89+
cls.tearDownClass()
90+
except Exception as e2:
91+
pass
92+
raise
93+
94+
@classmethod
95+
def tearDownClass(cls):
96+
if (cls._root):
97+
# pass
98+
shutil.rmtree(cls._root)
99+
100+
@classmethod
101+
def prepare_directory(cls, root):
102+
"""Override in subclass to manipulate temporary samples root folder before tests"""
103+
pass
104+
105+
def get_extra_command_args(self, cwd):
106+
"""Override in subclass to add arguments to command"""
107+
return ''
96108

97109
def checkAllInFolder(self, foldername, expectedDefs):
98110
# uncomment to show complete diff
@@ -102,35 +114,93 @@ def checkAllInFolder(self, foldername, expectedDefs):
102114
for f in fnames:
103115
if f.endswith('.def'):
104116
count += 1
105-
self.checkDef(os.path.join(dirpath, f))
117+
self._checkDef(os.path.join(dirpath, f))
106118
self.assertEqual(count, expectedDefs)
107119

108-
def checkDef(self, path):
120+
def _checkDef(self, path):
109121
"""runs command and compares to expected output from def file"""
110122
# self.maxDiff = None # to see full diff
111123
with open(path, 'rb') as filehandle:
112124
datalines = filehandle.readlines()
113125
stdoutLines = int(datalines[2])
114-
self.runAndCheck(os.path.dirname(path),
126+
self._runAndCheck(os.path.dirname(path),
115127
datalines[0].decode('utf8').strip(),
116128
int(datalines[1]),
117129
[line.decode('utf8').strip() for line in datalines[3:3 + stdoutLines]],
118130
[line.decode('utf8').strip() for line in datalines[3 + stdoutLines:]])
119131

120-
def runAndCheck(self, cwd, args, expectedStatus, expectedOut, expectedErr):
121-
cmd = BASE_CMD + ' --repository ' + cwd + ' ' + args
132+
def _runAndCheck(self, rel_cwd, args, expectedStatus, expectedOut, expectedErr):
133+
cmd = BASE_CMD + self.get_extra_command_args(rel_cwd) + args
134+
cwd = os.path.join(self._root, rel_cwd)
122135
# command to reproduce, do not forget first two lines have special meaning
123-
print("\ncd " + cwd + ";" + cmd + " 2> <filename>")
136+
print("\ncd " + cwd + " && " + cmd + " 2> <filename>")
124137
(status, out, err) = RunShellCommand(cmd, cwd)
125138
try:
126-
self.assertEqual(expectedStatus, status)
139+
self.assertEqual(expectedStatus, status, 'bad command status %s' % status)
127140
self.assertEqual(expectedErr, err.decode('utf8').split('\n'))
128141
self.assertEqual(expectedOut, out.decode('utf8').split('\n'))
129142
except AssertionError as e:
130-
print('Failed check in ' + cwd)
131-
print('for command: ' + cmd)
143+
e.args += ('Failed check in %s for command: %s' % (cwd, cmd),)
132144
raise e
133145

134146

147+
class NoRepoSignatureTests(TemporaryFolderClassSetup, unittest.TestCase):
148+
"""runs in a temporary folder (under /tmp in linux) without any .git/.hg/.svn file"""
149+
150+
def get_extra_command_args(self, cwd):
151+
return (' --repository %s ' % self._root)
152+
153+
def testChromiumSample(self):
154+
self.checkAllInFolder('./samples/chromium-sample', 1)
155+
156+
def testVlcSample(self):
157+
self.checkAllInFolder('./samples/vlc-sample', 1)
158+
159+
def testSillySample(self):
160+
self.checkAllInFolder('./samples/silly-sample', 2)
161+
162+
def testBoostSample(self):
163+
self.checkAllInFolder('./samples/boost-sample', 2)
164+
165+
def testProtobufSample(self):
166+
self.checkAllInFolder('./samples/protobuf-sample', 1)
167+
168+
def testCodeliteSample(self):
169+
self.checkAllInFolder('./samples/codelite-sample', 1)
170+
171+
172+
class GitRepoSignatureTests(TemporaryFolderClassSetup, unittest.TestCase):
173+
"""runs in a temporary folder with .git file"""
174+
175+
@classmethod
176+
def prepare_directory(cls, root):
177+
with open(os.path.join(root, '.git'), 'a'):
178+
pass
179+
180+
def testCodeliteSample(self):
181+
self.checkAllInFolder('./samples/codelite-sample', 1)
182+
183+
class MercurialRepoSignatureTests(TemporaryFolderClassSetup, unittest.TestCase):
184+
"""runs in a temporary folder with .hg file"""
185+
186+
@classmethod
187+
def prepare_directory(cls, root):
188+
with open(os.path.join(root, '.hg'), 'a'):
189+
pass
190+
191+
def testCodeliteSample(self):
192+
self.checkAllInFolder('./samples/codelite-sample', 1)
193+
194+
class SvnRepoSignatureTests(TemporaryFolderClassSetup, unittest.TestCase):
195+
"""runs in a temporary folder with .svn file"""
196+
197+
@classmethod
198+
def prepare_directory(cls, root):
199+
with open(os.path.join(root, '.svn'), 'a'):
200+
pass
201+
202+
def testCodeliteSample(self):
203+
self.checkAllInFolder('./samples/codelite-sample', 1)
204+
135205
if __name__ == '__main__':
136-
unittest.main()
206+
unittest.main()

0 commit comments

Comments
 (0)