2
from py.__.path.testing import common
5
if path.join('samplefile').check():
7
#print "setting up test fs for", repr(path)
8
samplefile = path.ensure('samplefile')
9
samplefile.write('samplefile\n')
11
execfile = path.ensure('execfile')
12
execfile.write('x=42')
14
execfilepy = path.ensure('execfile.py')
15
execfilepy.write('x=42')
17
d = {1:2, 'hello': 'world', 'answer': 42}
18
path.ensure('samplepickle').dump(d)
20
sampledir = path.ensure('sampledir', dir=1)
21
sampledir.ensure('otherfile')
23
otherdir = path.ensure('otherdir', dir=1)
24
otherdir.ensure('__init__.py')
26
module_a = otherdir.ensure('a.py')
27
module_a.write('from b import stuff as result\n')
28
module_b = otherdir.ensure('b.py')
29
module_b.write('stuff="got it"\n')
30
module_c = otherdir.ensure('c.py')
31
module_c.write('''import py; py.magic.autopath()
33
value = otherdir.a.result
35
module_d = otherdir.ensure('d.py')
36
module_d.write('''import py; py.magic.autopath()
37
from otherdir import a
41
class CommonFSTests(common.CommonPathTests):
42
root = None # subclasses have to provide a current 'root' attribute
44
def test_join_div_operator(self):
45
newpath = self.root / '/sampledir' / '/test//'
46
newpath2 = self.root.join('sampledir', 'test')
47
assert newpath == newpath2
50
newpath = self.root.join('sampledir.ext')
51
assert newpath.ext == '.ext'
52
newpath = self.root.join('sampledir')
53
assert not newpath.ext
55
def test_purebasename(self):
56
newpath = self.root.join('samplefile.py')
57
assert newpath.purebasename == 'samplefile'
59
def test_multiple_parts(self):
60
newpath = self.root.join('samplefile.py')
61
dirname, purebasename, basename, ext = newpath._getbyspec(
62
'dirname,purebasename,basename,ext')
63
assert str(self.root).endswith(dirname) # be careful with win32 'drive'
64
assert purebasename == 'samplefile'
65
assert basename == 'samplefile.py'
68
def test_dotted_name_ext(self):
69
newpath = self.root.join('a.b.c')
72
assert newpath.ext == '.c'
74
def test_newext(self):
75
newpath = self.root.join('samplefile.py')
76
newext = newpath.new(ext='.txt')
77
assert newext.basename == "samplefile.txt"
78
assert newext.purebasename == "samplefile"
80
def test_readlines(self):
81
fn = self.root.join('samplefile')
82
contents = fn.readlines()
83
assert contents == ['samplefile\n']
85
def test_readlines_nocr(self):
86
fn = self.root.join('samplefile')
87
contents = fn.readlines(cr=0)
88
assert contents == ['samplefile', '']
91
assert self.root.join('samplefile').check(file=1)
93
def test_not_file(self):
94
assert not self.root.join("sampledir").check(file=1)
95
assert self.root.join("sampledir").check(file=0)
97
#def test_fnmatch_dir(self):
99
def test_non_existent(self):
100
assert self.root.join("sampledir.nothere").check(dir=0)
101
assert self.root.join("sampledir.nothere").check(file=0)
102
assert self.root.join("sampledir.nothere").check(notfile=1)
103
assert self.root.join("sampledir.nothere").check(notdir=1)
104
assert self.root.join("sampledir.nothere").check(notexists=1)
105
assert not self.root.join("sampledir.nothere").check(notfile=0)
107
# pattern = self.root.sep.join(['s*file'])
108
# sfile = self.root.join("samplefile")
109
# assert sfile.check(fnmatch=pattern)
112
url = self.root.join("samplefile")
113
assert url.size() > len("samplefile")
115
def test_mtime(self):
116
url = self.root.join("samplefile")
117
assert url.mtime() > 0
119
def test_relto_wrong_type(self):
120
py.test.raises(TypeError, "self.root.relto(42)")
122
def test_visit_filesonly(self):
124
for i in self.root.visit(lambda x: x.check(file=1)):
125
l.append(i.relto(self.root))
126
assert not "sampledir" in l
127
assert self.root.sep.join(["sampledir", "otherfile"]) in l
130
p = self.root.join('samplepickle')
132
assert type(obj) is dict
133
assert obj.get('answer',None) == 42
135
def test_visit_nodotfiles(self):
137
for i in self.root.visit(lambda x: x.check(dotfile=0)):
138
l.append(i.relto(self.root))
139
assert "sampledir" in l
140
assert self.root.sep.join(["sampledir", "otherfile"]) in l
141
assert not ".dotfile" in l
143
def test_endswith(self):
145
return p.check(endswith="pickle")
146
assert not chk(self.root)
147
assert not chk(self.root.join('samplefile'))
148
assert chk(self.root.join('somepickle'))
150
def test_copy_file(self):
151
otherdir = self.root.join('otherdir')
152
initpy = otherdir.join('__init__.py')
153
copied = otherdir.join('copied')
156
assert copied.check()
164
def test_copy_dir(self):
165
otherdir = self.root.join('otherdir')
166
copied = self.root.join('newdir')
168
otherdir.copy(copied)
169
assert copied.check(dir=1)
170
assert copied.join('__init__.py').check(file=1)
171
s1 = otherdir.join('__init__.py').read()
172
s2 = copied.join('__init__.py').read()
175
if copied.check(dir=1):
178
def test_remove_file(self):
179
d = self.root.ensure('todeleted')
184
def test_remove_dir_recursive_by_default(self):
185
d = self.root.ensure('to', 'be', 'deleted')
187
p = self.root.join('to')
191
def test_mkdir_and_remove(self):
193
py.test.raises(py.error.EEXIST, tmpdir.mkdir, 'sampledir')
194
new = tmpdir.join('mktest1')
196
assert new.check(dir=1)
199
new = tmpdir.mkdir('mktest')
200
assert new.check(dir=1)
202
assert tmpdir.join('mktest') == new
204
def test_move_file(self):
205
p = self.root.join('samplefile')
206
newp = p.dirpath('moved_samplefile')
208
assert newp.check(file=1)
211
def test_move_directory(self):
212
source = self.root.join('sampledir')
213
dest = self.root.join('moveddir')
215
assert dest.check(dir=1)
216
assert dest.join('otherfile').check(file=1)
217
assert not source.join('sampledir').check()
219
def test__getpymodule(self):
220
obj = self.root.join('execfile')._getpymodule()
223
def test_not_has_resolve(self):
224
# because this would mean confusion with respect to
226
assert not hasattr(self.root, 'resolve')
228
def test__getpymodule_a(self):
229
otherdir = self.root.join('otherdir')
230
mod = otherdir.join('a.py')._getpymodule()
231
assert mod.result == "got it"
233
def test__getpymodule_b(self):
234
otherdir = self.root.join('otherdir')
235
mod = otherdir.join('b.py')._getpymodule()
236
assert mod.stuff == "got it"
238
def test__getpymodule_c(self):
239
otherdir = self.root.join('otherdir')
240
mod = otherdir.join('c.py')._getpymodule()
241
assert mod.value == "got it"
243
def test__getpymodule_d(self):
244
otherdir = self.root.join('otherdir')
245
mod = otherdir.join('d.py')._getpymodule()
246
assert mod.value2 == "got it"