comparison tests/run-tests.py @ 33126:98e2c78e309c

tests: more completely restore the environment in syshgenv Update the syshgenv function to attempt to completely restore the original environment, rather than only updating a few specific variables. run_tests.py now generates a shell script that can be used to restore the original environment, and syshgenv sources it. This is a bit more complicated than the previous code, but should do a better job of running the system hg in the correct environment. I've tested it on Linux using python 2.x, but let me know if it causes issues in other environments. I'm not terribly familiar with how the tests get run on Windows, for instance, and how the environment needs to be updated there.
author Adam Simpkins <simpkins@fb.com>
date Wed, 28 Jun 2017 12:23:22 -0700
parents fa9a90d5ad89
children f458a6701983
comparison
equal deleted inserted replaced
33125:acfce52518c4 33126:98e2c78e309c
67 67
68 try: 68 try:
69 import Queue as queue 69 import Queue as queue
70 except ImportError: 70 except ImportError:
71 import queue 71 import queue
72
73 try:
74 import shlex
75 shellquote = shlex.quote
76 except (ImportError, AttributeError):
77 import pipes
78 shellquote = pipes.quote
72 79
73 if os.environ.get('RTUNICODEPEDANTRY', False): 80 if os.environ.get('RTUNICODEPEDANTRY', False):
74 try: 81 try:
75 reload(sys) 82 reload(sys)
76 sys.setdefaultencoding("undefined") 83 sys.setdefaultencoding("undefined")
750 """Run this test instance. 757 """Run this test instance.
751 758
752 This will return a tuple describing the result of the test. 759 This will return a tuple describing the result of the test.
753 """ 760 """
754 env = self._getenv() 761 env = self._getenv()
762 self._genrestoreenv(env)
755 self._daemonpids.append(env['DAEMON_PIDS']) 763 self._daemonpids.append(env['DAEMON_PIDS'])
756 self._createhgrc(env['HGRCPATH']) 764 self._createhgrc(env['HGRCPATH'])
757 765
758 vlog('# Test', self.name) 766 vlog('# Test', self.name)
759 767
885 def _localip(self): 893 def _localip(self):
886 if self._useipv6: 894 if self._useipv6:
887 return b'::1' 895 return b'::1'
888 else: 896 else:
889 return b'127.0.0.1' 897 return b'127.0.0.1'
898
899 def _genrestoreenv(self, testenv):
900 """Generate a script that can be used by tests to restore the original
901 environment."""
902 # Put the restoreenv script inside self._threadtmp
903 scriptpath = os.path.join(self._threadtmp, b'restoreenv.sh')
904 testenv['HGTEST_RESTOREENV'] = scriptpath
905
906 # Only restore environment variable names that the shell allows
907 # us to export.
908 name_regex = re.compile('[a-zA-Z][a-zA-Z0-9_]*')
909
910 with open(scriptpath, 'w') as envf:
911 for name, value in os.environ.items():
912 if not name_regex.match(name):
913 # Skip environment variables with unusual names not
914 # allowed by most shells.
915 continue
916 envf.write('%s=%s\n' % (name, shellquote(value)))
917
918 for name in testenv:
919 if name in os.environ:
920 continue
921 envf.write('unset %s\n' % (name,))
890 922
891 def _getenv(self): 923 def _getenv(self):
892 """Obtain environment variables to use during test execution.""" 924 """Obtain environment variables to use during test execution."""
893 def defineport(i): 925 def defineport(i):
894 offset = '' if i == 0 else '%s' % i 926 offset = '' if i == 0 else '%s' % i
2272 osenvironb[b'RUNTESTDIR'] = runtestdir 2304 osenvironb[b'RUNTESTDIR'] = runtestdir
2273 if PYTHON3: 2305 if PYTHON3:
2274 sepb = _bytespath(os.pathsep) 2306 sepb = _bytespath(os.pathsep)
2275 else: 2307 else:
2276 sepb = os.pathsep 2308 sepb = os.pathsep
2277 # save the original path, for tests that need to invoke the
2278 # system python
2279 osenvironb[b"ORIG_PATH"] = osenvironb[b"PATH"]
2280 path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb) 2309 path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb)
2281 if os.path.islink(__file__): 2310 if os.path.islink(__file__):
2282 # test helper will likely be at the end of the symlink 2311 # test helper will likely be at the end of the symlink
2283 realfile = os.path.realpath(fileb) 2312 realfile = os.path.realpath(fileb)
2284 realdir = os.path.abspath(os.path.dirname(realfile)) 2313 realdir = os.path.abspath(os.path.dirname(realfile))
2300 # it, in case external libraries are only available via current 2329 # it, in case external libraries are only available via current
2301 # PYTHONPATH. (In particular, the Subversion bindings on OS X 2330 # PYTHONPATH. (In particular, the Subversion bindings on OS X
2302 # are in /opt/subversion.) 2331 # are in /opt/subversion.)
2303 oldpypath = osenvironb.get(IMPL_PATH) 2332 oldpypath = osenvironb.get(IMPL_PATH)
2304 if oldpypath: 2333 if oldpypath:
2305 osenvironb['ORIG_' + IMPL_PATH] = oldpypath
2306 pypath.append(oldpypath) 2334 pypath.append(oldpypath)
2307 osenvironb[IMPL_PATH] = sepb.join(pypath) 2335 osenvironb[IMPL_PATH] = sepb.join(pypath)
2308 2336
2309 if self.options.pure: 2337 if self.options.pure:
2310 os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure" 2338 os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure"