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.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Sets environment variables needed to run a chromium unit test."""
from __future__ import print_function
import io
import os
import signal
......@@ -13,6 +14,13 @@ import subprocess
import sys
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.
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
......@@ -167,12 +175,12 @@ def symbolize_snippets_in_json(cmd, env):
p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
(_, stderr) = p.communicate()
except OSError as e:
print >> sys.stderr, 'Exception while symbolizing snippets: %s' % e
print('Exception while symbolizing snippets: %s' % e, file=sys.stderr)
raise
if p.returncode != 0:
print >> sys.stderr, "Error: failed to symbolize snippets in JSON:\n"
print >> sys.stderr, stderr
print("Error: failed to symbolize snippets in JSON:\n", file=sys.stderr)
print(stderr, file=sys.stderr)
raise subprocess.CalledProcessError(p.returncode, symbolize_command)
......@@ -345,7 +353,7 @@ def run_executable(cmd, env, stdoutfile=None):
print('Additional test environment:\n%s\n'
'Command: %s\n' % (
'\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)))
sys.stdout.flush()
env.update(extra_env or {})
......@@ -371,7 +379,7 @@ def run_executable(cmd, env, stdoutfile=None):
else:
return run_command(cmd, env=env, log=False)
except OSError:
print >> sys.stderr, 'Failed to start %s' % cmd
print('Failed to start %s' % cmd, file=sys.stderr)
raise
......
......@@ -5,14 +5,14 @@
"""Script for use in test_env unittests."""
import os
from __future__ import print_function
import signal
import sys
import time
def print_signal(sig, *_):
print 'Signal :{}'.format(sig)
def print_signal(sig, *_args):
print('Signal :{}'.format(sig))
if __name__ == '__main__':
......
......@@ -17,20 +17,29 @@ import sys
import time
import unittest
TEST_SCRIPT = 'test_env_user_script.py'
HERE = os.path.dirname(os.path.abspath(__file__))
TEST_SCRIPT = os.path.join(HERE, 'test_env_user_script.py')
def launch_process_windows(args):
# The `universal_newlines` option is equivalent to `text` in Python 3.
return subprocess.Popen(
[sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=os.environ.copy(),
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
[sys.executable, TEST_SCRIPT] + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=os.environ.copy(),
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
universal_newlines=True)
def launch_process_nonwindows(args):
# The `universal_newlines` option is equivalent to `text` in Python 3.
return subprocess.Popen(
[sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=os.environ.copy())
[sys.executable, TEST_SCRIPT] + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=os.environ.copy(),
universal_newlines=True)
def read_subprocess_message(proc, starts_with):
......@@ -58,7 +67,7 @@ class SignalingWindowsTest(unittest.TestCase):
proc = launch_process_windows([])
send_and_wait(proc, signal.CTRL_BREAK_EVENT)
sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGBREAK))
self.assertEqual(sig, str(int(signal.SIGBREAK)))
class SignalingNonWindowsTest(unittest.TestCase):
......@@ -72,13 +81,13 @@ class SignalingNonWindowsTest(unittest.TestCase):
proc = launch_process_nonwindows([])
send_and_wait(proc, signal.SIGTERM)
sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGTERM))
self.assertEqual(sig, str(int(signal.SIGTERM)))
def test_send_sigint(self):
proc = launch_process_nonwindows([])
send_and_wait(proc, signal.SIGINT)
sig = read_subprocess_message(proc, 'Signal :')
self.assertEqual(sig, str(signal.SIGINT))
self.assertEqual(sig, str(int(signal.SIGINT)))
if __name__ == '__main__':
......
......@@ -5,8 +5,12 @@
"""Script for use in test_env unittests."""
import os
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__':
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