示例#1
0
 def test_defaults(self):
     """
     Test the create function with default arguments.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     self.isdir(self.bindir)
     self.isdir(self.include)
     self.isdir(*self.lib)
     # Issue 21197
     p = self.get_env_file('lib64')
     conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and
                   (sys.platform != 'darwin'))
     if conditions:
         self.assertTrue(os.path.islink(p))
     else:
         self.assertFalse(os.path.exists(p))
     data = self.get_text_file_contents('pyvenv.cfg')
     executable = sys._base_executable
     path = os.path.dirname(executable)
     self.assertIn('home = %s' % path, data)
     self.assertIn('executable = %s' %
                   os.path.realpath(sys.executable), data)
     copies = '' if os.name=='nt' else ' --copies'
     cmd = f'command = {sys.executable} -m venv{copies} --without-pip {self.env_dir}'
     self.assertIn(cmd, data)
     fn = self.get_env_file(self.bindir, self.exe)
     if not os.path.exists(fn):  # diagnostics for Windows buildbot failures
         bd = self.get_env_file(self.bindir)
         print('Contents of %r:' % bd)
         print('    %r' % os.listdir(bd))
     self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
示例#2
0
    def test_realpath_cwd(self):
        ABSTFN = ntpath.abspath(os_helper.TESTFN)

        os_helper.unlink(ABSTFN)
        os_helper.rmtree(ABSTFN)
        os.mkdir(ABSTFN)
        self.addCleanup(os_helper.rmtree, ABSTFN)

        test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
        os.mkdir(test_dir_long)

        test_dir_short = _getshortpathname(test_dir_long)
        test_file_long = ntpath.join(test_dir_long, "file.txt")
        test_file_short = ntpath.join(test_dir_short, "file.txt")

        with open(test_file_long, "wb") as f:
            f.write(b"content")

        self.assertPathEqual(test_file_long, ntpath.realpath(test_file_short))

        with os_helper.change_cwd(test_dir_long):
            self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
        with os_helper.change_cwd(test_dir_long.lower()):
            self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
        with os_helper.change_cwd(test_dir_short):
            self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
示例#3
0
 def test_defaults_with_pathlib_path(self):
     """
     Test the create function with default arguments and a pathlib.Path path.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, pathlib.Path(self.env_dir))
     self._check_output_of_default_create()
示例#4
0
 def test_config_file_command_key(self):
     attrs = [
         (None, None),
         ('symlinks', '--copies'),
         ('with_pip', '--without-pip'),
         ('system_site_packages', '--system-site-packages'),
         ('clear', '--clear'),
         ('upgrade', '--upgrade'),
         ('upgrade_deps', '--upgrade-deps'),
         ('prompt', '--prompt'),
     ]
     for attr, opt in attrs:
         rmtree(self.env_dir)
         if not attr:
             b = venv.EnvBuilder()
         else:
             b = venv.EnvBuilder(
                 **{attr: False if attr in ('with_pip', 'symlinks') else True})
         b.upgrade_dependencies = Mock() # avoid pip command to upgrade deps
         b._setup_pip = Mock() # avoid pip setup
         self.run_with_capture(b.create, self.env_dir)
         data = self.get_text_file_contents('pyvenv.cfg')
         if not attr:
             for opt in ('--system-site-packages', '--clear', '--upgrade',
                     '--upgrade-deps', '--prompt'):
                 self.assertNotRegex(data, rf'command = .* {opt}')
         elif os.name=='nt' and attr=='symlinks':
             pass
         else:
             self.assertRegex(data, rf'command = .* {opt}')
示例#5
0
    def test_macos_env(self):
        rmtree(self.env_dir)
        builder = venv.EnvBuilder()
        builder.create(self.env_dir)

        envpy = os.path.join(os.path.realpath(self.env_dir),
                             self.bindir, self.exe)
示例#6
0
 def test_pathsep_error(self):
     """
     Test that venv creation fails when the target directory contains
     the path separator.
     """
     rmtree(self.env_dir)
     self.assertRaises(ValueError, venv.create, self.env_dir + os.pathsep)
