sapling/tests/test-extutil.py
Adam Simpkins fdc2c3e6f9 extutil: add unit tests for runbgcommand
Summary:
Add some basic unit tests for extutil.runbgcommand().

This also changes the behavior to throw an OSError if we fail to execute the
process, instead of a subprocess.CalledProcessError.  This matches the behavior
of the subprocess module when it fails to execute the command.

Test Plan: Ran the tests.

Reviewers: stash, quark

Reviewed By: quark

Subscribers: net-systems-diffs@fb.com, yogeshwer, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D4864999

Signature: t1:4864999:1491874918:c03edafe02af217e41c28a770137bfd72bcbba9b
2017-04-11 11:25:40 -07:00

48 lines
1.5 KiB
Python

# Copyright 2004-present Facebook. All Rights Reserved.
import errno
import os
import sys
import time
import unittest
import silenttestrunner
if __name__ == '__main__':
sys.path.insert(0, os.path.join(os.environ["TESTDIR"], "..", "hgext3rd"))
import extutil
class ExtutilTests(unittest.TestCase):
def testbgcommandnoblock(self):
'''runbgcommand() should return without waiting for the process to
finish.'''
env = os.environ.copy()
start = time.time()
extutil.runbgcommand(['sleep', '5'], env)
end = time.time()
if end - start >= 1.0:
self.fail('runbgcommand() took took %s seconds, should have '
'returned immediately' % (end - start))
def testbgcommandfailure(self):
'''runbgcommand() should throw if executing the process fails.'''
env = os.environ.copy()
try:
extutil.runbgcommand(['no_such_program', 'arg1', 'arg2'], env)
self.fail('expected runbgcommand to fail with ENOENT')
except OSError as ex:
self.assertEqual(ex.errno, errno.ENOENT)
def testbgcommandfailure(self):
'''runbgcommand() should throw if executing the process fails.'''
env = os.environ.copy()
try:
extutil.runbgcommand([os.devnull, 'arg1', 'arg2'], env)
self.fail('expected runbgcommand to fail with EACCES')
except OSError as ex:
self.assertEqual(ex.errno, errno.EACCES)
if __name__ == '__main__':
silenttestrunner.main(__name__)