| 1 |
import pytest |
| 2 |
import py |
| 3 |
import os.path |
| 4 |
import sys |
| 5 |
from pypy.module.sys.initpath import (compute_stdlib_path_sourcetree, |
| 6 |
find_executable, find_stdlib, resolvedirof, pypy_init_home, pypy_init_free, |
| 7 |
find_pyvenv_cfg) |
| 8 |
from pypy.module.sys.version import PYPY_VERSION, CPYTHON_VERSION |
| 9 |
from rpython.rtyper.lltypesystem import rffi |
| 10 |
|
| 11 |
def build_hierarchy_srctree(prefix): |
| 12 |
dirname = '%d' % CPYTHON_VERSION[0] |
| 13 |
a = prefix.join('lib_pypy').ensure(dir=1) |
| 14 |
b = prefix.join('lib-python', dirname).ensure(dir=1) |
| 15 |
return a, b |
| 16 |
|
| 17 |
def build_hierarchy_package(prefix, platlibdir="lib"): |
| 18 |
if sys.platform == "win32": |
| 19 |
b = prefix.join(platlibdir).ensure(dir=1) |
| 20 |
else: |
| 21 |
dot_ver = 'pypy%d.%d' % CPYTHON_VERSION[:2] |
| 22 |
b = prefix.join(platlibdir, dot_ver).ensure(dir=1) |
| 23 |
b.join('site.py').ensure(dir=0) |
| 24 |
return b |
| 25 |
|
| 26 |
def test_find_stdlib(tmpdir): |
| 27 |
bin_dir = tmpdir.join('bin').ensure(dir=True) |
| 28 |
pypy = bin_dir.join('pypy3').ensure(file=True) |
| 29 |
build_hierarchy_srctree(tmpdir) |
| 30 |
path, prefix = find_stdlib(None, "lib", str(pypy)) |
| 31 |
assert prefix == tmpdir |
| 32 |
|
| 33 |
|
| 34 |
_, prefix = find_stdlib(None, "lib", '') |
| 35 |
cwd = os.path.dirname(os.path.realpath(__file__)) |
| 36 |
assert prefix is not None |
| 37 |
assert cwd.startswith(str(prefix)) |
| 38 |
|
| 39 |
if sys.platform == "win32": |
| 40 |
libdirnames = ["Lib"] |
| 41 |
else: |
| 42 |
libdirnames = ["lib", "lib64"] |
| 43 |
@pytest.mark.parametrize("platlibdir", libdirnames) |
| 44 |
def test_find_stdlib_package(tmpdir, platlibdir): |
| 45 |
bin_dir = tmpdir.join('bin').ensure(dir=True) |
| 46 |
pypy = bin_dir.join('pypy3').ensure(file=True) |
| 47 |
build_hierarchy_package(tmpdir, platlibdir) |
| 48 |
path, prefix = find_stdlib(None, platlibdir, str(pypy)) |
| 49 |
assert prefix == tmpdir |
| 50 |
|
| 51 |
|
| 52 |
_, prefix = find_stdlib(None, platlibdir, '') |
| 53 |
cwd = os.path.dirname(os.path.realpath(__file__)) |
| 54 |
assert prefix is not None |
| 55 |
assert cwd.startswith(str(prefix)) |
| 56 |
|
| 57 |
@py.test.mark.skipif('not hasattr(os, "symlink")') |
| 58 |
def test_find_stdlib_follow_symlink(tmpdir): |
| 59 |
pypydir = tmpdir.join('opt', 'pypy3-xxx') |
| 60 |
pypy = pypydir.join('bin', 'pypy3').ensure(file=True) |
| 61 |
build_hierarchy_srctree(pypydir) |
| 62 |
pypy_sym = tmpdir.join('pypy3_sym') |
| 63 |
os.symlink(str(pypy), str(pypy_sym)) |
| 64 |
path, prefix = find_stdlib(None, "lib", str(pypy_sym)) |
| 65 |
assert prefix == pypydir |
| 66 |
|
| 67 |
def test_pypy_init_home(): |
| 68 |
p = pypy_init_home() |
| 69 |
assert p |
| 70 |
s = rffi.charp2str(p) |
| 71 |
pypy_init_free(p) |
| 72 |
assert os.path.exists(s) |
| 73 |
|
| 74 |
def test_compute_stdlib_path(tmpdir): |
| 75 |
dirs = build_hierarchy_srctree(tmpdir) |
| 76 |
path = compute_stdlib_path_sourcetree(None, "lib", str(tmpdir)) |
| 77 |
|
| 78 |
assert path[:len(dirs)] == map(str, dirs) |
| 79 |
|
| 80 |
def test_find_executable(tmpdir, monkeypatch): |
| 81 |
from pypy.module.sys import initpath |
| 82 |
tmpdir = py.path.local(os.path.realpath(str(tmpdir))) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
a = tmpdir.join('a').ensure(dir=True) |
| 87 |
b = tmpdir.join('b').ensure(dir=True) |
| 88 |
c = tmpdir.join('c').ensure(dir=True) |
| 89 |
a.join('pypy3').ensure(file=True) |
| 90 |
b.join('pypy3').ensure(file=True) |
| 91 |
|
| 92 |
monkeypatch.setattr(os, 'access', lambda x, y: True) |
| 93 |
|
| 94 |
monkeypatch.chdir(tmpdir) |
| 95 |
assert find_executable('a/pypy3') == a.join('pypy3') |
| 96 |
|
| 97 |
|
| 98 |
monkeypatch.setenv('PATH', None) |
| 99 |
monkeypatch.chdir(a) |
| 100 |
assert find_executable('pypy3') == a.join('pypy3') |
| 101 |
monkeypatch.chdir(tmpdir) |
| 102 |
assert find_executable('pypy3') == '' |
| 103 |
|
| 104 |
|
| 105 |
monkeypatch.setenv('PATH', str(a)) |
| 106 |
assert find_executable('pypy3') == a.join('pypy3') |
| 107 |
|
| 108 |
|
| 109 |
monkeypatch.setenv('PATH', '%s%s%s' % (b, os.pathsep, a)) |
| 110 |
assert find_executable('pypy3') == b.join('pypy3') |
| 111 |
|
| 112 |
|
| 113 |
monkeypatch.setenv('PATH', '%s%s%s' % (c, os.pathsep, a)) |
| 114 |
assert find_executable('pypy3') == a.join('pypy3') |
| 115 |
|
| 116 |
c.join('pypy3').ensure(dir=True) |
| 117 |
assert find_executable('pypy3') == a.join('pypy3') |
| 118 |
|
| 119 |
monkeypatch.setattr(os, 'access', lambda x, y: False) |
| 120 |
assert find_executable('pypy3') == '' |
| 121 |
|
| 122 |
monkeypatch.setattr(os, 'access', lambda x, y: True) |
| 123 |
monkeypatch.setattr(initpath, 'we_are_translated', lambda: True) |
| 124 |
monkeypatch.setattr(initpath, '_WIN32', True) |
| 125 |
monkeypatch.setenv('PATH', str(a)) |
| 126 |
a.join('pypy3.exe').ensure(file=True) |
| 127 |
assert find_executable('pypy3') == a.join('pypy3.exe') |
| 128 |
|
| 129 |
def test_resolvedirof(tmpdir): |
| 130 |
assert resolvedirof('') == os.path.abspath(os.path.join(os.getcwd(), '..')) |
| 131 |
foo = tmpdir.join('foo').ensure(dir=True) |
| 132 |
bar = tmpdir.join('bar').ensure(dir=True) |
| 133 |
myfile = foo.join('myfile').ensure(file=True) |
| 134 |
assert resolvedirof(str(myfile)) == foo |
| 135 |
if hasattr(myfile, 'mksymlinkto'): |
| 136 |
myfile2 = bar.join('myfile') |
| 137 |
myfile2.mksymlinkto(myfile) |
| 138 |
assert resolvedirof(str(myfile2)) == foo |
| 139 |
|
| 140 |
def test_find_pyvenv_cfg(tmpdir): |
| 141 |
subdir = tmpdir.join('find_cfg').ensure(dir=True) |
| 142 |
assert find_pyvenv_cfg(str(subdir)) == '' |
| 143 |
subdir.join('pyvenv.cfg').write('foobar') |
| 144 |
assert find_pyvenv_cfg(str(subdir)) == '' |
| 145 |
subdir.join('pyvenv.cfg').write('foobar\nhome=xyz') |
| 146 |
assert find_pyvenv_cfg(str(subdir)) == 'xyz' |
| 147 |
subdir.join('pyvenv.cfg').write('foohome=xyz') |
| 148 |
assert find_pyvenv_cfg(str(subdir)) == '' |
| 149 |
subdir.join('pyvenv.cfg').write('home = xyx \nbar = baz\n') |
| 150 |
assert find_pyvenv_cfg(str(subdir)) == 'xyx' |
| 151 |
|
| 152 |
def test_find_stdlib_follow_pyvenv_cfg(tmpdir): |
| 153 |
mydir = tmpdir.join('follow_pyvenv_cfg').ensure(dir=True) |
| 154 |
otherdir = tmpdir.join('otherdir').ensure(dir=True) |
| 155 |
bin_dir = mydir.join('bin').ensure(dir=True) |
| 156 |
pypy = bin_dir.join('pypy3').ensure(file=True) |
| 157 |
build_hierarchy_srctree(otherdir) |
| 158 |
for homedir in [otherdir, otherdir.join('bin')]: |
| 159 |
mydir.join('pyvenv.cfg').write('home = %s\n' % (homedir,)) |
| 160 |
_, prefix = find_stdlib(None, "lib", str(pypy)) |
| 161 |
assert prefix == otherdir |