def _run()

in pylib/mercurial-support/run-tests.py [0:0]


    def _run(self, testdescs):
        testdir = getcwdb()
        self._testdir = osenvironb[b'TESTDIR'] = getcwdb()
        # assume all tests in same folder for now
        if testdescs:
            pathname = os.path.dirname(testdescs[0]['path'])
            if pathname:
                # TRACKING MOZ
                # osenvironb[b'TESTDIR'] = os.path.join(osenvironb[b'TESTDIR'],
                #                                      pathname)
                pass

        if self.options.outputdir:
            self._outputdir = canonpath(_bytespath(self.options.outputdir))
        else:
            self._outputdir = getcwdb()
            if testdescs and pathname:
                self._outputdir = os.path.join(self._outputdir, pathname)
        previoustimes = {}
        if self.options.order_by_runtime:
            previoustimes = dict(loadtimes(self._outputdir))
        sorttests(testdescs, previoustimes, shuffle=self.options.random)

        if 'PYTHONHASHSEED' not in os.environ:
            # use a random python hash seed all the time
            # we do the randomness ourself to know what seed is used
            os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))

        if self.options.tmpdir:
            self.options.keep_tmpdir = True
            tmpdir = _bytespath(self.options.tmpdir)
            if os.path.exists(tmpdir):
                # Meaning of tmpdir has changed since 1.3: we used to create
                # HGTMP inside tmpdir; now HGTMP is tmpdir.  So fail if
                # tmpdir already exists.
                print("error: temp dir %r already exists" % tmpdir)
                return 1

            os.makedirs(tmpdir)
        else:
            d = None
            if os.name == 'nt':
                # without this, we get the default temp dir location, but
                # in all lowercase, which causes troubles with paths (issue3490)
                d = osenvironb.get(b'TMP', None)
            tmpdir = tempfile.mkdtemp(b'', b'hgtests.', d)

        self._hgtmp = osenvironb[b'HGTMP'] = os.path.realpath(tmpdir)

        if self.options.with_hg:
            self._installdir = None
            whg = self.options.with_hg
            self._bindir = os.path.dirname(os.path.realpath(whg))
            assert isinstance(self._bindir, bytes)
            self._hgcommand = os.path.basename(whg)
            self._tmpbindir = os.path.join(self._hgtmp, b'install', b'bin')
            os.makedirs(self._tmpbindir)

            normbin = os.path.normpath(os.path.abspath(whg))
            normbin = normbin.replace(os.sep.encode('ascii'), b'/')

            # Other Python scripts in the test harness need to
            # `import mercurial`. If `hg` is a Python script, we assume
            # the Mercurial modules are relative to its path and tell the tests
            # to load Python modules from its directory.
            with open(whg, 'rb') as fh:
                initial = fh.read(1024)

            if re.match(b'#!.*python', initial):
                self._pythondir = self._bindir
            # If it looks like our in-repo Rust binary, use the source root.
            # This is a bit hacky. But rhg is still not supported outside the
            # source directory. So until it is, do the simple thing.
            elif re.search(b'/rust/target/[^/]+/hg', normbin):
                self._pythondir = os.path.dirname(self._testdir)
            # Fall back to the legacy behavior.
            else:
                self._pythondir = self._bindir

        else:
            self._installdir = os.path.join(self._hgtmp, b"install")
            self._bindir = os.path.join(self._installdir, b"bin")
            self._hgcommand = b'hg'
            self._tmpbindir = self._bindir
            self._pythondir = os.path.join(self._installdir, b"lib", b"python")

        # Force the use of hg.exe instead of relying on MSYS to recognize hg is
        # a python script and feed it to python.exe.  Legacy stdio is force
        # enabled by hg.exe, and this is a more realistic way to launch hg
        # anyway.
        if os.name == 'nt' and not self._hgcommand.endswith(b'.exe'):
            self._hgcommand += b'.exe'

        # set CHGHG, then replace "hg" command by "chg"
        chgbindir = self._bindir
        if self.options.chg or self.options.with_chg:
            osenvironb[b'CHGHG'] = os.path.join(self._bindir, self._hgcommand)
        else:
            osenvironb.pop(b'CHGHG', None)  # drop flag for hghave
        if self.options.chg:
            self._hgcommand = b'chg'
        elif self.options.with_chg:
            chgbindir = os.path.dirname(os.path.realpath(self.options.with_chg))
            self._hgcommand = os.path.basename(self.options.with_chg)

        osenvironb[b"BINDIR"] = self._bindir
        osenvironb[b"PYTHON"] = PYTHON

        fileb = _bytespath(__file__)
        runtestdir = os.path.abspath(os.path.dirname(fileb))
        osenvironb[b'RUNTESTDIR'] = runtestdir
        if PYTHON3:
            sepb = _bytespath(os.pathsep)
        else:
            sepb = os.pathsep
        path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb)
        if os.path.islink(__file__):
            # test helper will likely be at the end of the symlink
            realfile = os.path.realpath(fileb)
            realdir = os.path.abspath(os.path.dirname(realfile))
            path.insert(2, realdir)
        if chgbindir != self._bindir:
            path.insert(1, chgbindir)
        if self._testdir != runtestdir:
            path = [self._testdir] + path
        if self._tmpbindir != self._bindir:
            path = [self._tmpbindir] + path
        osenvironb[b"PATH"] = sepb.join(path)

        # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
        # can run .../tests/run-tests.py test-foo where test-foo
        # adds an extension to HGRC. Also include run-test.py directory to
        # import modules like heredoctest.
        pypath = [self._pythondir, self._testdir, runtestdir]
        # We have to augment PYTHONPATH, rather than simply replacing
        # it, in case external libraries are only available via current
        # PYTHONPATH.  (In particular, the Subversion bindings on OS X
        # are in /opt/subversion.)
        oldpypath = osenvironb.get(IMPL_PATH)
        if oldpypath:
            pypath.append(oldpypath)
        osenvironb[IMPL_PATH] = sepb.join(pypath)

        if self.options.pure:
            os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure"
            os.environ["HGMODULEPOLICY"] = "py"

        if self.options.allow_slow_tests:
            os.environ["HGTEST_SLOW"] = "slow"
        elif 'HGTEST_SLOW' in os.environ:
            del os.environ['HGTEST_SLOW']

        self._coveragefile = os.path.join(self._testdir, b'.coverage')

        if self.options.exceptions:
            exceptionsdir = os.path.join(self._outputdir, b'exceptions')
            try:
                os.makedirs(exceptionsdir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

            # Remove all existing exception reports.
            for f in os.listdir(exceptionsdir):
                os.unlink(os.path.join(exceptionsdir, f))

            osenvironb[b'HGEXCEPTIONSDIR'] = exceptionsdir
            logexceptions = os.path.join(self._testdir, b'logexceptions.py')
            self.options.extra_config_opt.append(
                'extensions.logexceptions=%s' % logexceptions.decode('utf-8')
            )

        vlog("# Using TESTDIR", _strpath(self._testdir))
        vlog("# Using RUNTESTDIR", _strpath(osenvironb[b'RUNTESTDIR']))
        vlog("# Using HGTMP", _strpath(self._hgtmp))
        vlog("# Using PATH", os.environ["PATH"])
        vlog(
            "# Using", _strpath(IMPL_PATH), _strpath(osenvironb[IMPL_PATH]),
        )
        vlog("# Writing to directory", _strpath(self._outputdir))

        try:
            return self._runtests(testdescs) or 0
        finally:
            time.sleep(0.1)
            self._cleanup()