Skip to content

Commit

Permalink
Test site_libs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekap committed Jan 13, 2016
1 parent 4f93d89 commit 8028e99
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
12 changes: 8 additions & 4 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ def _extras_paths(cls):
yield os.path.join(standard_lib, path)

@classmethod
def _site_libs(cls):
def _get_site_packages(cls):
try:
from site import getsitepackages
site_libs = set(getsitepackages())
return set(getsitepackages())
except ImportError:
site_libs = set()
return set()

@classmethod
def site_libs(cls):
site_libs = cls._get_site_packages()
site_libs.update([sysconfig.get_python_lib(plat_specific=False),
sysconfig.get_python_lib(plat_specific=True)])
# On windows getsitepackages() returns the python stdlib too.
Expand Down Expand Up @@ -193,7 +197,7 @@ def minimum_sys(cls):
:returns: (sys.path, sys.path_importer_cache, sys.modules) tuple of a
bare python installation.
"""
site_libs = set(cls._site_libs())
site_libs = set(cls.site_libs())
for site_lib in site_libs:
TRACER.log('Found site-library: %s' % site_lib)
for extras_path in cls._extras_paths():
Expand Down
46 changes: 45 additions & 1 deletion tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
import sys
import textwrap
from types import ModuleType

import pytest
from twitter.common.contextutil import temporary_dir

from pex.compatibility import to_bytes
from pex.compatibility import nested, to_bytes
from pex.installer import EggInstaller, WheelInstaller
from pex.pex import PEX
from pex.testing import make_installer, run_simple_pex_test
from pex.util import DistributionHelper

try:
from unittest import mock
except ImportError:
import mock


@pytest.mark.skipif('sys.version_info > (3,)')
def test_pex_uncaught_exceptions():
Expand Down Expand Up @@ -120,6 +127,43 @@ def test_minimum_sys_modules():
assert tainted_module.__path__ == ['good_path']


def test_site_libs():
with nested(mock.patch.object(PEX, '_get_site_packages'), temporary_dir()) as (
mock_site_packages, tempdir):
site_packages = os.path.join(tempdir, 'site-packages')
os.mkdir(site_packages)
mock_site_packages.return_value = set([site_packages])
site_libs = PEX.site_libs()
assert site_packages in site_libs


def test_site_libs_symlink():
with nested(mock.patch.object(PEX, '_get_site_packages'), temporary_dir()) as (
mock_site_packages, tempdir):
site_packages = os.path.join(tempdir, 'site-packages')
os.mkdir(site_packages)
site_packages_link = os.path.join(tempdir, 'site-packages-link')
os.symlink(site_packages, site_packages_link)
mock_site_packages.return_value = set([site_packages_link])

site_libs = PEX.site_libs()
assert os.path.realpath(site_packages) in site_libs
assert site_packages_link in site_libs


def test_site_libs_excludes_prefix():
"""Windows returns sys.prefix as part of getsitepackages(). Make sure to exclude it."""

with nested(mock.patch.object(PEX, '_get_site_packages'), temporary_dir()) as (
mock_site_packages, tempdir):
site_packages = os.path.join(tempdir, 'site-packages')
os.mkdir(site_packages)
mock_site_packages.return_value = set([site_packages, sys.prefix])
site_libs = PEX.site_libs()
assert site_packages in site_libs
assert sys.prefix not in site_libs


@pytest.mark.parametrize('zip_safe', (False, True))
@pytest.mark.parametrize('project_name', ('my_project', 'my-project'))
@pytest.mark.parametrize('installer_impl', (EggInstaller, WheelInstaller))
Expand Down

0 comments on commit 8028e99

Please sign in to comment.