示例#7
0
 def clear_directory(self, path):
     for fn in os.listdir(path):
         fn = os.path.join(path, fn)
         if os.path.islink(fn) or os.path.isfile(fn):
             os.remove(fn)
         elif os.path.isdir(fn):
             rmtree(fn)
示例#8
0
 def restore_files(self, saved_value):
     fn = os_helper.TESTFN
     if fn not in saved_value and (fn + '/') not in saved_value:
         if os.path.isfile(fn):
             os_helper.unlink(fn)
         elif os.path.isdir(fn):
             os_helper.rmtree(fn)
示例#9
0
 def test_executable(self):
     """
     Test that the sys.executable value is as expected.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     envpy = os.path.join(os.path.realpath(self.env_dir),
                          self.bindir, self.exe)
示例#10
0
 def tearDown(self):
     # Restore working dir, for Solaris and derivatives, where rmdir()
     # on the current directory fails.
     os.chdir(self.old_cwd)
     super().tearDown()
     while self.tempdirs:
         tmpdir = self.tempdirs.pop()
         os_helper.rmtree(tmpdir)
示例#11
0
 def test_executable_symlinks(self):
     """
     Test that the sys.executable value is as expected.
     """
     rmtree(self.env_dir)
     builder = venv.EnvBuilder(clear=True, symlinks=True)
     builder.create(self.env_dir)
     envpy = os.path.join(os.path.realpath(self.env_dir),
                          self.bindir, self.exe)
示例#12
0
    def test_macos_env(self):
        rmtree(self.env_dir)
        builder = venv.EnvBuilder()
        builder.create(self.env_dir)

        envpy = os.path.join(os.path.realpath(self.env_dir),
                             self.bindir, self.exe)
        out, err = check_output([envpy, '-c',
            'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
        self.assertEqual(out.strip(), 'False'.encode())
示例#13
0
def create_modules(*names):
    """Temporarily create each named module with an attribute (named 'attr')
    that contains the name passed into the context manager that caused the
    creation of the module.

    All files are created in a temporary directory returned by
    tempfile.mkdtemp(). This directory is inserted at the beginning of
    sys.path. When the context manager exits all created files (source and
    bytecode) are explicitly deleted.

    No magic is performed when creating packages! This means that if you create
    a module within a package you must also create the package's __init__ as
    well.

    """
    source = 'attr = {0!r}'
    created_paths = []
    mapping = {}
    state_manager = None
    uncache_manager = None
    try:
        temp_dir = tempfile.mkdtemp()
        mapping['.root'] = temp_dir
        import_names = set()
        for name in names:
            if not name.endswith('__init__'):
                import_name = name
            else:
                import_name = name[:-len('.__init__')]
            import_names.add(import_name)
            if import_name in sys.modules:
                del sys.modules[import_name]
            name_parts = name.split('.')
            file_path = temp_dir
            for directory in name_parts[:-1]:
                file_path = os.path.join(file_path, directory)
                if not os.path.exists(file_path):
                    os.mkdir(file_path)
                    created_paths.append(file_path)
            file_path = os.path.join(file_path, name_parts[-1] + '.py')
            with open(file_path, 'w') as file:
                file.write(source.format(name))
            created_paths.append(file_path)
            mapping[name] = file_path
        uncache_manager = uncache(*import_names)
        uncache_manager.__enter__()
        state_manager = import_state(path=[temp_dir])
        state_manager.__enter__()
        yield mapping
    finally:
        if state_manager is not None:
            state_manager.__exit__(None, None, None)
        if uncache_manager is not None:
            uncache_manager.__exit__(None, None, None)
        os_helper.rmtree(temp_dir)
示例#14
0
    def _runtest(self, test_name: str) -> MultiprocessResult:
        # gh-94026: Write stdout+stderr to a tempfile as workaround for
        # non-blocking pipes on Emscripten with NodeJS.
        with tempfile.TemporaryFile('w+',
                                    encoding=sys.stdout.encoding) as stdout_fh:
            # gh-93353: Check for leaked temporary files in the parent process,
            # since the deletion of temporary files can happen late during
            # Python finalization: too late for libregrtest.
            if not support.is_wasi:
                # Don't check for leaked temporary files and directories if Python is
                # run on WASI. WASI don't pass environment variables like TMPDIR to
                # worker processes.
                tmp_dir = tempfile.mkdtemp(prefix="test_python_")
                tmp_dir = os.path.abspath(tmp_dir)
                try:
                    retcode = self._run_process(test_name, tmp_dir, stdout_fh)
                finally:
                    tmp_files = os.listdir(tmp_dir)
                    os_helper.rmtree(tmp_dir)
            else:
                retcode = self._run_process(test_name, None, stdout_fh)
                tmp_files = ()
            stdout_fh.seek(0)
            stdout = stdout_fh.read().strip()

        if retcode is None:
            return self.mp_result_error(Timeout(test_name), stdout)

        err_msg = None
        if retcode != 0:
            err_msg = "Exit code %s" % retcode
        else:
            stdout, _, result = stdout.rpartition("\n")
            stdout = stdout.rstrip()
            if not result:
                err_msg = "Failed to parse worker stdout"
            else:
                try:
                    # deserialize run_tests_worker() output
                    result = json.loads(result, object_hook=decode_test_result)
                except Exception as exc:
                    err_msg = "Failed to parse worker JSON: %s" % exc

        if err_msg is not None:
            return self.mp_result_error(ChildError(test_name), stdout, err_msg)

        if tmp_files:
            msg = (f'\n\n'
                   f'Warning -- {test_name} leaked temporary files '
                   f'({len(tmp_files)}): {", ".join(sorted(tmp_files))}')
            stdout += msg
            if isinstance(result, Passed):
                result = EnvChanged.from_passed(result)

        return MultiprocessResult(result, stdout, err_msg)
示例#15
0
 def test_executable(self):
     """
     Test that the sys.executable value is as expected.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     envpy = os.path.join(os.path.realpath(self.env_dir),
                          self.bindir, self.exe)
     out, err = check_output([envpy, '-c',
         'import sys; print(sys.executable)'])
     self.assertEqual(out.strip(), envpy.encode())
