2007-01-24 22:24:01 +08:00
|
|
|
import py
|
2008-08-21 18:00:33 +08:00
|
|
|
import sys
|
2007-01-24 22:24:01 +08:00
|
|
|
from py.path import local
|
2009-09-06 22:59:39 +08:00
|
|
|
from testing.path import common
|
2009-09-01 01:51:25 +08:00
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
failsonjython = py.test.mark.xfail("sys.platform.startswith('java')")
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def pytest_funcarg__path1(request):
|
|
|
|
def setup():
|
|
|
|
path1 = request.config.mktemp("path1")
|
|
|
|
common.setuptestfs(path1)
|
|
|
|
return path1
|
|
|
|
def teardown(path1):
|
|
|
|
# post check
|
|
|
|
assert path1.join("samplefile").check()
|
|
|
|
return request.cached_setup(setup, teardown, scope="session")
|
|
|
|
|
|
|
|
class TestLocalPath(common.CommonFSTests):
|
|
|
|
def test_join_normpath(self, tmpdir):
|
|
|
|
assert tmpdir.join(".") == tmpdir
|
|
|
|
p = tmpdir.join("../%s" % tmpdir.basename)
|
|
|
|
assert p == tmpdir
|
|
|
|
p = tmpdir.join("..//%s/" % tmpdir.basename)
|
|
|
|
assert p == tmpdir
|
|
|
|
|
|
|
|
def test_gethash(self, tmpdir):
|
2009-09-05 03:47:49 +08:00
|
|
|
md5 = py.builtin._tryimport('md5', 'hashlib').md5
|
|
|
|
lib = py.builtin._tryimport('sha', 'hashlib')
|
|
|
|
sha = getattr(lib, 'sha1', getattr(lib, 'sha', None))
|
2009-09-01 01:51:25 +08:00
|
|
|
fn = tmpdir.join("testhashfile")
|
|
|
|
data = 'hello'.encode('ascii')
|
|
|
|
fn.write(data, mode="wb")
|
|
|
|
assert fn.computehash("md5") == md5(data).hexdigest()
|
|
|
|
assert fn.computehash("sha1") == sha(data).hexdigest()
|
2007-01-24 22:24:01 +08:00
|
|
|
py.test.raises(ValueError, fn.computehash, "asdasd")
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_remove_removes_readonly_file(self, tmpdir):
|
|
|
|
readonly_file = tmpdir.join('readonly').ensure()
|
2007-01-24 22:24:01 +08:00
|
|
|
readonly_file.chmod(0)
|
|
|
|
readonly_file.remove()
|
|
|
|
assert not readonly_file.check(exists=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_remove_removes_readonly_dir(self, tmpdir):
|
|
|
|
readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
|
2009-08-30 02:04:48 +08:00
|
|
|
readonly_dir.chmod(int("500", 8))
|
2007-01-24 22:24:01 +08:00
|
|
|
readonly_dir.remove()
|
|
|
|
assert not readonly_dir.check(exists=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_remove_removes_dir_and_readonly_file(self, tmpdir):
|
|
|
|
readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
|
2007-01-24 22:24:01 +08:00
|
|
|
readonly_file = readonly_dir.join('readonlyfile').ensure()
|
|
|
|
readonly_file.chmod(0)
|
|
|
|
readonly_dir.remove()
|
|
|
|
assert not readonly_dir.check(exists=1)
|
|
|
|
|
|
|
|
def test_initialize_curdir(self):
|
|
|
|
assert str(local()) == py.std.os.getcwd()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_initialize_reldir(self, path1):
|
|
|
|
old = path1.chdir()
|
2007-01-24 22:24:01 +08:00
|
|
|
try:
|
|
|
|
p = local('samplefile')
|
|
|
|
assert p.check()
|
|
|
|
finally:
|
|
|
|
old.chdir()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_eq_with_strings(self, path1):
|
|
|
|
path1 = path1.join('sampledir')
|
2007-01-24 22:24:01 +08:00
|
|
|
path2 = str(path1)
|
|
|
|
assert path1 == path2
|
|
|
|
assert path2 == path1
|
2009-09-01 01:51:25 +08:00
|
|
|
path3 = path1.join('samplefile')
|
2007-01-24 22:24:01 +08:00
|
|
|
assert path3 != path2
|
|
|
|
assert path2 != path3
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
@py.test.mark.multi(bin=(False, True))
|
|
|
|
def test_dump(self, tmpdir, bin):
|
|
|
|
path = tmpdir.join("dumpfile%s" % int(bin))
|
|
|
|
try:
|
|
|
|
d = {'answer' : 42}
|
|
|
|
path.dump(d, bin=bin)
|
|
|
|
f = path.open('rb+')
|
2009-09-04 22:32:49 +08:00
|
|
|
dnew = py.std.pickle.load(f)
|
2009-09-01 01:51:25 +08:00
|
|
|
assert d == dnew
|
|
|
|
finally:
|
|
|
|
f.close()
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_setmtime(self):
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
try:
|
|
|
|
fd, name = tempfile.mkstemp()
|
|
|
|
py.std.os.close(fd)
|
|
|
|
except AttributeError:
|
|
|
|
name = tempfile.mktemp()
|
|
|
|
open(name, 'w').close()
|
|
|
|
try:
|
|
|
|
mtime = int(time.time())-100
|
|
|
|
path = local(name)
|
|
|
|
assert path.mtime() != mtime
|
|
|
|
path.setmtime(mtime)
|
|
|
|
assert path.mtime() == mtime
|
|
|
|
path.setmtime()
|
|
|
|
assert path.mtime() != mtime
|
|
|
|
finally:
|
|
|
|
py.std.os.remove(name)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_normpath(self, path1):
|
|
|
|
new1 = path1.join("/otherdir")
|
|
|
|
new2 = path1.join("otherdir")
|
2007-01-24 22:24:01 +08:00
|
|
|
assert str(new1) == str(new2)
|
|
|
|
|
|
|
|
def test_mkdtemp_creation(self):
|
|
|
|
d = local.mkdtemp()
|
|
|
|
try:
|
|
|
|
assert d.check(dir=1)
|
|
|
|
finally:
|
|
|
|
d.remove(rec=1)
|
|
|
|
|
|
|
|
def test_tmproot(self):
|
|
|
|
d = local.mkdtemp()
|
|
|
|
tmproot = local.get_temproot()
|
|
|
|
try:
|
|
|
|
assert d.check(dir=1)
|
|
|
|
assert d.dirpath() == tmproot
|
|
|
|
finally:
|
|
|
|
d.remove(rec=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chdir(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
old = local()
|
|
|
|
try:
|
|
|
|
res = tmpdir.chdir()
|
|
|
|
assert str(res) == str(old)
|
|
|
|
assert py.std.os.getcwd() == str(tmpdir)
|
|
|
|
finally:
|
|
|
|
old.chdir()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_ensure_filepath_withdir(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
newfile = tmpdir.join('test1','test')
|
|
|
|
newfile.ensure()
|
|
|
|
assert newfile.check(file=1)
|
|
|
|
newfile.write("42")
|
|
|
|
newfile.ensure()
|
2009-09-01 01:51:25 +08:00
|
|
|
s = newfile.read()
|
|
|
|
assert s == "42"
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_ensure_filepath_withoutdir(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
newfile = tmpdir.join('test1file')
|
|
|
|
t = newfile.ensure()
|
|
|
|
assert t == newfile
|
|
|
|
assert newfile.check(file=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_ensure_dirpath(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
newfile = tmpdir.join('test1','testfile')
|
|
|
|
t = newfile.ensure(dir=1)
|
|
|
|
assert t == newfile
|
|
|
|
assert newfile.check(dir=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_init_from_path(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
l = local()
|
|
|
|
l2 = local(l)
|
|
|
|
assert l2 is l
|
|
|
|
|
|
|
|
wc = py.path.svnwc('.')
|
|
|
|
l3 = local(wc)
|
|
|
|
assert l3 is not wc
|
|
|
|
assert l3.strpath == wc.strpath
|
|
|
|
assert not hasattr(l3, 'commit')
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_long_filenames(self, tmpdir):
|
2008-08-21 18:00:33 +08:00
|
|
|
if sys.platform == "win32":
|
|
|
|
py.test.skip("win32: work around needed for path length limit")
|
|
|
|
# see http://codespeak.net/pipermail/py-dev/2008q2/000922.html
|
|
|
|
|
2008-07-29 18:07:41 +08:00
|
|
|
# testing paths > 260 chars (which is Windows' limitation, but
|
|
|
|
# depending on how the paths are used), but > 4096 (which is the
|
|
|
|
# Linux' limitation) - the behaviour of paths with names > 4096 chars
|
|
|
|
# is undetermined
|
|
|
|
newfilename = '/test' * 60
|
|
|
|
l = tmpdir.join(newfilename)
|
|
|
|
l.ensure(file=True)
|
|
|
|
l.write('foo')
|
|
|
|
l2 = tmpdir.join(newfilename)
|
|
|
|
assert l2.read() == 'foo'
|
|
|
|
|
2009-09-23 01:13:33 +08:00
|
|
|
def test_visit_depth_first(self, tmpdir):
|
|
|
|
p1 = tmpdir.ensure("a","1")
|
|
|
|
p2 = tmpdir.ensure("b","2")
|
|
|
|
p3 = tmpdir.ensure("breadth")
|
|
|
|
l = list(tmpdir.visit(lambda x: x.check(file=1)))
|
|
|
|
assert l[0] == p1
|
|
|
|
assert l[1] == p2
|
|
|
|
assert l[2] == p3
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
class TestExecutionOnWindows:
|
2009-10-23 00:37:24 +08:00
|
|
|
pytestmark = py.test.mark.skipif("sys.platform != 'win32'")
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_sysfind(self):
|
|
|
|
x = py.path.local.sysfind('cmd')
|
|
|
|
assert x.check(file=1)
|
|
|
|
assert py.path.local.sysfind('jaksdkasldqwe') is None
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
class TestExecution:
|
2009-10-23 00:37:24 +08:00
|
|
|
pytestmark = py.test.mark.skipif("sys.platform == 'win32'")
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_sysfind(self):
|
|
|
|
x = py.path.local.sysfind('test')
|
|
|
|
assert x.check(file=1)
|
|
|
|
assert py.path.local.sysfind('jaksdkasldqwe') is None
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_sysfind_no_permisson_ignored(self, monkeypatch, tmpdir):
|
|
|
|
noperm = tmpdir.ensure('noperm', dir=True)
|
|
|
|
monkeypatch.setenv("PATH", noperm, prepend=":")
|
|
|
|
noperm.chmod(0)
|
|
|
|
assert py.path.local.sysfind('jaksdkasldqwe') is None
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_sysfind_absolute(self):
|
|
|
|
x = py.path.local.sysfind('test')
|
|
|
|
assert x.check(file=1)
|
|
|
|
y = py.path.local.sysfind(str(x))
|
|
|
|
assert y.check(file=1)
|
|
|
|
assert y == x
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_sysfind_multiple(self, tmpdir, monkeypatch):
|
|
|
|
monkeypatch.setenv('PATH',
|
|
|
|
"%s:%s" % (tmpdir.ensure('a'),
|
|
|
|
tmpdir.join('b')),
|
|
|
|
prepend=":")
|
|
|
|
tmpdir.ensure('b', 'a')
|
|
|
|
checker = lambda x: x.dirpath().basename == 'b'
|
|
|
|
x = py.path.local.sysfind('a', checker=checker)
|
|
|
|
assert x.basename == 'a'
|
|
|
|
assert x.dirpath().basename == 'b'
|
|
|
|
checker = lambda x: None
|
|
|
|
assert py.path.local.sysfind('a', checker=checker) is None
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_sysexec(self):
|
|
|
|
x = py.path.local.sysfind('ls')
|
|
|
|
out = x.sysexec('-a')
|
|
|
|
for x in py.path.local().listdir():
|
|
|
|
assert out.find(x.basename) != -1
|
|
|
|
|
|
|
|
def test_sysexec_failing(self):
|
|
|
|
x = py.path.local.sysfind('false')
|
|
|
|
py.test.raises(py.process.cmdexec.Error, """
|
|
|
|
x.sysexec('aksjdkasjd')
|
|
|
|
""")
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_make_numbered_dir(self, tmpdir):
|
|
|
|
tmpdir.ensure('base.not_an_int', dir=1)
|
2007-01-24 22:24:01 +08:00
|
|
|
for i in range(10):
|
2009-09-01 01:51:25 +08:00
|
|
|
numdir = local.make_numbered_dir(prefix='base.', rootdir=tmpdir,
|
2007-01-24 22:24:01 +08:00
|
|
|
keep=2, lock_timeout=0)
|
|
|
|
assert numdir.check()
|
|
|
|
assert numdir.basename == 'base.%d' %i
|
|
|
|
if i>=1:
|
|
|
|
assert numdir.new(ext=str(i-1)).check()
|
|
|
|
if i>=2:
|
|
|
|
assert numdir.new(ext=str(i-2)).check()
|
|
|
|
if i>=3:
|
|
|
|
assert not numdir.new(ext=str(i-3)).check()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_locked_make_numbered_dir(self, tmpdir):
|
2007-01-24 22:24:01 +08:00
|
|
|
for i in range(10):
|
2009-09-01 01:51:25 +08:00
|
|
|
numdir = local.make_numbered_dir(prefix='base2.', rootdir=tmpdir,
|
2007-01-24 22:24:01 +08:00
|
|
|
keep=2)
|
|
|
|
assert numdir.check()
|
2007-02-09 04:56:12 +08:00
|
|
|
assert numdir.basename == 'base2.%d' %i
|
2007-01-24 22:24:01 +08:00
|
|
|
for j in range(i):
|
|
|
|
assert numdir.new(ext=str(j)).check()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_error_preservation(self, path1):
|
|
|
|
py.test.raises (EnvironmentError, path1.join('qwoeqiwe').mtime)
|
|
|
|
py.test.raises (EnvironmentError, path1.join('qwoeqiwe').read)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
#def test_parentdirmatch(self):
|
|
|
|
# local.parentdirmatch('std', startmodule=__name__)
|
|
|
|
#
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
|
|
|
|
class TestImport:
|
|
|
|
def test_pyimport(self, path1):
|
|
|
|
obj = path1.join('execfile.py').pyimport()
|
2007-01-24 22:24:01 +08:00
|
|
|
assert obj.x == 42
|
|
|
|
assert obj.__name__ == 'execfile'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_execfile_different_name(self, path1):
|
|
|
|
obj = path1.join('execfile.py').pyimport(modname="0x.y.z")
|
2007-01-24 22:24:01 +08:00
|
|
|
assert obj.x == 42
|
|
|
|
assert obj.__name__ == '0x.y.z'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_a(self, path1):
|
|
|
|
otherdir = path1.join('otherdir')
|
2007-01-24 22:24:01 +08:00
|
|
|
mod = otherdir.join('a.py').pyimport()
|
|
|
|
assert mod.result == "got it"
|
|
|
|
assert mod.__name__ == 'otherdir.a'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_b(self, path1):
|
|
|
|
otherdir = path1.join('otherdir')
|
2007-01-24 22:24:01 +08:00
|
|
|
mod = otherdir.join('b.py').pyimport()
|
|
|
|
assert mod.stuff == "got it"
|
|
|
|
assert mod.__name__ == 'otherdir.b'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_c(self, path1):
|
|
|
|
otherdir = path1.join('otherdir')
|
2007-01-24 22:24:01 +08:00
|
|
|
mod = otherdir.join('c.py').pyimport()
|
|
|
|
assert mod.value == "got it"
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_d(self, path1):
|
|
|
|
otherdir = path1.join('otherdir')
|
2007-01-24 22:24:01 +08:00
|
|
|
mod = otherdir.join('d.py').pyimport()
|
|
|
|
assert mod.value2 == "got it"
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_pyimport_and_import(self, tmpdir):
|
|
|
|
tmpdir.ensure('xxxpackage', '__init__.py')
|
|
|
|
mod1path = tmpdir.ensure('xxxpackage', 'module1.py')
|
2007-01-24 22:24:01 +08:00
|
|
|
mod1 = mod1path.pyimport()
|
|
|
|
assert mod1.__name__ == 'xxxpackage.module1'
|
|
|
|
from xxxpackage import module1
|
|
|
|
assert module1 is mod1
|
|
|
|
|
2009-10-29 02:51:20 +08:00
|
|
|
def test_pyimport_check_filepath_consistency(self, monkeypatch, tmpdir):
|
|
|
|
name = 'pointsback123'
|
2009-10-30 06:46:14 +08:00
|
|
|
ModuleType = type(py.std.os)
|
2009-10-29 02:51:20 +08:00
|
|
|
p = tmpdir.ensure(name + '.py')
|
|
|
|
for ending in ('.pyc', '$py.class', '.pyo'):
|
|
|
|
mod = ModuleType(name)
|
|
|
|
pseudopath = tmpdir.ensure(name+ending)
|
|
|
|
mod.__file__ = str(pseudopath)
|
|
|
|
monkeypatch.setitem(sys.modules, name, mod)
|
|
|
|
newmod = p.pyimport()
|
|
|
|
assert mod == newmod
|
|
|
|
monkeypatch.undo()
|
|
|
|
mod = ModuleType(name)
|
|
|
|
pseudopath = tmpdir.ensure(name+"123.py")
|
|
|
|
mod.__file__ = str(pseudopath)
|
|
|
|
monkeypatch.setitem(sys.modules, name, mod)
|
|
|
|
excinfo = py.test.raises(EnvironmentError, "p.pyimport()")
|
|
|
|
s = str(excinfo.value)
|
|
|
|
assert "mismatch" in s
|
|
|
|
assert name+"123" in s
|
|
|
|
|
2009-09-06 05:21:58 +08:00
|
|
|
def test_pypkgdir(tmpdir):
|
|
|
|
pkg = tmpdir.ensure('pkg1', dir=1)
|
2007-01-24 22:24:01 +08:00
|
|
|
pkg.ensure("__init__.py")
|
|
|
|
pkg.ensure("subdir/__init__.py")
|
|
|
|
assert pkg.pypkgpath() == pkg
|
|
|
|
assert pkg.join('subdir', '__init__.py').pypkgpath() == pkg
|
|
|
|
|
2007-02-05 08:35:40 +08:00
|
|
|
def test_homedir():
|
|
|
|
homedir = py.path.local._gethomedir()
|
|
|
|
assert homedir.check(dir=1)
|
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_samefile(tmpdir):
|
|
|
|
assert tmpdir.samefile(tmpdir)
|
|
|
|
p = tmpdir.ensure("hello")
|
|
|
|
assert p.samefile(p)
|
|
|
|
|
2009-08-20 23:37:06 +08:00
|
|
|
class TestWINLocalPath:
|
2009-10-23 00:37:24 +08:00
|
|
|
pytestmark = py.test.mark.skipif("sys.platform != 'win32'")
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_owner_group_not_implemented(self, path1):
|
2009-09-01 01:51:25 +08:00
|
|
|
py.test.raises(NotImplementedError, "path1.stat().owner")
|
|
|
|
py.test.raises(NotImplementedError, "path1.stat().group")
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_chmod_simple_int(self, path1):
|
2009-09-01 01:51:25 +08:00
|
|
|
py.builtin.print_("path1 is", path1)
|
|
|
|
mode = path1.stat().mode
|
2009-08-20 23:37:06 +08:00
|
|
|
# Ensure that we actually change the mode to something different.
|
2009-09-01 01:51:25 +08:00
|
|
|
path1.chmod(mode == 0 and 1 or 0)
|
2009-08-20 23:37:06 +08:00
|
|
|
try:
|
2009-09-01 01:51:25 +08:00
|
|
|
print(path1.stat().mode)
|
2009-08-30 02:04:48 +08:00
|
|
|
print(mode)
|
2009-09-01 01:51:25 +08:00
|
|
|
assert path1.stat().mode != mode
|
2009-08-20 23:37:06 +08:00
|
|
|
finally:
|
2009-09-01 01:51:25 +08:00
|
|
|
path1.chmod(mode)
|
|
|
|
assert path1.stat().mode == mode
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_path_comparison_lowercase_mixed(self, path1):
|
2009-09-01 01:51:25 +08:00
|
|
|
t1 = path1.join("a_path")
|
|
|
|
t2 = path1.join("A_path")
|
2009-08-20 23:37:06 +08:00
|
|
|
assert t1 == t1
|
|
|
|
assert t1 == t2
|
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_relto_with_mixed_case(self, path1):
|
2009-09-01 01:51:25 +08:00
|
|
|
t1 = path1.join("a_path", "fiLe")
|
|
|
|
t2 = path1.join("A_path")
|
2009-08-20 23:37:06 +08:00
|
|
|
assert t1.relto(t2) == "fiLe"
|
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_allow_unix_style_paths(self, path1):
|
2009-09-01 01:51:25 +08:00
|
|
|
t1 = path1.join('a_path')
|
|
|
|
assert t1 == str(path1) + '\\a_path'
|
|
|
|
t1 = path1.join('a_path/')
|
|
|
|
assert t1 == str(path1) + '\\a_path'
|
|
|
|
t1 = path1.join('dir/a_path')
|
|
|
|
assert t1 == str(path1) + '\\dir\\a_path'
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-10-30 03:10:05 +08:00
|
|
|
def test_sysfind_in_currentdir(self, path1):
|
2009-08-20 23:37:06 +08:00
|
|
|
cmd = py.path.local.sysfind('cmd')
|
|
|
|
root = cmd.new(dirname='', basename='') # c:\ in most installations
|
|
|
|
old = root.chdir()
|
|
|
|
try:
|
|
|
|
x = py.path.local.sysfind(cmd.relto(root))
|
|
|
|
assert x.check(file=1)
|
|
|
|
finally:
|
|
|
|
old.chdir()
|
|
|
|
|
|
|
|
class TestPOSIXLocalPath:
|
2009-10-23 00:37:24 +08:00
|
|
|
pytestmark = py.test.mark.skipif("sys.platform == 'win32'")
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_hardlink(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
filepath = tmpdir.join('file')
|
|
|
|
filepath.write("Hello")
|
|
|
|
nlink = filepath.stat().nlink
|
|
|
|
linkpath.mklinkto(filepath)
|
|
|
|
assert filepath.stat().nlink == nlink + 1
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_are_identical(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
filepath = tmpdir.join('file')
|
|
|
|
filepath.write("Hello")
|
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
linkpath.mksymlinkto(filepath)
|
|
|
|
assert linkpath.readlink() == str(filepath)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_isfile(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
filepath = tmpdir.join('file')
|
|
|
|
filepath.write("")
|
|
|
|
linkpath.mksymlinkto(filepath)
|
|
|
|
assert linkpath.check(file=1)
|
|
|
|
assert not linkpath.check(link=0, file=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_relative(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
filepath = tmpdir.join('file')
|
|
|
|
filepath.write("Hello")
|
|
|
|
linkpath.mksymlinkto(filepath, absolute=False)
|
|
|
|
assert linkpath.readlink() == "file"
|
|
|
|
assert filepath.read() == linkpath.read()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_not_existing(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('testnotexisting')
|
|
|
|
assert not linkpath.check(link=1)
|
|
|
|
assert linkpath.check(link=0)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_relto_with_root(self, path1, tmpdir):
|
|
|
|
y = path1.join('x').relto(py.path.local('/'))
|
|
|
|
assert y[0] == str(path1)[1]
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_visit_recursive_symlink(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
linkpath.mksymlinkto(tmpdir)
|
|
|
|
visitor = tmpdir.visit(None, lambda x: x.check(link=0))
|
|
|
|
assert list(visitor) == [linkpath]
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_isdir(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
linkpath.mksymlinkto(tmpdir)
|
|
|
|
assert linkpath.check(dir=1)
|
|
|
|
assert not linkpath.check(link=0, dir=1)
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_symlink_remove(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
linkpath.mksymlinkto(linkpath) # point to itself
|
|
|
|
assert linkpath.check(link=1)
|
|
|
|
linkpath.remove()
|
|
|
|
assert not linkpath.check()
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_realpath_file(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
linkpath = tmpdir.join('test')
|
|
|
|
filepath = tmpdir.join('file')
|
|
|
|
filepath.write("")
|
|
|
|
linkpath.mksymlinkto(filepath)
|
|
|
|
realpath = linkpath.realpath()
|
|
|
|
assert realpath.basename == 'file'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_owner(self, path1, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
from pwd import getpwuid
|
|
|
|
from grp import getgrgid
|
2009-09-01 01:51:25 +08:00
|
|
|
stat = path1.stat()
|
|
|
|
assert stat.path == path1
|
2009-08-20 23:37:06 +08:00
|
|
|
|
|
|
|
uid = stat.uid
|
|
|
|
gid = stat.gid
|
|
|
|
owner = getpwuid(uid)[0]
|
|
|
|
group = getgrgid(gid)[0]
|
|
|
|
|
|
|
|
assert uid == stat.uid
|
|
|
|
assert owner == stat.owner
|
|
|
|
assert gid == stat.gid
|
|
|
|
assert group == stat.group
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_atime(self, tmpdir):
|
2009-08-20 23:37:06 +08:00
|
|
|
import time
|
2009-09-01 01:51:25 +08:00
|
|
|
path = tmpdir.ensure('samplefile')
|
2009-08-20 23:37:06 +08:00
|
|
|
now = time.time()
|
|
|
|
atime1 = path.atime()
|
|
|
|
# we could wait here but timer resolution is very
|
|
|
|
# system dependent
|
|
|
|
path.read()
|
|
|
|
atime2 = path.atime()
|
|
|
|
duration = time.time() - now
|
|
|
|
assert (atime2-atime1) <= duration
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_commondir(self, path1):
|
2009-08-20 23:37:06 +08:00
|
|
|
# XXX This is here in local until we find a way to implement this
|
|
|
|
# using the subversion command line api.
|
2009-09-01 01:51:25 +08:00
|
|
|
p1 = path1.join('something')
|
|
|
|
p2 = path1.join('otherthing')
|
|
|
|
assert p1.common(p2) == path1
|
|
|
|
assert p2.common(p1) == path1
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_commondir_nocommon(self, path1):
|
2009-08-20 23:37:06 +08:00
|
|
|
# XXX This is here in local until we find a way to implement this
|
|
|
|
# using the subversion command line api.
|
2009-09-01 01:51:25 +08:00
|
|
|
p1 = path1.join('something')
|
|
|
|
p2 = py.path.local(path1.sep+'blabla')
|
2009-08-20 23:37:06 +08:00
|
|
|
assert p1.common(p2) == '/'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_join_to_root(self, path1):
|
|
|
|
root = path1.parts()[0]
|
2009-08-20 23:37:06 +08:00
|
|
|
assert len(str(root)) == 1
|
|
|
|
assert str(root.join('a')) == '/a'
|
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_join_root_to_root_with_no_abs(self, path1):
|
|
|
|
nroot = path1.join('/')
|
|
|
|
assert str(path1) == str(nroot)
|
|
|
|
assert path1 == nroot
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chmod_simple_int(self, path1):
|
|
|
|
mode = path1.stat().mode
|
|
|
|
path1.chmod(int(mode/2))
|
2009-08-20 23:37:06 +08:00
|
|
|
try:
|
2009-09-01 01:51:25 +08:00
|
|
|
assert path1.stat().mode != mode
|
2009-08-20 23:37:06 +08:00
|
|
|
finally:
|
2009-09-01 01:51:25 +08:00
|
|
|
path1.chmod(mode)
|
|
|
|
assert path1.stat().mode == mode
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chmod_rec_int(self, path1):
|
2009-08-20 23:37:06 +08:00
|
|
|
# XXX fragile test
|
|
|
|
recfilter = lambda x: x.check(dotfile=0, link=0)
|
|
|
|
oldmodes = {}
|
2009-09-01 01:51:25 +08:00
|
|
|
for x in path1.visit(rec=recfilter):
|
2009-08-20 23:37:06 +08:00
|
|
|
oldmodes[x] = x.stat().mode
|
2009-09-01 01:51:25 +08:00
|
|
|
path1.chmod(int("772", 8), rec=recfilter)
|
2009-08-20 23:37:06 +08:00
|
|
|
try:
|
2009-09-01 01:51:25 +08:00
|
|
|
for x in path1.visit(rec=recfilter):
|
2009-08-30 02:04:48 +08:00
|
|
|
assert x.stat().mode & int("777", 8) == int("772", 8)
|
2009-08-20 23:37:06 +08:00
|
|
|
finally:
|
|
|
|
for x,y in oldmodes.items():
|
|
|
|
x.chmod(y)
|
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@failsonjython
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chown_identity(self, path1):
|
|
|
|
owner = path1.stat().owner
|
|
|
|
group = path1.stat().group
|
|
|
|
path1.chown(owner, group)
|
2009-08-20 23:37:06 +08:00
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@failsonjython
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chown_dangling_link(self, path1):
|
|
|
|
owner = path1.stat().owner
|
|
|
|
group = path1.stat().group
|
|
|
|
x = path1.join('hello')
|
2009-08-20 23:37:06 +08:00
|
|
|
x.mksymlinkto('qlwkejqwlek')
|
|
|
|
try:
|
2009-09-01 01:51:25 +08:00
|
|
|
path1.chown(owner, group, rec=1)
|
2009-08-20 23:37:06 +08:00
|
|
|
finally:
|
|
|
|
x.remove(rec=0)
|
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@failsonjython
|
2009-09-01 01:51:25 +08:00
|
|
|
def test_chown_identity_rec_mayfail(self, path1):
|
|
|
|
owner = path1.stat().owner
|
|
|
|
group = path1.stat().group
|
|
|
|
path1.chown(owner, group)
|