##// END OF EJS Templates
tests: rename _bytespath() to _sys2bytes() and _strpath() to _sys2str()...
Manuel Jacob -
r44935:55c443fc default
parent child Browse files
Show More
@@ -30,7 +30,7 b' if sys.version_info > (3, 5, 0):'
30 30 PYTHON3 = True
31 31 xrange = range # we use xrange in one place, and we'd rather not use range
32 32
33 def _bytespath(p):
33 def _sys2bytes(p):
34 34 return p.encode('utf-8')
35 35
36 36
@@ -47,7 +47,7 b' else:'
47 47 # bytestrings by default, so we don't have to do any extra
48 48 # fiddling there. We define the wrapper functions anyway just to
49 49 # help keep code consistent between platforms.
50 def _bytespath(p):
50 def _sys2bytes(p):
51 51 return p
52 52
53 53
@@ -107,7 +107,7 b' def watchman(args):'
107 107 ]
108 108
109 109 envb = osenvironb.copy()
110 envb[b'WATCHMAN_CONFIG_FILE'] = _bytespath(cfgfile)
110 envb[b'WATCHMAN_CONFIG_FILE'] = _sys2bytes(cfgfile)
111 111 with open(clilogfile, 'wb') as f:
112 112 proc = subprocess.Popen(
113 113 argv, env=envb, stdin=None, stdout=f, stderr=f
@@ -129,7 +129,7 b' def run():'
129 129 args, runtestsargv = parser.parse_known_args()
130 130
131 131 with watchman(args) as sockfile:
132 osenvironb[b'WATCHMAN_SOCK'] = _bytespath(sockfile)
132 osenvironb[b'WATCHMAN_SOCK'] = _sys2bytes(sockfile)
133 133 # Indicate to hghave that we're running with fsmonitor enabled.
134 134 osenvironb[b'HGFSMONITOR_TESTS'] = b'1'
135 135
@@ -29,12 +29,12 b" stderr = getattr(sys.stderr, 'buffer', s"
29 29
30 30 if sys.version_info[0] >= 3:
31 31
32 def _bytespath(p):
32 def _sys2bytes(p):
33 33 if p is None:
34 34 return p
35 35 return p.encode('utf-8')
36 36
37 def _strpath(p):
37 def _bytes2sys(p):
38 38 if p is None:
39 39 return p
40 40 return p.decode('utf-8')
@@ -42,10 +42,10 b' if sys.version_info[0] >= 3:'
42 42
43 43 else:
44 44
45 def _bytespath(p):
45 def _sys2bytes(p):
46 46 return p
47 47
48 _strpath = _bytespath
48 _bytes2sys = _sys2bytes
49 49
50 50
51 51 def check(name, desc):
@@ -461,7 +461,7 b' def has_hardlink():'
461 461 os.close(fh)
462 462 name = tempfile.mktemp(dir='.', prefix=tempprefix)
463 463 try:
464 util.oslink(_bytespath(fn), _bytespath(name))
464 util.oslink(_sys2bytes(fn), _sys2bytes(name))
465 465 os.unlink(name)
466 466 return True
467 467 except OSError:
@@ -1032,7 +1032,7 b' def has_black():'
1032 1032 version_regex = b'black, version ([0-9a-b.]+)'
1033 1033 version = matchoutput(blackcmd, version_regex)
1034 1034 sv = distutils.version.StrictVersion
1035 return version and sv(_strpath(version.group(1))) >= sv('19.10b0')
1035 return version and sv(_bytes2sys(version.group(1))) >= sv('19.10b0')
1036 1036
1037 1037
1038 1038 @check('pytype', 'the pytype type checker')
@@ -1040,7 +1040,7 b' def has_pytype():'
1040 1040 pytypecmd = 'pytype --version'
1041 1041 version = matchoutput(pytypecmd, b'[0-9a-b.]+')
1042 1042 sv = distutils.version.StrictVersion
1043 return version and sv(_strpath(version.group(0))) >= sv('2019.10.17')
1043 return version and sv(_bytes2sys(version.group(0))) >= sv('2019.10.17')
1044 1044
1045 1045
1046 1046 @check("rustfmt", "rustfmt tool")
@@ -143,12 +143,12 b' if sys.version_info > (3, 5, 0):'
143 143 PYTHON3 = True
144 144 xrange = range # we use xrange in one place, and we'd rather not use range
145 145
146 def _bytespath(p):
146 def _sys2bytes(p):
147 147 if p is None:
148 148 return p
149 149 return p.encode('utf-8')
150 150
151 def _strpath(p):
151 def _bytes2sys(p):
152 152 if p is None:
153 153 return p
154 154 return p.decode('utf-8')
@@ -165,34 +165,34 b' if sys.version_info > (3, 5, 0):'
165 165 self._strenv = strenv
166 166
167 167 def __getitem__(self, k):
168 v = self._strenv.__getitem__(_strpath(k))
169 return _bytespath(v)
168 v = self._strenv.__getitem__(_bytes2sys(k))
169 return _sys2bytes(v)
170 170
171 171 def __setitem__(self, k, v):
172 self._strenv.__setitem__(_strpath(k), _strpath(v))
172 self._strenv.__setitem__(_bytes2sys(k), _bytes2sys(v))
173 173
174 174 def __delitem__(self, k):
175 self._strenv.__delitem__(_strpath(k))
175 self._strenv.__delitem__(_bytes2sys(k))
176 176
177 177 def __contains__(self, k):
178 return self._strenv.__contains__(_strpath(k))
178 return self._strenv.__contains__(_bytes2sys(k))
179 179
180 180 def __iter__(self):
181 return iter([_bytespath(k) for k in iter(self._strenv)])
181 return iter([_sys2bytes(k) for k in iter(self._strenv)])
182 182
183 183 def get(self, k, default=None):
184 v = self._strenv.get(_strpath(k), _strpath(default))
185 return _bytespath(v)
184 v = self._strenv.get(_bytes2sys(k), _bytes2sys(default))
185 return _sys2bytes(v)
186 186
187 187 def pop(self, k, default=None):
188 v = self._strenv.pop(_strpath(k), _strpath(default))
189 return _bytespath(v)
188 v = self._strenv.pop(_bytes2sys(k), _bytes2sys(default))
189 return _sys2bytes(v)
190 190
191 191 osenvironb = environbytes(os.environ)
192 192
193 193 getcwdb = getattr(os, 'getcwdb')
194 194 if not getcwdb or os.name == 'nt':
195 getcwdb = lambda: _bytespath(os.getcwd())
195 getcwdb = lambda: _sys2bytes(os.getcwd())
196 196
197 197 elif sys.version_info >= (3, 0, 0):
198 198 print(
@@ -207,10 +207,10 b' else:'
207 207 # bytestrings by default, so we don't have to do any extra
208 208 # fiddling there. We define the wrapper functions anyway just to
209 209 # help keep code consistent between platforms.
210 def _bytespath(p):
210 def _sys2bytes(p):
211 211 return p
212 212
213 _strpath = _bytespath
213 _bytes2sys = _sys2bytes
214 214 osenvironb = os.environ
215 215 getcwdb = os.getcwd
216 216
@@ -274,10 +274,10 b" closefds = os.name == 'posix'"
274 274 def Popen4(cmd, wd, timeout, env=None):
275 275 processlock.acquire()
276 276 p = subprocess.Popen(
277 _strpath(cmd),
277 _bytes2sys(cmd),
278 278 shell=True,
279 279 bufsize=-1,
280 cwd=_strpath(wd),
280 cwd=_bytes2sys(wd),
281 281 env=env,
282 282 close_fds=closefds,
283 283 stdin=subprocess.PIPE,
@@ -315,7 +315,7 b" elif os.environ.get('PYTHON'):"
315 315 else:
316 316 raise AssertionError('Could not find Python interpreter')
317 317
318 PYTHON = _bytespath(sysexecutable.replace('\\', '/'))
318 PYTHON = _sys2bytes(sysexecutable.replace('\\', '/'))
319 319 IMPL_PATH = b'PYTHONPATH'
320 320 if 'java' in sys.platform:
321 321 IMPL_PATH = b'JYTHONPATH'
@@ -640,7 +640,7 b' def parseargs(args, parser):'
640 640 if options.local:
641 641 if options.with_hg or options.with_chg:
642 642 parser.error('--local cannot be used with --with-hg or --with-chg')
643 testdir = os.path.dirname(_bytespath(canonpath(sys.argv[0])))
643 testdir = os.path.dirname(_sys2bytes(canonpath(sys.argv[0])))
644 644 reporootdir = os.path.dirname(testdir)
645 645 pathandattrs = [(b'hg', 'with_hg')]
646 646 if options.chg:
@@ -652,10 +652,10 b' def parseargs(args, parser):'
652 652 '--local specified, but %r not found or '
653 653 'not executable' % binpath
654 654 )
655 setattr(options, attr, _strpath(binpath))
655 setattr(options, attr, _bytes2sys(binpath))
656 656
657 657 if options.with_hg:
658 options.with_hg = canonpath(_bytespath(options.with_hg))
658 options.with_hg = canonpath(_sys2bytes(options.with_hg))
659 659 if not (
660 660 os.path.isfile(options.with_hg)
661 661 and os.access(options.with_hg, os.X_OK)
@@ -669,7 +669,7 b' def parseargs(args, parser):'
669 669 parser.error('chg does not work on %s' % os.name)
670 670 if options.with_chg:
671 671 options.chg = False # no installation to temporary location
672 options.with_chg = canonpath(_bytespath(options.with_chg))
672 options.with_chg = canonpath(_sys2bytes(options.with_chg))
673 673 if not (
674 674 os.path.isfile(options.with_chg)
675 675 and os.access(options.with_chg, os.X_OK)
@@ -942,7 +942,7 b' class Test(unittest.TestCase):'
942 942 slowtimeout = defaults['slowtimeout']
943 943 self.path = path
944 944 self.bname = os.path.basename(path)
945 self.name = _strpath(self.bname)
945 self.name = _bytes2sys(self.bname)
946 946 self._testdir = os.path.dirname(path)
947 947 self._outputdir = outputdir
948 948 self._tmpname = os.path.basename(path)
@@ -956,7 +956,7 b' class Test(unittest.TestCase):'
956 956 self._slowtimeout = slowtimeout
957 957 self._startport = startport
958 958 self._extraconfigopts = extraconfigopts or []
959 self._shell = _bytespath(shell)
959 self._shell = _sys2bytes(shell)
960 960 self._hgcommand = hgcommand or b'hg'
961 961 self._usechg = usechg
962 962 self._useipv6 = useipv6
@@ -1268,7 +1268,7 b' class Test(unittest.TestCase):'
1268 1268 environment."""
1269 1269 # Put the restoreenv script inside self._threadtmp
1270 1270 scriptpath = os.path.join(self._threadtmp, b'restoreenv.sh')
1271 testenv['HGTEST_RESTOREENV'] = _strpath(scriptpath)
1271 testenv['HGTEST_RESTOREENV'] = _bytes2sys(scriptpath)
1272 1272
1273 1273 # Only restore environment variable names that the shell allows
1274 1274 # us to export.
@@ -1302,15 +1302,15 b' class Test(unittest.TestCase):'
1302 1302 env = os.environ.copy()
1303 1303 env['PYTHONUSERBASE'] = sysconfig.get_config_var('userbase') or ''
1304 1304 env['HGEMITWARNINGS'] = '1'
1305 env['TESTTMP'] = _strpath(self._testtmp)
1305 env['TESTTMP'] = _bytes2sys(self._testtmp)
1306 1306 env['TESTNAME'] = self.name
1307 env['HOME'] = _strpath(self._testtmp)
1307 env['HOME'] = _bytes2sys(self._testtmp)
1308 1308 # This number should match portneeded in _getport
1309 1309 for port in xrange(3):
1310 1310 # This list should be parallel to _portmap in _getreplacements
1311 1311 defineport(port)
1312 env["HGRCPATH"] = _strpath(os.path.join(self._threadtmp, b'.hgrc'))
1313 env["DAEMON_PIDS"] = _strpath(
1312 env["HGRCPATH"] = _bytes2sys(os.path.join(self._threadtmp, b'.hgrc'))
1313 env["DAEMON_PIDS"] = _bytes2sys(
1314 1314 os.path.join(self._threadtmp, b'daemon.pids')
1315 1315 )
1316 1316 env["HGEDITOR"] = (
@@ -1342,7 +1342,7 b' class Test(unittest.TestCase):'
1342 1342
1343 1343 # LOCALIP could be ::1 or 127.0.0.1. Useful for tests that require raw
1344 1344 # IP addresses.
1345 env['LOCALIP'] = _strpath(self._localip())
1345 env['LOCALIP'] = _bytes2sys(self._localip())
1346 1346
1347 1347 # This has the same effect as Py_LegacyWindowsStdioFlag in exewrapper.c,
1348 1348 # but this is needed for testing python instances like dummyssh,
@@ -1451,7 +1451,10 b' class Test(unittest.TestCase):'
1451 1451 """
1452 1452 if self._debug:
1453 1453 proc = subprocess.Popen(
1454 _strpath(cmd), shell=True, cwd=_strpath(self._testtmp), env=env
1454 _bytes2sys(cmd),
1455 shell=True,
1456 cwd=_bytes2sys(self._testtmp),
1457 env=env,
1455 1458 )
1456 1459 ret = proc.wait()
1457 1460 return (ret, None)
@@ -1561,7 +1564,7 b' class TTest(Test):'
1561 1564 super(TTest, self).__init__(path, *args, **kwds)
1562 1565 if case:
1563 1566 casepath = b'#'.join(case)
1564 self.name = '%s#%s' % (self.name, _strpath(casepath))
1567 self.name = '%s#%s' % (self.name, _bytes2sys(casepath))
1565 1568 self.errpath = b'%s#%s.err' % (self.errpath[:-4], casepath)
1566 1569 self._tmpname += b'-%s' % casepath
1567 1570 self._have = {}
@@ -1612,7 +1615,7 b' class TTest(Test):'
1612 1615 return self._have.get(allreqs)
1613 1616
1614 1617 # TODO do something smarter when all other uses of hghave are gone.
1615 runtestdir = os.path.abspath(os.path.dirname(_bytespath(__file__)))
1618 runtestdir = os.path.abspath(os.path.dirname(_sys2bytes(__file__)))
1616 1619 tdir = runtestdir.replace(b'\\', b'/')
1617 1620 proc = Popen4(
1618 1621 b'%s -c "%s/hghave %s"' % (self._shell, tdir, allreqs),
@@ -2199,7 +2202,7 b' class TestResult(unittest._TextTestResul'
2199 2202 v = self._options.view
2200 2203 subprocess.call(
2201 2204 r'"%s" "%s" "%s"'
2202 % (v, _strpath(test.refpath), _strpath(test.errpath)),
2205 % (v, _bytes2sys(test.refpath), _bytes2sys(test.errpath)),
2203 2206 shell=True,
2204 2207 )
2205 2208 else:
@@ -2688,7 +2691,7 b' class TextTestRunner(unittest.TextTestRu'
2688 2691 opts = ''
2689 2692 withhg = self._runner.options.with_hg
2690 2693 if withhg:
2691 opts += ' --with-hg=%s ' % shellquote(_strpath(withhg))
2694 opts += ' --with-hg=%s ' % shellquote(_bytes2sys(withhg))
2692 2695 rtc = '%s %s %s %s' % (sysexecutable, sys.argv[0], opts, test)
2693 2696 data = pread(bisectcmd + ['--command', rtc])
2694 2697 m = re.search(
@@ -2930,7 +2933,7 b' class TestRunner(object):'
2930 2933 try:
2931 2934 parser = parser or getparser()
2932 2935 options = parseargs(args, parser)
2933 tests = [_bytespath(a) for a in options.tests]
2936 tests = [_sys2bytes(a) for a in options.tests]
2934 2937 if options.test_list is not None:
2935 2938 for listfile in options.test_list:
2936 2939 with open(listfile, 'rb') as f:
@@ -2962,7 +2965,7 b' class TestRunner(object):'
2962 2965 testdir = os.path.join(testdir, pathname)
2963 2966 self._testdir = osenvironb[b'TESTDIR'] = testdir
2964 2967 if self.options.outputdir:
2965 self._outputdir = canonpath(_bytespath(self.options.outputdir))
2968 self._outputdir = canonpath(_sys2bytes(self.options.outputdir))
2966 2969 else:
2967 2970 self._outputdir = getcwdb()
2968 2971 if testdescs and pathname:
@@ -2979,7 +2982,7 b' class TestRunner(object):'
2979 2982
2980 2983 if self.options.tmpdir:
2981 2984 self.options.keep_tmpdir = True
2982 tmpdir = _bytespath(self.options.tmpdir)
2985 tmpdir = _sys2bytes(self.options.tmpdir)
2983 2986 if os.path.exists(tmpdir):
2984 2987 # Meaning of tmpdir has changed since 1.3: we used to create
2985 2988 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
@@ -3008,7 +3011,7 b' class TestRunner(object):'
3008 3011 os.makedirs(self._tmpbindir)
3009 3012
3010 3013 normbin = os.path.normpath(os.path.abspath(whg))
3011 normbin = normbin.replace(_bytespath(os.sep), b'/')
3014 normbin = normbin.replace(_sys2bytes(os.sep), b'/')
3012 3015
3013 3016 # Other Python scripts in the test harness need to
3014 3017 # `import mercurial`. If `hg` is a Python script, we assume
@@ -3057,11 +3060,11 b' class TestRunner(object):'
3057 3060 osenvironb[b"BINDIR"] = self._bindir
3058 3061 osenvironb[b"PYTHON"] = PYTHON
3059 3062
3060 fileb = _bytespath(__file__)
3063 fileb = _sys2bytes(__file__)
3061 3064 runtestdir = os.path.abspath(os.path.dirname(fileb))
3062 3065 osenvironb[b'RUNTESTDIR'] = runtestdir
3063 3066 if PYTHON3:
3064 sepb = _bytespath(os.pathsep)
3067 sepb = _sys2bytes(os.pathsep)
3065 3068 else:
3066 3069 sepb = os.pathsep
3067 3070 path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb)
@@ -3121,14 +3124,14 b' class TestRunner(object):'
3121 3124 'extensions.logexceptions=%s' % logexceptions.decode('utf-8')
3122 3125 )
3123 3126
3124 vlog("# Using TESTDIR", _strpath(self._testdir))
3125 vlog("# Using RUNTESTDIR", _strpath(osenvironb[b'RUNTESTDIR']))
3126 vlog("# Using HGTMP", _strpath(self._hgtmp))
3127 vlog("# Using TESTDIR", _bytes2sys(self._testdir))
3128 vlog("# Using RUNTESTDIR", _bytes2sys(osenvironb[b'RUNTESTDIR']))
3129 vlog("# Using HGTMP", _bytes2sys(self._hgtmp))
3127 3130 vlog("# Using PATH", os.environ["PATH"])
3128 3131 vlog(
3129 "# Using", _strpath(IMPL_PATH), _strpath(osenvironb[IMPL_PATH]),
3132 "# Using", _bytes2sys(IMPL_PATH), _bytes2sys(osenvironb[IMPL_PATH]),
3130 3133 )
3131 vlog("# Writing to directory", _strpath(self._outputdir))
3134 vlog("# Writing to directory", _bytes2sys(self._outputdir))
3132 3135
3133 3136 try:
3134 3137 return self._runtests(testdescs) or 0
@@ -3146,7 +3149,7 b' class TestRunner(object):'
3146 3149 if self.options.changed:
3147 3150 proc = Popen4(
3148 3151 b'hg st --rev "%s" -man0 .'
3149 % _bytespath(self.options.changed),
3152 % _sys2bytes(self.options.changed),
3150 3153 None,
3151 3154 0,
3152 3155 )
@@ -3366,7 +3369,7 b' class TestRunner(object):'
3366 3369 if self.options.keep_tmpdir:
3367 3370 return
3368 3371
3369 vlog("# Cleaning up HGTMP", _strpath(self._hgtmp))
3372 vlog("# Cleaning up HGTMP", _bytes2sys(self._hgtmp))
3370 3373 shutil.rmtree(self._hgtmp, True)
3371 3374 for f in self._createdfiles:
3372 3375 try:
@@ -3434,9 +3437,9 b' class TestRunner(object):'
3434 3437 script = os.path.realpath(sys.argv[0])
3435 3438 exe = sysexecutable
3436 3439 if PYTHON3:
3437 compiler = _bytespath(compiler)
3438 script = _bytespath(script)
3439 exe = _bytespath(exe)
3440 compiler = _sys2bytes(compiler)
3441 script = _sys2bytes(script)
3442 exe = _sys2bytes(exe)
3440 3443 hgroot = os.path.dirname(os.path.dirname(script))
3441 3444 self._hgroot = hgroot
3442 3445 os.chdir(hgroot)
@@ -3478,7 +3481,7 b' class TestRunner(object):'
3478 3481 makedirs(self._bindir)
3479 3482
3480 3483 vlog("# Running", cmd.decode("utf-8"))
3481 if subprocess.call(_strpath(cmd), shell=True) == 0:
3484 if subprocess.call(_bytes2sys(cmd), shell=True) == 0:
3482 3485 if not self.options.verbose:
3483 3486 try:
3484 3487 os.remove(installerrs)
@@ -3558,7 +3561,7 b' class TestRunner(object):'
3558 3561 cmd = b'"%s" -c "import mercurial; print (mercurial.__path__[0])"'
3559 3562 cmd = cmd % PYTHON
3560 3563 if PYTHON3:
3561 cmd = _strpath(cmd)
3564 cmd = _bytes2sys(cmd)
3562 3565
3563 3566 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
3564 3567 out, err = p.communicate()
@@ -3604,33 +3607,33 b' class TestRunner(object):'
3604 3607 # chdir is the easiest way to get short, relative paths in the
3605 3608 # output.
3606 3609 os.chdir(self._hgroot)
3607 covdir = os.path.join(_strpath(self._installdir), '..', 'coverage')
3610 covdir = os.path.join(_bytes2sys(self._installdir), '..', 'coverage')
3608 3611 cov = coverage(data_file=os.path.join(covdir, 'cov'))
3609 3612
3610 3613 # Map install directory paths back to source directory.
3611 cov.config.paths['srcdir'] = ['.', _strpath(self._pythondir)]
3614 cov.config.paths['srcdir'] = ['.', _bytes2sys(self._pythondir)]
3612 3615
3613 3616 cov.combine()
3614 3617
3615 3618 omit = [
3616 _strpath(os.path.join(x, b'*'))
3619 _bytes2sys(os.path.join(x, b'*'))
3617 3620 for x in [self._bindir, self._testdir]
3618 3621 ]
3619 3622 cov.report(ignore_errors=True, omit=omit)
3620 3623
3621 3624 if self.options.htmlcov:
3622 htmldir = os.path.join(_strpath(self._outputdir), 'htmlcov')
3625 htmldir = os.path.join(_bytes2sys(self._outputdir), 'htmlcov')
3623 3626 cov.html_report(directory=htmldir, omit=omit)
3624 3627 if self.options.annotate:
3625 adir = os.path.join(_strpath(self._outputdir), 'annotated')
3628 adir = os.path.join(_bytes2sys(self._outputdir), 'annotated')
3626 3629 if not os.path.isdir(adir):
3627 3630 os.mkdir(adir)
3628 3631 cov.annotate(directory=adir, omit=omit)
3629 3632
3630 3633 def _findprogram(self, program):
3631 3634 """Search PATH for a executable program"""
3632 dpb = _bytespath(os.defpath)
3633 sepb = _bytespath(os.pathsep)
3635 dpb = _sys2bytes(os.defpath)
3636 sepb = _sys2bytes(os.pathsep)
3634 3637 for p in osenvironb.get(b'PATH', dpb).split(sepb):
3635 3638 name = os.path.join(p, program)
3636 3639 if os.name == 'nt' or os.access(name, os.X_OK):
@@ -3645,7 +3648,7 b' class TestRunner(object):'
3645 3648 found = self._findprogram(p)
3646 3649 p = p.decode("utf-8")
3647 3650 if found:
3648 vlog("# Found prerequisite", p, "at", _strpath(found))
3651 vlog("# Found prerequisite", p, "at", _bytes2sys(found))
3649 3652 else:
3650 3653 print("WARNING: Did not find prerequisite tool: %s " % p)
3651 3654
General Comments 0
You need to be logged in to leave comments. Login now