sapling/tests/test-fb-hgext-extutil.py

82 lines
2.6 KiB
Python
Raw Normal View History

# Copyright 2004-present Facebook. All Rights Reserved.
import errno
import os
import time
import unittest
import silenttestrunner
from mercurial import (
error,
vfs,
worker,
)
from hgext import extutil
locktimeout = 25
locksuccess = 24
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))
flake8: resolve some F checks Summary: Solves issues below: ``` hgext/backups.py:18:1: F811 redefinition of unused 'registrar' from line 17 hgext/catnotate.py:1:1: F811 redefinition of unused 'util' from line 1 hgext/remotenames.py:57:5: F811 redefinition of unused 'registrar' from line 34 hgsubversion/setup.py:103:5: F401 'mercurial' imported but unused hgsubversion/setup.py:109:5: F401 'hgsubversion.svnwrap.svn_swig_wrapper' imported but unused i18n/polib.py:1281:29: F841 local variable 'exc' is assigned to but never used (Python 2) i18n/polib.py:1427:13: F841 local variable 'typ' is assigned to but never used i18n/polib.py:28:1: F401 'sys' imported but unused mercurial/manifest.py:411:5: F811 redefinition of unused '_lazymanifest' from line 168 mercurial/posix.py:419:5: F811 redefinition of unused 'normcasefallback' from line 362 mercurial/posix.py:425:5: F811 redefinition of unused 'checkexec' from line 167 mercurial/posix.py:431:5: F811 redefinition of unused 'checklink' from line 234 mercurial/pycompat.py:29:5: F401 'http.cookiejar as cookielib' imported but unused mercurial/pycompat.py:30:5: F401 'http.client as httplib' imported but unused mercurial/pycompat.py:31:5: F401 'pickle' imported but unused mercurial/pycompat.py:33:5: F401 'socketserver' imported but unused mercurial/pycompat.py:34:5: F401 'xmlrpc.client as xmlrpclib' imported but unused mercurial/statprof.py:573:36: F812 list comprehension redefines 'parent' from line 562 (Python 2) mercurial/util.py:1076:5: F811 redefinition of unused 'nogc' from line 1051 mercurial/util.py:3221:5: F811 redefinition of unused 'dirs' from line 3184 tests/silenttestrunner.py:24:5: F811 redefinition of unused 'main' from line 6 tests/test-context.py:90:1: F811 redefinition of unused 'scmutil' from line 4 tests/test-fb-hgext-cstore-treemanifest.py:146:5: F811 redefinition of unused 'testDeeplyNested' from line 134 tests/test-fb-hgext-extutil.py:46:5: F811 redefinition of unused 'testbgcommandfailure' from line 37 tests/test_hgsubversion_util.py:47:1: F811 redefinition of unused 'svnwrap' from line 31 (Python 2) tests/test_hgsubversion_util.py:49:1: F811 redefinition of unused 'svnwrap' from line 47 (Python 2) ``` Reviewed By: ryanmce Differential Revision: D6934533 fbshipit-source-id: 8b51851a76fec88bb59107ed05a901d42c7326f8
2018-02-10 04:31:41 +03:00
def testbgcommandfailure1(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)
flake8: resolve some F checks Summary: Solves issues below: ``` hgext/backups.py:18:1: F811 redefinition of unused 'registrar' from line 17 hgext/catnotate.py:1:1: F811 redefinition of unused 'util' from line 1 hgext/remotenames.py:57:5: F811 redefinition of unused 'registrar' from line 34 hgsubversion/setup.py:103:5: F401 'mercurial' imported but unused hgsubversion/setup.py:109:5: F401 'hgsubversion.svnwrap.svn_swig_wrapper' imported but unused i18n/polib.py:1281:29: F841 local variable 'exc' is assigned to but never used (Python 2) i18n/polib.py:1427:13: F841 local variable 'typ' is assigned to but never used i18n/polib.py:28:1: F401 'sys' imported but unused mercurial/manifest.py:411:5: F811 redefinition of unused '_lazymanifest' from line 168 mercurial/posix.py:419:5: F811 redefinition of unused 'normcasefallback' from line 362 mercurial/posix.py:425:5: F811 redefinition of unused 'checkexec' from line 167 mercurial/posix.py:431:5: F811 redefinition of unused 'checklink' from line 234 mercurial/pycompat.py:29:5: F401 'http.cookiejar as cookielib' imported but unused mercurial/pycompat.py:30:5: F401 'http.client as httplib' imported but unused mercurial/pycompat.py:31:5: F401 'pickle' imported but unused mercurial/pycompat.py:33:5: F401 'socketserver' imported but unused mercurial/pycompat.py:34:5: F401 'xmlrpc.client as xmlrpclib' imported but unused mercurial/statprof.py:573:36: F812 list comprehension redefines 'parent' from line 562 (Python 2) mercurial/util.py:1076:5: F811 redefinition of unused 'nogc' from line 1051 mercurial/util.py:3221:5: F811 redefinition of unused 'dirs' from line 3184 tests/silenttestrunner.py:24:5: F811 redefinition of unused 'main' from line 6 tests/test-context.py:90:1: F811 redefinition of unused 'scmutil' from line 4 tests/test-fb-hgext-cstore-treemanifest.py:146:5: F811 redefinition of unused 'testDeeplyNested' from line 134 tests/test-fb-hgext-extutil.py:46:5: F811 redefinition of unused 'testbgcommandfailure' from line 37 tests/test_hgsubversion_util.py:47:1: F811 redefinition of unused 'svnwrap' from line 31 (Python 2) tests/test_hgsubversion_util.py:49:1: F811 redefinition of unused 'svnwrap' from line 47 (Python 2) ``` Reviewed By: ryanmce Differential Revision: D6934533 fbshipit-source-id: 8b51851a76fec88bb59107ed05a901d42c7326f8
2018-02-10 04:31:41 +03:00
def testbgcommandfailure2(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)
def testflock(self):
testtmp = os.environ["TESTTMP"]
opener = vfs.vfs(testtmp)
name = 'testlock'
with extutil.flock(opener.join(name), 'testing a lock',
timeout=0):
otherlock = self.otherprocesslock(opener, name)
self.assertEquals(otherlock, locktimeout,
"other process should not have taken the lock")
otherlock = self.otherprocesslock(opener, name)
self.assertEquals(otherlock, locksuccess,
"other process should have taken the lock")
def otherprocesslock(self, opener, name):
pid = os.fork()
if pid == 0:
try:
with extutil.flock(opener.join(name), 'other process lock',
timeout=0):
os._exit(locksuccess)
except error.LockHeld:
os._exit(locktimeout)
else:
p, st = os.waitpid(pid, 0)
st = worker._exitstatus(st) # Convert back to an int
return st
if __name__ == '__main__':
silenttestrunner.main(__name__)