Add tests for both universal_newlines=True and False.

Fix stdout+stderr on Windows to binary mode in the child test process to make
sure testing is done correctly.

R=dpranke@chromium.org
BUG=
TEST=manually tested on OSX, Windows, cygwin and ubuntu


Review URL: http://codereview.chromium.org/8472002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@109282 0039d316-1c4b-4281-b951-d872f2087c98
experimental/szager/collated-output
maruel@chromium.org 14 years ago
parent 65be6f6bcf
commit f2dca4e174

@ -21,14 +21,24 @@ import subprocess2
# pylint: disable=R0201 # pylint: disable=R0201
def convert(string): def convert_to_crlf(string):
"""Converts string to CRLF on Windows.""" """Unconditionally convert LF to CRLF."""
return string.replace('\n', '\r\n')
def convert_to_cr(string):
"""Unconditionally convert LF to CR."""
return string.replace('\n', '\r')
def convert_win(string):
"""Converts string to CRLF on Windows only."""
if sys.platform == 'win32': if sys.platform == 'win32':
return string.replace('\n', '\r\n') return string.replace('\n', '\r\n')
return string return string
class Subprocess2Test(unittest.TestCase): class DefaultsTest(unittest.TestCase):
# Can be mocked in a test. # Can be mocked in a test.
TO_SAVE = { TO_SAVE = {
subprocess2: [ subprocess2: [
@ -37,8 +47,6 @@ class Subprocess2Test(unittest.TestCase):
} }
def setUp(self): def setUp(self):
self.exe_path = __file__
self.exe = [sys.executable, self.exe_path, '--child']
self.saved = {} self.saved = {}
for module, names in self.TO_SAVE.iteritems(): for module, names in self.TO_SAVE.iteritems():
self.saved[module] = dict( self.saved[module] = dict(
@ -155,19 +163,12 @@ class Subprocess2Test(unittest.TestCase):
} }
self.assertEquals(expected, results) self.assertEquals(expected, results)
def test_timeout(self):
out, returncode = subprocess2.communicate(
self.exe + ['--sleep', '--stdout'],
timeout=0.01,
stdout=subprocess2.PIPE,
shell=False)
self.assertEquals(subprocess2.TIMED_OUT, returncode)
self.assertEquals(('', None), out)
def test_timeout_shell_throws(self): def test_timeout_shell_throws(self):
# Never called.
_ = self._fake_Popen()
try: try:
subprocess2.communicate( subprocess2.communicate(
self.exe + ['--sleep', '--stdout'], sys.executable,
timeout=0.01, timeout=0.01,
stdout=subprocess2.PIPE, stdout=subprocess2.PIPE,
shell=True) shell=True)
@ -175,6 +176,45 @@ class Subprocess2Test(unittest.TestCase):
except TypeError: except TypeError:
pass pass
class S2Test(unittest.TestCase):
def setUp(self):
super(S2Test, self).setUp()
self.exe_path = __file__
self.exe = [sys.executable, self.exe_path, '--child']
def _run_test(self, function):
"""Runs tests in 6 combinations:
- LF output with universal_newlines=False
- CR output with universal_newlines=False
- CRLF output with universal_newlines=False
- LF output with universal_newlines=True
- CR output with universal_newlines=True
- CRLF output with universal_newlines=True
First |function| argument is the convertion for the origianl expected LF
string to the right EOL.
Second |function| argument is the executable and initial flag to run, to
control what EOL is used by the child process.
Third |function| argument is universal_newlines value.
"""
noop = lambda x: x
function(noop, self.exe, False)
function(convert_to_cr, self.exe + ['--cr'], False)
function(convert_to_crlf, self.exe + ['--crlf'], False)
function(noop, self.exe, True)
function(noop, self.exe + ['--cr'], True)
function(noop, self.exe + ['--crlf'], True)
def test_timeout(self):
out, returncode = subprocess2.communicate(
self.exe + ['--sleep_first', '--stdout'],
timeout=0.01,
stdout=subprocess2.PIPE,
shell=False)
self.assertEquals(subprocess2.TIMED_OUT, returncode)
self.assertEquals(('', None), out)
def test_check_output_no_stdout(self): def test_check_output_no_stdout(self):
try: try:
subprocess2.check_output(self.exe, stdout=subprocess2.PIPE) subprocess2.check_output(self.exe, stdout=subprocess2.PIPE)
@ -183,65 +223,78 @@ class Subprocess2Test(unittest.TestCase):
pass pass
def test_stdout_void(self): def test_stdout_void(self):
(out, err), code = subprocess2.communicate( def fn(c, e, un):
self.exe + ['--stdout', '--stderr'], (out, err), code = subprocess2.communicate(
stdout=subprocess2.VOID, e + ['--stdout', '--stderr'],
stderr=subprocess2.PIPE) stdout=subprocess2.VOID,
self.assertEquals(None, out) stderr=subprocess2.PIPE,
self.assertEquals(convert('a\nbb\nccc\n'), err) universal_newlines=un)
self.assertEquals(0, code) self.assertEquals(None, out)
self.assertEquals(c('a\nbb\nccc\n'), err)
self.assertEquals(0, code)
self._run_test(fn)
def test_stderr_void(self): def test_stderr_void(self):
(out, err), code = subprocess2.communicate( def fn(c, e, un):
self.exe + ['--stdout', '--stderr'], (out, err), code = subprocess2.communicate(
universal_newlines=True, e + ['--stdout', '--stderr'],
stdout=subprocess2.PIPE, stdout=subprocess2.PIPE,
stderr=subprocess2.VOID) stderr=subprocess2.VOID,
self.assertEquals('A\nBB\nCCC\n', out) universal_newlines=un)
self.assertEquals(None, err) self.assertEquals(c('A\nBB\nCCC\n'), out)
self.assertEquals(0, code) self.assertEquals(None, err)
self.assertEquals(0, code)
self._run_test(fn)
def test_check_output_throw_stdout(self): def test_check_output_throw_stdout(self):
try: def fn(c, e, un):
subprocess2.check_output( try:
self.exe + ['--fail', '--stdout'], universal_newlines=True) subprocess2.check_output(
self.fail() e + ['--fail', '--stdout'], universal_newlines=un)
except subprocess2.CalledProcessError, e: self.fail()
self.assertEquals('A\nBB\nCCC\n', e.stdout) except subprocess2.CalledProcessError, e:
self.assertEquals(None, e.stderr) self.assertEquals(c('A\nBB\nCCC\n'), e.stdout)
self.assertEquals(64, e.returncode) self.assertEquals(None, e.stderr)
self.assertEquals(64, e.returncode)
self._run_test(fn)
def test_check_output_throw_no_stderr(self): def test_check_output_throw_no_stderr(self):
try: def fn(c, e, un):
subprocess2.check_output( try:
self.exe + ['--fail', '--stderr'], universal_newlines=True) subprocess2.check_output(
self.fail() e + ['--fail', '--stderr'], universal_newlines=un)
except subprocess2.CalledProcessError, e: self.fail()
self.assertEquals('', e.stdout) except subprocess2.CalledProcessError, e:
self.assertEquals(None, e.stderr) self.assertEquals(c(''), e.stdout)
self.assertEquals(64, e.returncode) self.assertEquals(None, e.stderr)
self.assertEquals(64, e.returncode)
self._run_test(fn)
def test_check_output_throw_stderr(self): def test_check_output_throw_stderr(self):
try: def fn(c, e, un):
subprocess2.check_output( try:
self.exe + ['--fail', '--stderr'], stderr=subprocess2.PIPE, subprocess2.check_output(
universal_newlines=True) e + ['--fail', '--stderr'], stderr=subprocess2.PIPE,
self.fail() universal_newlines=un)
except subprocess2.CalledProcessError, e: self.fail()
self.assertEquals('', e.stdout) except subprocess2.CalledProcessError, e:
self.assertEquals('a\nbb\nccc\n', e.stderr) self.assertEquals('', e.stdout)
self.assertEquals(64, e.returncode) self.assertEquals(c('a\nbb\nccc\n'), e.stderr)
self.assertEquals(64, e.returncode)
self._run_test(fn)
def test_check_output_throw_stderr_stdout(self): def test_check_output_throw_stderr_stdout(self):
try: def fn(c, e, un):
subprocess2.check_output( try:
self.exe + ['--fail', '--stderr'], stderr=subprocess2.STDOUT, subprocess2.check_output(
universal_newlines=True) e + ['--fail', '--stderr'], stderr=subprocess2.STDOUT,
self.fail() universal_newlines=un)
except subprocess2.CalledProcessError, e: self.fail()
self.assertEquals('a\nbb\nccc\n', e.stdout) except subprocess2.CalledProcessError, e:
self.assertEquals(None, e.stderr) self.assertEquals(c('a\nbb\nccc\n'), e.stdout)
self.assertEquals(64, e.returncode) self.assertEquals(None, e.stderr)
self.assertEquals(64, e.returncode)
self._run_test(fn)
def test_check_call_throw(self): def test_check_call_throw(self):
try: try:
@ -253,45 +306,57 @@ class Subprocess2Test(unittest.TestCase):
self.assertEquals(64, e.returncode) self.assertEquals(64, e.returncode)
def test_check_output_tee_stderr(self): def test_check_output_tee_stderr(self):
stderr = [] def fn(c, e, un):
out, returncode = subprocess2.communicate( stderr = []
self.exe + ['--stderr'], stderr=stderr.append) out, returncode = subprocess2.communicate(
self.assertEquals(convert('a\nbb\nccc\n'), ''.join(stderr)) e + ['--stderr'], stderr=stderr.append,
self.assertEquals((None, None), out) universal_newlines=un)
self.assertEquals(0, returncode) self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr))
self.assertEquals((None, None), out)
self.assertEquals(0, returncode)
self._run_test(fn)
def test_check_output_tee_stdout_stderr(self): def test_check_output_tee_stdout_stderr(self):
stdout = [] def fn(c, e, un):
stderr = [] stdout = []
out, returncode = subprocess2.communicate( stderr = []
self.exe + ['--stdout', '--stderr'], out, returncode = subprocess2.communicate(
stdout=stdout.append, e + ['--stdout', '--stderr'],
stderr=stderr.append) stdout=stdout.append,
self.assertEquals(convert('A\nBB\nCCC\n'), ''.join(stdout)) stderr=stderr.append,
self.assertEquals(convert('a\nbb\nccc\n'), ''.join(stderr)) universal_newlines=un)
self.assertEquals((None, None), out) self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout))
self.assertEquals(0, returncode) self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr))
self.assertEquals((None, None), out)
self.assertEquals(0, returncode)
self._run_test(fn)
def test_check_output_tee_stdin(self): def test_check_output_tee_stdin(self):
stdout = [] def fn(c, e, un):
stdin = '0123456789' stdout = []
out, returncode = subprocess2.communicate( stdin = '0123456789'
self.exe + ['--stdout', '--read'], stdin=stdin, stdout=stdout.append) out, returncode = subprocess2.communicate(
self.assertEquals(convert('A\nBB\nCCC\n'), ''.join(stdout)) e + ['--stdout', '--read'], stdin=stdin, stdout=stdout.append,
self.assertEquals((None, None), out) universal_newlines=un)
self.assertEquals(0, returncode) self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout))
self.assertEquals((None, None), out)
self.assertEquals(0, returncode)
self._run_test(fn)
def test_check_output_tee_throw(self): def test_check_output_tee_throw(self):
stderr = [] def fn(c, e, un):
try: stderr = []
subprocess2.check_output( try:
self.exe + ['--stderr', '--fail'], stderr=stderr.append) subprocess2.check_output(
self.fail() e + ['--stderr', '--fail'], stderr=stderr.append,
except subprocess2.CalledProcessError, e: universal_newlines=un)
self.assertEquals(convert('a\nbb\nccc\n'), ''.join(stderr)) self.fail()
self.assertEquals('', e.stdout) except subprocess2.CalledProcessError, e:
self.assertEquals(None, e.stderr) self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr))
self.assertEquals(64, e.returncode) self.assertEquals('', e.stdout)
self.assertEquals(None, e.stderr)
self.assertEquals(64, e.returncode)
self._run_test(fn)
def test_check_output_tee_large(self): def test_check_output_tee_large(self):
stdout = [] stdout = []
@ -314,6 +379,13 @@ class Subprocess2Test(unittest.TestCase):
def child_main(args): def child_main(args):
if sys.platform == 'win32':
# Annoying, make sure the output is not translated on Windows.
# pylint: disable=E1101,F0401
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option( parser.add_option(
'--fail', '--fail',
@ -321,22 +393,29 @@ def child_main(args):
action='store_const', action='store_const',
default=0, default=0,
const=64) const=64)
parser.add_option(
'--crlf', action='store_const', const='\r\n', dest='eol', default='\n')
parser.add_option(
'--cr', action='store_const', const='\r', dest='eol')
parser.add_option('--stdout', action='store_true') parser.add_option('--stdout', action='store_true')
parser.add_option('--stderr', action='store_true') parser.add_option('--stderr', action='store_true')
parser.add_option('--sleep', action='store_true') parser.add_option('--sleep_first', action='store_true')
parser.add_option('--sleep_last', action='store_true')
parser.add_option('--large', action='store_true') parser.add_option('--large', action='store_true')
parser.add_option('--read', action='store_true') parser.add_option('--read', action='store_true')
options, args = parser.parse_args(args) options, args = parser.parse_args(args)
if args: if args:
parser.error('Internal error') parser.error('Internal error')
if options.sleep: if options.sleep_first:
time.sleep(10) time.sleep(10)
def do(string): def do(string):
if options.stdout: if options.stdout:
print >> sys.stdout, string.upper() sys.stdout.write(string.upper())
sys.stdout.write(options.eol)
if options.stderr: if options.stderr:
print >> sys.stderr, string.lower() sys.stderr.write(string.lower())
sys.stderr.write(options.eol)
do('A') do('A')
do('BB') do('BB')
@ -351,6 +430,8 @@ def child_main(args):
pass pass
except OSError: except OSError:
pass pass
if options.sleep_last:
time.sleep(10)
return options.return_value return options.return_value

Loading…
Cancel
Save