Exemple #1
0
    def virtualenv(
        self,
        python_executable: str = sys.executable
    ) -> Generator[Tuple[str, str], None, None]:
        """Context manager that creates a virtualenv in a temporary directory

        returns the path to the created Python executable"""
        # Sadly, we need virtualenv, as the Python 3 venv module does not support creating a venv
        # for Python 2, and Python 2 does not have its own venv.
        with tempfile.TemporaryDirectory() as venv_dir:
            returncode, lines = run_command([
                sys.executable, '-m', 'virtualenv',
                '-p{}'.format(python_executable), venv_dir
            ],
                                            cwd=os.getcwd())
            if returncode != 0:
                err = '\n'.join(lines)
                self.fail(
                    "Failed to create venv. Do you have virtualenv installed?\n"
                    + err)
            if sys.platform == 'win32':
                yield venv_dir, os.path.abspath(
                    os.path.join(venv_dir, 'Scripts', 'python'))
            else:
                yield venv_dir, os.path.abspath(
                    os.path.join(venv_dir, 'bin', 'python'))
Exemple #2
0
def test_python_evaluation(testcase: DataDrivenTestCase,
                           cache_dir: str) -> None:
    """Runs Mypy in a subprocess.

    If this passes without errors, executes the script again with a given Python
    version.
    """
    assert testcase.old_cwd is not None, "test was not properly set up"
    # TODO: Enable strict optional for these tests
    mypy_cmdline = [
        '--show-traceback',
        '--no-site-packages',
        '--no-strict-optional',
        '--no-silence-site-packages',
    ]
    if testcase.name.lower().endswith('_newsemanal'):
        mypy_cmdline.append('--new-semantic-analyzer')
    py2 = testcase.name.lower().endswith('python2')
    if py2:
        mypy_cmdline.append('--py2')
        interpreter = try_find_python2_interpreter()
        if interpreter is None:
            # Skip, can't find a Python 2 interpreter.
            pytest.skip()
            # placate the type checker
            return
    else:
        interpreter = python3_path
        mypy_cmdline.append('--python-version={}'.format('.'.join(
            map(str, PYTHON3_VERSION))))

    # Write the program to a file.
    program = '_' + testcase.name + '.py'
    program_path = os.path.join(test_temp_dir, program)
    mypy_cmdline.append(program_path)
    with open(program_path, 'w', encoding='utf8') as file:
        for s in testcase.input:
            file.write('{}\n'.format(s))
    mypy_cmdline.append('--cache-dir={}'.format(cache_dir))
    output = []
    # Type check the program.
    out, err, returncode = api.run(mypy_cmdline)
    # split lines, remove newlines, and remove directory of test case
    for line in (out + err).splitlines():
        if line.startswith(test_temp_dir + os.sep):
            output.append(line[len(test_temp_dir + os.sep):].rstrip("\r\n"))
        else:
            output.append(line.rstrip("\r\n"))
    if returncode == 0:
        # Execute the program.
        returncode, interp_out = run_command([interpreter, program])
        output.extend(interp_out)
    # Remove temp file.
    os.remove(program_path)
    for i, line in enumerate(output):
        if os.path.sep + 'typeshed' + os.path.sep in line:
            output[i] = line.split(os.path.sep)[-1]
    assert_string_arrays_equal(
        adapt_output(testcase), output,
        'Invalid output ({}, line {})'.format(testcase.file, testcase.line))