示例#16
0
    def cleanup(self):
        import glob

        path = os.path.join(glob.escape(self.tmp_dir), 'test_python_*')
        print("Cleanup %s directory" % self.tmp_dir)
        for name in glob.glob(path):
            if os.path.isdir(name):
                print("Remove directory: %s" % name)
                os_helper.rmtree(name)
            else:
                print("Remove file: %s" % name)
                os_helper.unlink(name)
示例#17
0
 def test_sysconfig_preferred_and_default_scheme(self):
     """
     Test that the sysconfig preferred(prefix) and default scheme is venv.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     envpy = os.path.join(self.env_dir, self.bindir, self.exe)
     cmd = [envpy, '-c', None]
     for call in ('get_preferred_scheme("prefix")', 'get_default_scheme()'):
         cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
         out, err = check_output(cmd)
         self.assertEqual(out.strip(), b'venv', err)
示例#18
0
 def test_executable_symlinks(self):
     """
     Test that the sys.executable value is as expected.
     """
     rmtree(self.env_dir)
     builder = venv.EnvBuilder(clear=True, symlinks=True)
     builder.create(self.env_dir)
     envpy = os.path.join(os.path.realpath(self.env_dir),
                          self.bindir, self.exe)
     out, err = check_output([envpy, '-c',
         'import sys; print(sys.executable)'])
     self.assertEqual(out.strip(), envpy.encode())
示例#19
0
 def test_unicode_in_batch_file(self):
     """
     Test handling of Unicode paths
     """
     rmtree(self.env_dir)
     env_dir = os.path.join(os.path.realpath(self.env_dir), 'ϼўТλФЙ')
     builder = venv.EnvBuilder(clear=True)
     builder.create(env_dir)
     activate = os.path.join(env_dir, self.bindir, 'activate.bat')
     envpy = os.path.join(env_dir, self.bindir, self.exe)
     out, err = check_output(
         [activate, '&', self.exe, '-c', 'print(0)'],
         encoding='oem',
示例#20
0
    def test_multiprocessing(self):
        """
        Test that the multiprocessing is able to spawn.
        """
        # bpo-36342: Instantiation of a Pool object imports the
        # multiprocessing.synchronize module. Skip the test if this module
        # cannot be imported.
        skip_if_broken_multiprocessing_synchronize()

        rmtree(self.env_dir)
        self.run_with_capture(venv.create, self.env_dir)
        envpy = os.path.join(os.path.realpath(self.env_dir),
                             self.bindir, self.exe)
示例#21
0
 def test_deactivate_with_strict_bash_opts(self):
     bash = shutil.which("bash")
     if bash is None:
         self.skipTest("bash required for this test")
     rmtree(self.env_dir)
     builder = venv.EnvBuilder(clear=True)
     builder.create(self.env_dir)
     activate = os.path.join(self.env_dir, self.bindir, "activate")
     test_script = os.path.join(self.env_dir, "test_strict.sh")
     with open(test_script, "w") as f:
         f.write("set -euo pipefail\n"
                 f"source {activate}\n"
                 "deactivate\n")
示例#22
0
 def test_defaults(self):
     """
     Test the create function with default arguments.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     self.isdir(self.bindir)
     self.isdir(self.include)
     self.isdir(*self.lib)
     # Issue 21197
     p = self.get_env_file('lib64')
     conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and
                   (sys.platform != 'darwin'))
