Commit 6635baff authored by Josip Sokcevic's avatar Josip Sokcevic Committed by LUCI CQ

Make CannedChecksUnittest py3 compatible

R=apolito@google.com

Change-Id: I24610ca05536b4ac6eed2c0a33ef411650b46dd0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3292089Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
parent 90da0e15
...@@ -206,9 +206,10 @@ class ThreadPool(object): ...@@ -206,9 +206,10 @@ class ThreadPool(object):
p = subprocess.Popen(cmd, **kwargs) p = subprocess.Popen(cmd, **kwargs)
with Timer(self.timeout, p.terminate) as timer: with Timer(self.timeout, p.terminate) as timer:
stdout, _ = sigint_handler.wait(p, stdin) stdout, _ = sigint_handler.wait(p, stdin)
stdout = stdout.decode('utf-8', 'ignore')
if timer.completed: if timer.completed:
stdout = 'Process timed out after %ss\n%s' % (self.timeout, stdout) stdout = 'Process timed out after %ss\n%s' % (self.timeout, stdout)
return p.returncode, stdout.decode('utf-8', 'ignore'); return p.returncode, stdout
def CallCommand(self, test): def CallCommand(self, test):
"""Runs an external program. """Runs an external program.
...@@ -1493,9 +1494,9 @@ class PresubmitExecuter(object): ...@@ -1493,9 +1494,9 @@ class PresubmitExecuter(object):
m = re.search('^USE_PYTHON3 = (True|False)$', script_text, m = re.search('^USE_PYTHON3 = (True|False)$', script_text,
flags=re.MULTILINE) flags=re.MULTILINE)
if m: if m:
use_python3 = m.group(1) == 'True' use_python3 = m.group(1) == 'True'
else: else:
use_python3 = self.use_python3 use_python3 = self.use_python3
if (((sys.version_info.major == 2) and use_python3) or if (((sys.version_info.major == 2) and use_python3) or
((sys.version_info.major == 3) and not use_python3)): ((sys.version_info.major == 3) and not use_python3)):
return [] return []
......
...@@ -160,6 +160,10 @@ index fe3de7b..54ae6e1 100755 ...@@ -160,6 +160,10 @@ index fe3de7b..54ae6e1 100755
def setUp(self): def setUp(self):
super(PresubmitTestsBase, self).setUp() super(PresubmitTestsBase, self).setUp()
# Disable string diff max. It's hard to parse assertion error if there's
# limit set.
self.maxDiff = None
class FakeChange(object): class FakeChange(object):
def __init__(self, obj): def __init__(self, obj):
self._root = obj.fake_root_dir self._root = obj.fake_root_dir
...@@ -2361,7 +2365,7 @@ the current line as well! ...@@ -2361,7 +2365,7 @@ the current line as well!
def testRunPythonUnitTestsNonExistentUpload(self): def testRunPythonUnitTestsNonExistentUpload(self):
input_api = self.MockInputApi(None, False) input_api = self.MockInputApi(None, False)
subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter
presubmit.sigint_handler.wait.return_value = ('foo', None) presubmit.sigint_handler.wait.return_value = (b'foo', None)
results = presubmit_canned_checks.RunPythonUnitTests( results = presubmit_canned_checks.RunPythonUnitTests(
input_api, presubmit.OutputApi, ['_non_existent_module']) input_api, presubmit.OutputApi, ['_non_existent_module'])
...@@ -2372,7 +2376,7 @@ the current line as well! ...@@ -2372,7 +2376,7 @@ the current line as well!
def testRunPythonUnitTestsNonExistentCommitting(self): def testRunPythonUnitTestsNonExistentCommitting(self):
input_api = self.MockInputApi(None, True) input_api = self.MockInputApi(None, True)
subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter
presubmit.sigint_handler.wait.return_value = ('foo', None) presubmit.sigint_handler.wait.return_value = (b'foo', None)
results = presubmit_canned_checks.RunPythonUnitTests( results = presubmit_canned_checks.RunPythonUnitTests(
input_api, presubmit.OutputApi, ['_non_existent_module']) input_api, presubmit.OutputApi, ['_non_existent_module'])
...@@ -2383,7 +2387,7 @@ the current line as well! ...@@ -2383,7 +2387,7 @@ the current line as well!
input_api = self.MockInputApi(None, False) input_api = self.MockInputApi(None, False)
input_api.unittest = mock.MagicMock(unittest) input_api.unittest = mock.MagicMock(unittest)
subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter
presubmit.sigint_handler.wait.return_value = ('foo', None) presubmit.sigint_handler.wait.return_value = (b'foo', None)
results = presubmit_canned_checks.RunPythonUnitTests( results = presubmit_canned_checks.RunPythonUnitTests(
input_api, presubmit.OutputApi, ['test_module']) input_api, presubmit.OutputApi, ['test_module'])
...@@ -2397,7 +2401,7 @@ the current line as well! ...@@ -2397,7 +2401,7 @@ the current line as well!
def testRunPythonUnitTestsFailureCommitting(self): def testRunPythonUnitTestsFailureCommitting(self):
input_api = self.MockInputApi(None, True) input_api = self.MockInputApi(None, True)
subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter subprocess.Popen().returncode = 1 # pylint: disable=no-value-for-parameter
presubmit.sigint_handler.wait.return_value = ('foo', None) presubmit.sigint_handler.wait.return_value = (b'foo', None)
results = presubmit_canned_checks.RunPythonUnitTests( results = presubmit_canned_checks.RunPythonUnitTests(
input_api, presubmit.OutputApi, ['test_module']) input_api, presubmit.OutputApi, ['test_module'])
...@@ -2411,7 +2415,7 @@ the current line as well! ...@@ -2411,7 +2415,7 @@ the current line as well!
input_api = self.MockInputApi(None, False) input_api = self.MockInputApi(None, False)
input_api.unittest = mock.MagicMock(unittest) input_api.unittest = mock.MagicMock(unittest)
subprocess.Popen().returncode = 0 # pylint: disable=no-value-for-parameter subprocess.Popen().returncode = 0 # pylint: disable=no-value-for-parameter
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
presubmit_canned_checks.RunPythonUnitTests( presubmit_canned_checks.RunPythonUnitTests(
input_api, presubmit.OutputApi, ['test_module']) input_api, presubmit.OutputApi, ['test_module'])
...@@ -2431,7 +2435,7 @@ the current line as well! ...@@ -2431,7 +2435,7 @@ the current line as well!
process = mock.Mock() process = mock.Mock()
process.returncode = 0 process.returncode = 0
subprocess.Popen.return_value = process subprocess.Popen.return_value = process
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
pylint = os.path.join(_ROOT, 'pylint-1.5') pylint = os.path.join(_ROOT, 'pylint-1.5')
pylintrc = os.path.join(_ROOT, 'pylintrc') pylintrc = os.path.join(_ROOT, 'pylintrc')
...@@ -2456,12 +2460,11 @@ the current line as well! ...@@ -2456,12 +2460,11 @@ the current line as well!
self.assertEqual(presubmit.sigint_handler.wait.mock_calls, [ self.assertEqual(presubmit.sigint_handler.wait.mock_calls, [
mock.call( mock.call(
process, process,
'--rcfile=%s\n--disable=all\n--enable=cyclic-import\nfile1.py' ('--rcfile=%s\n--disable=all\n--enable=cyclic-import\nfile1.py' %
% pylintrc), pylintrc).encode('utf-8')),
mock.call( mock.call(process,
process, ('--rcfile=%s\n--disable=cyclic-import\n--jobs=2\nfile1.py' %
'--rcfile=%s\n--disable=cyclic-import\n--jobs=2\nfile1.py' pylintrc).encode('utf-8')),
% pylintrc),
]) ])
self.checkstdout('') self.checkstdout('')
...@@ -2988,7 +2991,7 @@ the current line as well! ...@@ -2988,7 +2991,7 @@ the current line as well!
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
input_api.verbose = True input_api.verbose = True
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
process1 = mock.Mock() process1 = mock.Mock()
process1.returncode = 1 process1.returncode = 1
...@@ -3035,7 +3038,7 @@ the current line as well! ...@@ -3035,7 +3038,7 @@ the current line as well!
input_api.verbose = True input_api.verbose = True
input_api.thread_pool.timeout = 100 input_api.thread_pool.timeout = 100
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
subprocess.Popen.return_value = mock.Mock(returncode=0) subprocess.Popen.return_value = mock.Mock(returncode=0)
results = presubmit_canned_checks.RunUnitTests( results = presubmit_canned_checks.RunUnitTests(
...@@ -3061,7 +3064,7 @@ the current line as well! ...@@ -3061,7 +3064,7 @@ the current line as well!
input_api.verbose = True input_api.verbose = True
input_api.thread_pool.timeout = 100 input_api.thread_pool.timeout = 100
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
subprocess.Popen.return_value = mock.Mock(returncode=1) subprocess.Popen.return_value = mock.Mock(returncode=1)
timer_instance = mock.Mock() timer_instance = mock.Mock()
...@@ -3094,7 +3097,7 @@ the current line as well! ...@@ -3094,7 +3097,7 @@ the current line as well!
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
input_api.verbose = True input_api.verbose = True
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
subprocesses = [ subprocesses = [
mock.Mock(returncode=1), mock.Mock(returncode=1),
...@@ -3150,7 +3153,7 @@ the current line as well! ...@@ -3150,7 +3153,7 @@ the current line as well!
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
input_api.verbose = True input_api.verbose = True
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
subprocess.Popen.side_effect = [ subprocess.Popen.side_effect = [
mock.Mock(returncode=1), mock.Mock(returncode=1),
...@@ -3194,7 +3197,7 @@ the current line as well! ...@@ -3194,7 +3197,7 @@ the current line as well!
input_api = self.MockInputApi(change, False) input_api = self.MockInputApi(change, False)
input_api.verbose = True input_api.verbose = True
input_api.PresubmitLocalPath.return_value = self.fake_root_dir input_api.PresubmitLocalPath.return_value = self.fake_root_dir
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
subprocess.Popen.side_effect = [ subprocess.Popen.side_effect = [
mock.Mock(returncode=1), mock.Mock(returncode=1),
...@@ -3243,7 +3246,7 @@ the current line as well! ...@@ -3243,7 +3246,7 @@ the current line as well!
process = mock.Mock() process = mock.Mock()
process.returncode = 0 process.returncode = 0
subprocess.Popen.return_value = process subprocess.Popen.return_value = process
presubmit.sigint_handler.wait.return_value = ('', None) presubmit.sigint_handler.wait.return_value = (b'', None)
results = presubmit_canned_checks.RunUnitTestsInDirectory( results = presubmit_canned_checks.RunUnitTestsInDirectory(
input_api, input_api,
...@@ -3397,7 +3400,7 @@ class ThreadPoolTest(unittest.TestCase): ...@@ -3397,7 +3400,7 @@ class ThreadPoolTest(unittest.TestCase):
mock.patch('subprocess2.Popen').start() mock.patch('subprocess2.Popen').start()
mock.patch('presubmit_support.sigint_handler').start() mock.patch('presubmit_support.sigint_handler').start()
mock.patch('presubmit_support.time_time', return_value=0).start() mock.patch('presubmit_support.time_time', return_value=0).start()
presubmit.sigint_handler.wait.return_value = ('stdout', '') presubmit.sigint_handler.wait.return_value = (b'stdout', '')
self.addCleanup(mock.patch.stopall) self.addCleanup(mock.patch.stopall)
def testSurfaceExceptions(self): def testSurfaceExceptions(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment