| 1 |
import py |
| 2 |
import sys, stat, os |
| 3 |
from rpython.translator.sandbox.vfs import * |
| 4 |
from rpython.tool.udir import udir |
| 5 |
|
| 6 |
HASLINK = hasattr(os, 'symlink') |
| 7 |
|
| 8 |
def setup_module(mod): |
| 9 |
d = udir.ensure('test_vfs', dir=1) |
| 10 |
d.join('file1').write('somedata1') |
| 11 |
d.join('file2').write('somelongerdata2') |
| 12 |
os.chmod(str(d.join('file2')), stat.S_IWUSR) |
| 13 |
d.join('.hidden').write('secret') |
| 14 |
d.ensure('subdir1', dir=1).join('subfile1').write('spam') |
| 15 |
d.ensure('.subdir2', dir=1).join('subfile2').write('secret as well') |
| 16 |
if HASLINK: |
| 17 |
d.join('symlink1').mksymlinkto(str(d.join('subdir1'))) |
| 18 |
d.join('symlink2').mksymlinkto('.hidden') |
| 19 |
d.join('symlink3').mksymlinkto('BROKEN') |
| 20 |
|
| 21 |
|
| 22 |
def test_dir(): |
| 23 |
d = Dir({'foo': Dir()}) |
| 24 |
assert d.keys() == ['foo'] |
| 25 |
py.test.raises(OSError, d.open) |
| 26 |
assert 0 <= d.getsize() <= sys.maxint |
| 27 |
d1 = d.join('foo') |
| 28 |
assert stat.S_ISDIR(d1.kind) |
| 29 |
assert d1.keys() == [] |
| 30 |
py.test.raises(OSError, d.join, 'bar') |
| 31 |
st = d.stat() |
| 32 |
assert stat.S_ISDIR(st.st_mode) |
| 33 |
assert d.access(os.R_OK | os.X_OK) |
| 34 |
assert not d.access(os.W_OK) |
| 35 |
|
| 36 |
def test_file(): |
| 37 |
f = File('hello world') |
| 38 |
assert stat.S_ISREG(f.kind) |
| 39 |
py.test.raises(OSError, f.keys) |
| 40 |
assert f.getsize() == 11 |
| 41 |
h = f.open() |
| 42 |
data = h.read() |
| 43 |
assert data == 'hello world' |
| 44 |
h.close() |
| 45 |
st = f.stat() |
| 46 |
assert stat.S_ISREG(st.st_mode) |
| 47 |
assert st.st_size == 11 |
| 48 |
assert f.access(os.R_OK) |
| 49 |
assert not f.access(os.W_OK) |
| 50 |
|
| 51 |
def test_realdir_realfile(): |
| 52 |
for show_dotfiles in [False, True]: |
| 53 |
for follow_links in [False, True]: |
| 54 |
v_udir = RealDir(str(udir), show_dotfiles = show_dotfiles, |
| 55 |
follow_links = follow_links) |
| 56 |
v_test_vfs = v_udir.join('test_vfs') |
| 57 |
names = v_test_vfs.keys() |
| 58 |
names.sort() |
| 59 |
assert names == (show_dotfiles * ['.hidden', '.subdir2'] + |
| 60 |
['file1', 'file2', 'subdir1'] + |
| 61 |
HASLINK * ['symlink1', 'symlink2', 'symlink3']) |
| 62 |
py.test.raises(OSError, v_test_vfs.open) |
| 63 |
assert 0 <= v_test_vfs.getsize() <= sys.maxint |
| 64 |
|
| 65 |
f = v_test_vfs.join('file1') |
| 66 |
assert f.open().read() == 'somedata1' |
| 67 |
|
| 68 |
f = v_test_vfs.join('file2') |
| 69 |
assert f.getsize() == len('somelongerdata2') |
| 70 |
if os.name != 'nt': |
| 71 |
py.test.raises(OSError, f.open) |
| 72 |
|
| 73 |
py.test.raises(OSError, v_test_vfs.join, 'does_not_exist') |
| 74 |
py.test.raises(OSError, v_test_vfs.join, 'symlink3') |
| 75 |
if follow_links and HASLINK: |
| 76 |
d = v_test_vfs.join('symlink1') |
| 77 |
assert stat.S_ISDIR(d.stat().st_mode) |
| 78 |
assert d.keys() == ['subfile1'] |
| 79 |
assert d.join('subfile1').open().read() == 'spam' |
| 80 |
|
| 81 |
f = v_test_vfs.join('symlink2') |
| 82 |
assert stat.S_ISREG(f.stat().st_mode) |
| 83 |
assert f.access(os.R_OK) |
| 84 |
assert f.open().read() == 'secret' |
| 85 |
else: |
| 86 |
py.test.raises(OSError, v_test_vfs.join, 'symlink1') |
| 87 |
py.test.raises(OSError, v_test_vfs.join, 'symlink2') |
| 88 |
|
| 89 |
if show_dotfiles: |
| 90 |
f = v_test_vfs.join('.hidden') |
| 91 |
assert f.open().read() == 'secret' |
| 92 |
|
| 93 |
d = v_test_vfs.join('.subdir2') |
| 94 |
assert d.keys() == ['subfile2'] |
| 95 |
assert d.join('subfile2').open().read() == 'secret as well' |
| 96 |
else: |
| 97 |
py.test.raises(OSError, v_test_vfs.join, '.hidden') |
| 98 |
py.test.raises(OSError, v_test_vfs.join, '.subdir2') |
| 99 |
|
| 100 |
def test_realdir_exclude(): |
| 101 |
xdir = udir.ensure('test_realdir_exclude', dir=1) |
| 102 |
xdir.ensure('test_realdir_exclude.yes') |
| 103 |
xdir.ensure('test_realdir_exclude.no') |
| 104 |
v_udir = RealDir(str(udir), exclude=['.no']) |
| 105 |
v_xdir = v_udir.join('test_realdir_exclude') |
| 106 |
assert 'test_realdir_exclude.yes' in v_xdir.keys() |
| 107 |
assert 'test_realdir_exclude.no' not in v_xdir.keys() |
| 108 |
v_xdir.join('test_realdir_exclude.yes') |
| 109 |
py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.no') |
| 110 |
|
| 111 |
py.test.raises(OSError, v_xdir.join, 'Test_RealDir_Exclude.no') |
| 112 |
py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.No') |
| 113 |
py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.nO') |
| 114 |
py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.NO') |