~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to py/path/testing/fscommon.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py
 
2
from py.__.path.testing import common 
 
3
 
 
4
def setuptestfs(path):
 
5
    if path.join('samplefile').check():
 
6
        return
 
7
    #print "setting up test fs for", repr(path)
 
8
    samplefile = path.ensure('samplefile')
 
9
    samplefile.write('samplefile\n')
 
10
 
 
11
    execfile = path.ensure('execfile')
 
12
    execfile.write('x=42')
 
13
 
 
14
    execfilepy = path.ensure('execfile.py')
 
15
    execfilepy.write('x=42')
 
16
 
 
17
    d = {1:2, 'hello': 'world', 'answer': 42}
 
18
    path.ensure('samplepickle').dump(d)
 
19
 
 
20
    sampledir = path.ensure('sampledir', dir=1)
 
21
    sampledir.ensure('otherfile')
 
22
 
 
23
    otherdir = path.ensure('otherdir', dir=1)
 
24
    otherdir.ensure('__init__.py')
 
25
 
 
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()
 
32
import otherdir.a
 
33
value = otherdir.a.result
 
34
''')
 
35
    module_d = otherdir.ensure('d.py')
 
36
    module_d.write('''import py; py.magic.autopath()
 
37
from otherdir import a
 
38
value2 = a.result
 
39
''')
 
40
 
 
41
class CommonFSTests(common.CommonPathTests):
 
42
    root = None  # subclasses have to provide a current 'root' attribute
 
43
 
 
44
    def test_join_div_operator(self):
 
45
        newpath = self.root / '/sampledir' / '/test//'
 
46
        newpath2 = self.root.join('sampledir', 'test')
 
47
        assert newpath == newpath2
 
48
 
 
49
    def test_ext(self):
 
50
        newpath = self.root.join('sampledir.ext')
 
51
        assert newpath.ext == '.ext'
 
52
        newpath = self.root.join('sampledir')
 
53
        assert not newpath.ext
 
54
 
 
55
    def test_purebasename(self):
 
56
        newpath = self.root.join('samplefile.py')
 
57
        assert newpath.purebasename == 'samplefile'
 
58
 
 
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'
 
66
        assert ext == '.py'
 
67
 
 
68
    def test_dotted_name_ext(self):
 
69
        newpath = self.root.join('a.b.c')
 
70
        ext = newpath.ext
 
71
        assert ext == '.c'
 
72
        assert newpath.ext == '.c'
 
73
 
 
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"
 
79
 
 
80
    def test_readlines(self):
 
81
        fn = self.root.join('samplefile')
 
82
        contents = fn.readlines()
 
83
        assert contents == ['samplefile\n']
 
84
 
 
85
    def test_readlines_nocr(self):
 
86
        fn = self.root.join('samplefile')
 
87
        contents = fn.readlines(cr=0)
 
88
        assert contents == ['samplefile', '']
 
89
 
 
90
    def test_file(self):
 
91
        assert self.root.join('samplefile').check(file=1)
 
92
 
 
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)
 
96
 
 
97
    #def test_fnmatch_dir(self):
 
98
 
 
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)
 
106
 
 
107
    #    pattern = self.root.sep.join(['s*file'])
 
108
    #    sfile = self.root.join("samplefile")
 
109
    #    assert sfile.check(fnmatch=pattern)
 
110
 
 
111
    def test_size(self):
 
112
        url = self.root.join("samplefile")
 
113
        assert url.size() > len("samplefile")
 
114
 
 
115
    def test_mtime(self):
 
116
        url = self.root.join("samplefile")
 
117
        assert url.mtime() > 0
 
118
 
 
119
    def test_relto_wrong_type(self): 
 
120
        py.test.raises(TypeError, "self.root.relto(42)")
 
121
 
 
122
    def test_visit_filesonly(self):
 
123
        l = []
 
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
 
128
 
 
129
    def test_load(self):
 
130
        p = self.root.join('samplepickle')
 
131
        obj = p.load()
 
132
        assert type(obj) is dict
 
133
        assert obj.get('answer',None) == 42
 
134
 
 
135
    def test_visit_nodotfiles(self):
 
136
        l = []
 
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
 
142
 
 
143
    def test_endswith(self):
 
144
        def chk(p):
 
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'))
 
149
 
 
150
    def test_copy_file(self):
 
151
        otherdir = self.root.join('otherdir')
 
152
        initpy = otherdir.join('__init__.py')
 
153
        copied = otherdir.join('copied')
 
154
        initpy.copy(copied)
 
155
        try:
 
156
            assert copied.check()
 
157
            s1 = initpy.read()
 
158
            s2 = copied.read()
 
159
            assert s1 == s2
 
160
        finally:
 
161
            if copied.check():
 
162
                copied.remove()
 
163
 
 
164
    def test_copy_dir(self):
 
165
        otherdir = self.root.join('otherdir')
 
166
        copied = self.root.join('newdir')
 
167
        try:
 
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()
 
173
            assert s1 == s2
 
174
        finally:
 
175
            if copied.check(dir=1):
 
176
                copied.remove(rec=1)
 
177
 
 
178
    def test_remove_file(self):
 
179
        d = self.root.ensure('todeleted')
 
180
        assert d.check()
 
181
        d.remove()
 
182
        assert not d.check()
 
183
 
 
184
    def test_remove_dir_recursive_by_default(self):
 
185
        d = self.root.ensure('to', 'be', 'deleted')
 
186
        assert d.check()
 
187
        p = self.root.join('to')
 
188
        p.remove()
 
189
        assert not p.check()
 
190
 
 
191
    def test_mkdir_and_remove(self):
 
192
        tmpdir = self.root
 
193
        py.test.raises(py.error.EEXIST, tmpdir.mkdir, 'sampledir')
 
194
        new = tmpdir.join('mktest1')
 
195
        new.mkdir()
 
196
        assert new.check(dir=1)
 
197
        new.remove()
 
198
 
 
199
        new = tmpdir.mkdir('mktest')
 
200
        assert new.check(dir=1)
 
201
        new.remove()
 
202
        assert tmpdir.join('mktest') == new
 
203
 
 
204
    def test_move_file(self):
 
205
        p = self.root.join('samplefile')
 
206
        newp = p.dirpath('moved_samplefile')
 
207
        p.move(newp)
 
208
        assert newp.check(file=1)
 
209
        assert not p.check()
 
210
 
 
211
    def test_move_directory(self):
 
212
        source = self.root.join('sampledir') 
 
213
        dest = self.root.join('moveddir') 
 
214
        source.move(dest)
 
215
        assert dest.check(dir=1)
 
216
        assert dest.join('otherfile').check(file=1) 
 
217
        assert not source.join('sampledir').check()
 
218
 
 
219
    def test__getpymodule(self):
 
220
        obj = self.root.join('execfile')._getpymodule()
 
221
        assert obj.x == 42
 
222
 
 
223
    def test_not_has_resolve(self):
 
224
        # because this would mean confusion with respect to
 
225
        # py.path.extpy
 
226
        assert not hasattr(self.root, 'resolve')
 
227
 
 
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"
 
232
 
 
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"
 
237
 
 
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"
 
242
 
 
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"