Exemple #3
0
def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None:
    """Runs Mypy in a subprocess.

    If this passes without errors, executes the script again with a given Python
    version.
    """
    assert testcase.old_cwd is not None, "test was not properly set up"
    # TODO: Enable strict optional for these tests
    mypy_cmdline = [
        '--show-traceback',
        '--no-site-packages',
        '--no-strict-optional',
        '--no-silence-site-packages',
    ]
    if testcase.name.lower().endswith('_newsemanal'):
        mypy_cmdline.append('--new-semantic-analyzer')
    py2 = testcase.name.lower().endswith('python2')
    if py2:
        mypy_cmdline.append('--py2')
        interpreter = try_find_python2_interpreter()
        if interpreter is None:
            # Skip, can't find a Python 2 interpreter.
            pytest.skip()
            # placate the type checker
            return
    else:
        interpreter = python3_path
        mypy_cmdline.append('--python-version={}'.format('.'.join(map(str, PYTHON3_VERSION))))

    # Write the program to a file.
    program = '_' + testcase.name + '.py'
    program_path = os.path.join(test_temp_dir, program)
    mypy_cmdline.append(program_path)
    with open(program_path, 'w', encoding='utf8') as file:
        for s in testcase.input:
            file.write('{}\n'.format(s))
    mypy_cmdline.append('--cache-dir={}'.format(cache_dir))
    output = []
    # Type check the program.
    out, err, returncode = api.run(mypy_cmdline)
    # split lines, remove newlines, and remove directory of test case
    for line in (out + err).splitlines():
        if line.startswith(test_temp_dir + os.sep):
            output.append(line[len(test_temp_dir + os.sep):].rstrip("\r\n"))
        else:
            output.append(line.rstrip("\r\n"))
    if returncode == 0:
        # Execute the program.
        returncode, interp_out = run_command([interpreter, program])
        output.extend(interp_out)
    # Remove temp file.
    os.remove(program_path)
    for i, line in enumerate(output):
        if os.path.sep + 'typeshed' + os.path.sep in line:
            output[i] = line.split(os.path.sep)[-1]
    assert_string_arrays_equal(adapt_output(testcase), output,
                               'Invalid output ({}, line {})'.format(
                                   testcase.file, testcase.line))
Exemple #4
0
 def install_package(self,
                     pkg: str,
                     python_executable: str = sys.executable) -> None:
     """Context manager to temporarily install a package from test-data/packages/pkg/"""
     working_dir = os.path.join(package_path, pkg)
     install_cmd = [python_executable, '-m', 'pip', 'install', '.']
     returncode, lines = run_command(install_cmd, cwd=working_dir)
     if returncode != 0:
         self.fail('\n'.join(lines))
Exemple #5
0
    def virtualenv(
            self,
            python_executable: str = sys.executable
    ) -> Generator[str, None, None]:
        """Context manager that creates a virtualenv in a temporary directory

        returns the path to the created Python executable"""
        with tempfile.TemporaryDirectory() as venv_dir:
            run_command([
                sys.executable, '-m', 'virtualenv',
                '-p{}'.format(python_executable), venv_dir
            ],
                        cwd=os.getcwd())
            if sys.platform == 'win32':
                yield os.path.abspath(
                    os.path.join(venv_dir, 'Scripts', 'python'))
            else:
                yield os.path.abspath(os.path.join(venv_dir, 'bin', 'python'))
 def install_package(
         self,
         pkg: str,
         python_executable: str = sys.executable) -> Iterator[None]:
     """Context manager to temporarily install a package from test-data/packages/pkg/"""
     working_dir = os.path.join(package_path, pkg)
     install_cmd = [python_executable, '-m', 'pip', 'install', '.']
     # if we aren't in a virtualenv, install in the
     # user package directory so we don't need sudo
     if not is_in_venv() or python_executable != sys.executable:
         install_cmd.append('--user')
     returncode, lines = run_command(install_cmd, cwd=working_dir)
     if returncode != 0:
         self.fail('\n'.join(lines))
     try:
         yield
     finally:
         run_command(
             [python_executable, '-m', 'pip', 'uninstall', '-y', pkg],
             cwd=package_path)
Exemple #7
0
 def install_package(self, pkg: str,
                     python_executable: str = sys.executable,
                     use_pip: bool = True,
                     editable: bool = False) -> None:
     """Context manager to temporarily install a package from test-data/packages/pkg/"""
     working_dir = os.path.join(package_path, pkg)
     if use_pip:
         install_cmd = [python_executable, '-m', 'pip', 'install']
         if editable:
             install_cmd.append('-e')
         install_cmd.append('.')
     else:
         install_cmd = [python_executable, 'setup.py']
         if editable:
             install_cmd.append('develop')
         else:
             install_cmd.append('install')
     returncode, lines = run_command(install_cmd, cwd=working_dir)
     if returncode != 0:
         self.fail('\n'.join(lines))