Exemplo n.º 1
0
def test_checkout_python_script():
    with silence() as captured:
        assert checkout_python_script(_Venv(), "lib2to3", ["--help"])

    stdout, stderr = captured
    stdout.seek(0)
    assert stdout.read() == "=> lib2to3 [OK]\n"
Exemplo n.º 2
0
def test_run_python_script_failed():
    with silence() as captured:
        assert not checkout_python_script(_Venv(), "nothing")

    stdout, stderr = captured
    stdout.seek(0)
    assert stdout.read().endswith("[FAILED]\n")
Exemplo n.º 3
0
    def _run_tests(self, **kwargs):
        from pathlib import Path
        from mozperftest.runner import _setup_path
        from mozperftest.utils import (
            install_package,
            ON_TRY,
            checkout_script,
            checkout_python_script,
        )

        venv = self.virtualenv_manager
        skip_linters = kwargs.get("skip_linters", False)
        verbose = kwargs.get("verbose", False)

        # include in sys.path all deps
        _setup_path()
        try:
            import coverage  # noqa
        except ImportError:
            pydeps = Path(self.topsrcdir, "third_party", "python")
            vendors = ["coverage"]
            if not ON_TRY:
                vendors.append("attrs")

            # pip-installing dependencies that require compilation or special setup
            for dep in vendors:
                install_package(self.virtualenv_manager, str(Path(pydeps,
                                                                  dep)))

        if not ON_TRY and not skip_linters:
            cmd = "./mach lint "
            if verbose:
                cmd += " -v"
            cmd += " " + str(HERE)
            if not checkout_script(
                    cmd, label="linters", display=verbose, verbose=verbose):
                raise AssertionError("Please fix your code.")

        # running pytest with coverage
        # coverage is done in three steps:
        # 1/ coverage erase => erase any previous coverage data
        # 2/ coverage run pytest ... => run the tests and collect info
        # 3/ coverage report => generate the report
        tests_dir = Path(HERE, "tests").resolve()
        tests = kwargs.get("tests", [])
        if tests == []:
            tests = str(tests_dir)
            run_coverage_check = not skip_linters
        else:
            run_coverage_check = False

            def _get_test(test):
                if Path(test).exists():
                    return str(test)
                return str(tests_dir / test)

            tests = " ".join([_get_test(test) for test in tests])

        # on macOS + try we skip the coverage
        # because macOS workers prevent us from installing
        # packages from PyPI
        if sys.platform == "darwin" and ON_TRY:
            run_coverage_check = False

        import pytest

        options = "-xs"
        if kwargs.get("verbose"):
            options += "v"

        if run_coverage_check:
            assert checkout_python_script(venv,
                                          "coverage", ["erase"],
                                          label="remove old coverage data")
        args = [
            "run",
            pytest.__file__,
            options,
            "--duration",
            "10",
            tests,
        ]
        assert checkout_python_script(venv,
                                      "coverage",
                                      args,
                                      label="running tests",
                                      verbose=verbose)
        if run_coverage_check and not checkout_python_script(
                venv, "coverage", ["report"], display=True):
            raise ValueError("Coverage is too low!")