[PATCH 004 of 179 tests-refactor] run-tests: move createenv() into Test

Gregory Szorc gregory.szorc at gmail.com
Fri May 2 13:37:21 CDT 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1397938025 25200
#      Sat Apr 19 13:07:05 2014 -0700
# Branch stable
# Node ID 71520e4115ab64e38cc872ec04044f37c747ffcf
# Parent  b5da54671ef0204c48ce54715aa547cb1766fb7d
run-tests: move createenv() into Test

createenv() is consulting lots of test-specific variables. It makes
sense to attach it to the Test class.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -357,51 +357,16 @@ def createhgrc(path, options):
     if options.extra_config_opt:
         for opt in options.extra_config_opt:
             section, key = opt.split('.', 1)
             assert '=' in key, ('extra config opt %s must '
                                 'have an = for assignment' % opt)
             hgrc.write('[%s]\n%s\n' % (section, key))
     hgrc.close()
 
-def createenv(options, testtmp, threadtmp, port):
-    env = os.environ.copy()
-    env['TESTTMP'] = testtmp
-    env['HOME'] = testtmp
-    env["HGPORT"] = str(port)
-    env["HGPORT1"] = str(port + 1)
-    env["HGPORT2"] = str(port + 2)
-    env["HGRCPATH"] = os.path.join(threadtmp, '.hgrc')
-    env["DAEMON_PIDS"] = os.path.join(threadtmp, 'daemon.pids')
-    env["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
-    env["HGMERGE"] = "internal:merge"
-    env["HGUSER"]   = "test"
-    env["HGENCODING"] = "ascii"
-    env["HGENCODINGMODE"] = "strict"
-
-    # Reset some environment variables to well-known values so that
-    # the tests produce repeatable output.
-    env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
-    env['TZ'] = 'GMT'
-    env["EMAIL"] = "Foo Bar <foo.bar at example.com>"
-    env['COLUMNS'] = '80'
-    env['TERM'] = 'xterm'
-
-    for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
-              'NO_PROXY').split():
-        if k in env:
-            del env[k]
-
-    # unset env related to hooks
-    for k in env.keys():
-        if k.startswith('HG_'):
-            del env[k]
-
-    return env
-
 def checktools():
     # Before we go any further, check for pre-requisite tools
     # stuff from coreutils (cat, rm, etc) are not tested
     for p in requiredtools:
         if os.name == 'nt' and not p.endswith('.exe'):
             p += '.exe'
         found = findprogram(p)
         if found:
@@ -578,16 +543,17 @@ def outputcoverage(options):
         covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
 
 class Test(object):
     """Encapsulates a single, runnable test."""
 
     def __init__(self, path, options, threadtmp):
         self._path = path
         self._options = options
+        self._threadtmp = threadtmp
 
         self.testtmp = os.path.join(threadtmp, os.path.basename(path))
         os.mkdir(self.testtmp)
 
     def run(self, replacements, env):
         return self._run(replacements, env)
 
     def _run(self, replacements, env):
@@ -606,16 +572,51 @@ class Test(object):
                 (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
                     c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
                     for c in self.testtmp), '$TESTTMP'))
         else:
             r.append((re.escape(self.testtmp), '$TESTTMP'))
 
         return r, port
 
+    def getenv(self, port):
+        env = os.environ.copy()
+        env['TESTTMP'] = self.testtmp
+        env['HOME'] = self.testtmp
+        env["HGPORT"] = str(port)
+        env["HGPORT1"] = str(port + 1)
+        env["HGPORT2"] = str(port + 2)
+        env["HGRCPATH"] = os.path.join(self._threadtmp, '.hgrc')
+        env["DAEMON_PIDS"] = os.path.join(self._threadtmp, 'daemon.pids')
+        env["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
+        env["HGMERGE"] = "internal:merge"
+        env["HGUSER"]   = "test"
+        env["HGENCODING"] = "ascii"
+        env["HGENCODINGMODE"] = "strict"
+
+        # Reset some environment variables to well-known values so that
+        # the tests produce repeatable output.
+        env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
+        env['TZ'] = 'GMT'
+        env["EMAIL"] = "Foo Bar <foo.bar at example.com>"
+        env['COLUMNS'] = '80'
+        env['TERM'] = 'xterm'
+
+        for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
+                  'NO_PROXY').split():
+            if k in env:
+                del env[k]
+
+        # unset env related to hooks
+        for k in env.keys():
+            if k.startswith('HG_'):
+                del env[k]
+
+        return env
+
 def pytest(test, wd, options, replacements, env):
     py3kswitch = options.py3k_warnings and ' -3' or ''
     cmd = '%s%s "%s"' % (PYTHON, py3kswitch, test)
     vlog("# Running", cmd)
     if os.name == 'nt':
         replacements.append((r'\r\n', '\n'))
     return run(cmd, wd, options, replacements, env)
 
@@ -1011,17 +1012,17 @@ def runone(options, test, count):
         os.remove(err)       # Remove any previous output files
 
     # Make a tmp subdirectory to work in
     threadtmp = os.path.join(HGTMP, "child%d" % count)
     os.mkdir(threadtmp)
 
     t = runner(testpath, options, threadtmp)
     replacements, port = t.getreplacements(count)
-    env = createenv(options, t.testtmp, threadtmp, port)
+    env = t.getenv(port)
     createhgrc(env['HGRCPATH'], options)
 
     starttime = time.time()
     try:
         ret, out = t.run(replacements, env)
     except KeyboardInterrupt:
         endtime = time.time()
         log('INTERRUPTED: %s (after %d seconds)' % (test, endtime - starttime))


More information about the Mercurial-devel mailing list