Commit c3e0f26b authored by Chris McDonald's avatar Chris McDonald Committed by Commit Bot

Make test_env compatible with python 2 & 3

Use the vendored copy of the `six` library to make this code work across
Python versions.

Bug: 942720
Change-Id: I4bdb1032ed8e89e733b929eb13ada54efeb3b019
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2229335
Commit-Queue: Chris McDonald <cjmcdonald@chromium.org>
Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
Reviewed-by: default avatarBen Pastene <bpastene@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774934}
parent b7d0c622
#!/usr/bin/env python2 #!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
"""Sets environment variables needed to run a chromium unit test.""" """Sets environment variables needed to run a chromium unit test."""
from __future__ import print_function
import io import io
import os import os
import signal import signal
...@@ -13,6 +14,13 @@ import subprocess ...@@ -13,6 +14,13 @@ import subprocess
import sys import sys
import time import time
SIX_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'third_party',
'six')
sys.path.insert(0, SIX_DIR)
import six
# This is hardcoded to be src/ relative to this script. # This is hardcoded to be src/ relative to this script.
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...@@ -167,12 +175,12 @@ def symbolize_snippets_in_json(cmd, env): ...@@ -167,12 +175,12 @@ def symbolize_snippets_in_json(cmd, env):
p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env) p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
(_, stderr) = p.communicate() (_, stderr) = p.communicate()
except OSError as e: except OSError as e:
print >> sys.stderr, 'Exception while symbolizing snippets: %s' % e print('Exception while symbolizing snippets: %s' % e, file=sys.stderr)
raise raise
if p.returncode != 0: if p.returncode != 0:
print >> sys.stderr, "Error: failed to symbolize snippets in JSON:\n" print("Error: failed to symbolize snippets in JSON:\n", file=sys.stderr)
print >> sys.stderr, stderr print(stderr, file=sys.stderr)
raise subprocess.CalledProcessError(p.returncode, symbolize_command) raise subprocess.CalledProcessError(p.returncode, symbolize_command)
...@@ -339,13 +347,13 @@ def run_executable(cmd, env, stdoutfile=None): ...@@ -339,13 +347,13 @@ def run_executable(cmd, env, stdoutfile=None):
# because you need them to reproduce the task properly. # because you need them to reproduce the task properly.
env_to_print = extra_env.copy() env_to_print = extra_env.copy()
for env_var_name in ('GTEST_SHARD_INDEX', 'GTEST_TOTAL_SHARDS'): for env_var_name in ('GTEST_SHARD_INDEX', 'GTEST_TOTAL_SHARDS'):
if env_var_name in env: if env_var_name in env:
env_to_print[env_var_name] = env[env_var_name] env_to_print[env_var_name] = env[env_var_name]
print('Additional test environment:\n%s\n' print('Additional test environment:\n%s\n'
'Command: %s\n' % ( 'Command: %s\n' % (
'\n'.join(' %s=%s' % '\n'.join(' %s=%s' %
(k, v) for k, v in sorted(env_to_print.iteritems())), (k, v) for k, v in sorted(six.iteritems(env_to_print))),
' '.join(cmd))) ' '.join(cmd)))
sys.stdout.flush() sys.stdout.flush()
env.update(extra_env or {}) env.update(extra_env or {})
...@@ -371,7 +379,7 @@ def run_executable(cmd, env, stdoutfile=None): ...@@ -371,7 +379,7 @@ def run_executable(cmd, env, stdoutfile=None):
else: else:
return run_command(cmd, env=env, log=False) return run_command(cmd, env=env, log=False)
except OSError: except OSError:
print >> sys.stderr, 'Failed to start %s' % cmd print('Failed to start %s' % cmd, file=sys.stderr)
raise raise
......
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
"""Script for use in test_env unittests.""" """Script for use in test_env unittests."""
import os from __future__ import print_function
import signal import signal
import sys import sys
import time import time
def print_signal(sig, *_): def print_signal(sig, *_args):
print 'Signal :{}'.format(sig) print('Signal :{}'.format(sig))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -17,20 +17,29 @@ import sys ...@@ -17,20 +17,29 @@ import sys
import time import time
import unittest import unittest
HERE = os.path.dirname(os.path.abspath(__file__))
TEST_SCRIPT = 'test_env_user_script.py' TEST_SCRIPT = os.path.join(HERE, 'test_env_user_script.py')
def launch_process_windows(args): def launch_process_windows(args):
# The `universal_newlines` option is equivalent to `text` in Python 3.
return subprocess.Popen( return subprocess.Popen(
[sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE, [sys.executable, TEST_SCRIPT] + args,
stderr=subprocess.STDOUT, env=os.environ.copy(), stdout=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) stderr=subprocess.STDOUT,
env=os.environ.copy(),
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
universal_newlines=True)
def launch_process_nonwindows(args): def launch_process_nonwindows(args):
# The `universal_newlines` option is equivalent to `text` in Python 3.
return subprocess.Popen( return subprocess.Popen(
[sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE, [sys.executable, TEST_SCRIPT] + args,
stderr=subprocess.STDOUT, env=os.environ.copy()) stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=os.environ.copy(),
universal_newlines=True)
def read_subprocess_message(proc, starts_with): def read_subprocess_message(proc, starts_with):
...@@ -58,7 +67,7 @@ class SignalingWindowsTest(unittest.TestCase): ...@@ -58,7 +67,7 @@ class SignalingWindowsTest(unittest.TestCase):
proc = launch_process_windows([]) proc = launch_process_windows([])
send_and_wait(proc, signal.CTRL_BREAK_EVENT) send_and_wait(proc, signal.CTRL_BREAK_EVENT)
sig = read_subprocess_message(proc, 'Signal :') sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGBREAK)) self.assertEqual(sig, str(int(signal.SIGBREAK)))
class SignalingNonWindowsTest(unittest.TestCase): class SignalingNonWindowsTest(unittest.TestCase):
...@@ -72,13 +81,13 @@ class SignalingNonWindowsTest(unittest.TestCase): ...@@ -72,13 +81,13 @@ class SignalingNonWindowsTest(unittest.TestCase):
proc = launch_process_nonwindows([]) proc = launch_process_nonwindows([])
send_and_wait(proc, signal.SIGTERM) send_and_wait(proc, signal.SIGTERM)
sig = read_subprocess_message(proc, 'Signal :') sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGTERM)) self.assertEqual(sig, str(int(signal.SIGTERM)))
def test_send_sigint(self): def test_send_sigint(self):
proc = launch_process_nonwindows([]) proc = launch_process_nonwindows([])
send_and_wait(proc, signal.SIGINT) send_and_wait(proc, signal.SIGINT)
sig = read_subprocess_message(proc, 'Signal :') sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGINT)) self.assertEqual(sig, str(int(signal.SIGINT)))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -5,8 +5,12 @@ ...@@ -5,8 +5,12 @@
"""Script for use in test_env unittests.""" """Script for use in test_env unittests."""
import os
import test_env import test_env
HERE = os.path.dirname(os.path.abspath(__file__))
TEST_SCRIPT = os.path.join(HERE, 'test_env_test_script.py')
if __name__ == '__main__': if __name__ == '__main__':
test_env.run_command(['python', 'test_env_test_script.py']) test_env.run_command([TEST_SCRIPT])
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