示例#23
0
    def test_temp_dir(self):
        """Test that temp_dir() creates and destroys its directory."""
        parent_dir = tempfile.mkdtemp()
        parent_dir = os.path.realpath(parent_dir)

        try:
            path = os.path.join(parent_dir, 'temp')
            self.assertFalse(os.path.isdir(path))
            with os_helper.temp_dir(path) as temp_path:
                self.assertEqual(temp_path, path)
                self.assertTrue(os.path.isdir(path))
            self.assertFalse(os.path.isdir(path))
        finally:
            os_helper.rmtree(parent_dir)
示例#24
0
    def _do_directory(self, make_name, chdir_name):
        if os.path.isdir(make_name):
            rmtree(make_name)
        os.mkdir(make_name)
        try:
            with change_cwd(chdir_name):
                cwd_result = os.getcwd()
                name_result = make_name

                cwd_result = unicodedata.normalize("NFD", cwd_result)
                name_result = unicodedata.normalize("NFD", name_result)

                self.assertEqual(os.path.basename(cwd_result), name_result)
        finally:
            os.rmdir(make_name)
示例#25
0
    def test_forget(self):
        mod_filename = TESTFN + '.py'
        with open(mod_filename, 'w', encoding="utf-8") as f:
            print('foo = 1', file=f)
        sys.path.insert(0, os.curdir)
        importlib.invalidate_caches()
        try:
            mod = __import__(TESTFN)
            self.assertIn(TESTFN, sys.modules)

            import_helper.forget(TESTFN)
            self.assertNotIn(TESTFN, sys.modules)
        finally:
            del sys.path[0]
            os_helper.unlink(mod_filename)
            os_helper.rmtree('__pycache__')
示例#26
0
 def test_prefixes(self):
     """
     Test that the prefix values are as expected.
     """
     # check a venv's prefixes
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir)
     envpy = os.path.join(self.env_dir, self.bindir, self.exe)
     cmd = [envpy, '-c', None]
     for prefix, expected in (('prefix', self.env_dir), ('exec_prefix',
                                                         self.env_dir),
                              ('base_prefix', sys.base_prefix),
                              ('base_exec_prefix', sys.base_exec_prefix)):
         cmd[2] = 'import sys; print(sys.%s)' % prefix
         out, err = check_output(cmd)
         self.assertEqual(out.strip(), expected.encode(), prefix)
示例#27
0
    def _runtest(self, test_name: str) -> MultiprocessResult:
        if self.ns.use_mp == 1:
            # gh-93353: Check for leaked temporary files in the parent process,
            # since the deletion of temporary files can happen late during
            # Python finalization: too late for libregrtest.
            tmp_dir = os.getcwd() + '_tmpdir'
            tmp_dir = os.path.abspath(tmp_dir)
            try:
                os.mkdir(tmp_dir)
                retcode, stdout = self._run_process(test_name, tmp_dir)
            finally:
                tmp_files = os.listdir(tmp_dir)
                os_helper.rmtree(tmp_dir)
        else:
            retcode, stdout = self._run_process(test_name, None)
            tmp_files = ()

        if retcode is None:
            return self.mp_result_error(Timeout(test_name), stdout)

        err_msg = None
        if retcode != 0:
            err_msg = "Exit code %s" % retcode
        else:
            stdout, _, result = stdout.rpartition("\n")
            stdout = stdout.rstrip()
            if not result:
                err_msg = "Failed to parse worker stdout"
            else:
                try:
                    # deserialize run_tests_worker() output
                    result = json.loads(result, object_hook=decode_test_result)
                except Exception as exc:
                    err_msg = "Failed to parse worker JSON: %s" % exc

        if err_msg is not None:
            return self.mp_result_error(ChildError(test_name), stdout, err_msg)

        if tmp_files:
            msg = (f'\n\n'
                   f'Warning -- {test_name} leaked temporary files '
                   f'({len(tmp_files)}): {", ".join(sorted(tmp_files))}')
            stdout += msg
            if isinstance(result, Passed):
                result = EnvChanged.from_passed(result)

        return MultiprocessResult(result, stdout, err_msg)
示例#28
0
    def test_prompt(self):
        env_name = os.path.split(self.env_dir)[1]

        rmtree(self.env_dir)
        builder = venv.EnvBuilder()
        self.run_with_capture(builder.create, self.env_dir)
        context = builder.ensure_directories(self.env_dir)
        data = self.get_text_file_contents('pyvenv.cfg')
        self.assertEqual(context.prompt, '(%s) ' % env_name)
        self.assertNotIn("prompt = ", data)

        rmtree(self.env_dir)
        builder = venv.EnvBuilder(prompt='My prompt')
        self.run_with_capture(builder.create, self.env_dir)
        context = builder.ensure_directories(self.env_dir)
        data = self.get_text_file_contents('pyvenv.cfg')
        self.assertEqual(context.prompt, '(My prompt) ')
        self.assertIn("prompt = 'My prompt'\n", data)
示例#29
0
    def test_multiprocessing(self):
        """
        Test that the multiprocessing is able to spawn.
        """
        # bpo-36342: Instantiation of a Pool object imports the
        # multiprocessing.synchronize module. Skip the test if this module
        # cannot be imported.
        skip_if_broken_multiprocessing_synchronize()

        rmtree(self.env_dir)
        self.run_with_capture(venv.create, self.env_dir)
        envpy = os.path.join(os.path.realpath(self.env_dir),
                             self.bindir, self.exe)
        out, err = check_output([envpy, '-c',
            'from multiprocessing import Pool; '
            'pool = Pool(1); '
            'print(pool.apply_async("Python".lower).get(3)); '
            'pool.terminate()'])
        self.assertEqual(out.strip(), "python".encode())
示例#30
0
文件: test_venv.py 项目: nirs/cpython
 def test_sysconfig_symlinks(self):
     """
     Test that the sysconfig functions work in a virtual environment.
     """
     rmtree(self.env_dir)
     self.run_with_capture(venv.create, self.env_dir, symlinks=True)
     envpy = os.path.join(self.env_dir, self.bindir, self.exe)
     cmd = [envpy, '-c', None]
     for call, expected in (
         # installation scheme
         ('get_preferred_scheme("prefix")', 'venv'),
         ('get_default_scheme()', 'venv'),
         # build environment
         ('is_python_build()', str(sysconfig.is_python_build())),
         ('get_makefile_filename()', sysconfig.get_makefile_filename()),
         ('get_config_h_filename()', sysconfig.get_config_h_filename())):
         with self.subTest(call):
             cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
             out, err = check_output(cmd)
             self.assertEqual(out.strip(), expected.encode(), err)