1
# Copyright (C) 2005, 2010 Canonical Ltd
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
"""Test helper for constructing and testing directories.
20
This module transforms filesystem directories to and from Python lists.
20
This module transforms filesystem directories to and from Python lists.
21
21
As a Python list the descriptions can be stored in test cases, compared,
25
25
# TODO: Script to write a description of a directory for testing
26
26
# TODO: Helper that compares two structures and raises a helpful error
27
# where they differ. Option to ignore some files or directories in the
27
# where they differ. Option to ignore some files or directories in the
33
from ..trace import warning
34
from ..osutils import pathjoin
33
from bzrlib.trace import warning
34
from bzrlib.osutils import pathjoin
37
36
def build_tree_contents(template):
38
37
"""Reconstitute some files from a text description.
43
42
The template is built relative to the Python process's current
46
('foo/',) will build a directory.
47
('foo', 'bar') will write 'bar' to 'foo'
48
('foo@', 'linktarget') will raise an error
50
45
for tt in template:
52
47
if name[-1] == '/':
54
49
elif name[-1] == '@':
55
os.symlink(tt[1], tt[0][:-1])
50
raise NotImplementedError('symlinks not handled yet')
57
with open(name, 'w' + ('b' if isinstance(tt[1], bytes) else '')) as f:
61
59
def capture_tree_contents(top):
62
60
"""Make a Python datastructure description of a tree.
64
62
If top is an absolute path the descriptions will be absolute."""
65
63
for dirpath, dirnames, filenames in os.walk(top):
66
64
yield (dirpath + '/', )
68
66
for fn in filenames:
69
67
fullpath = pathjoin(dirpath, fn)
70
if (fullpath[-1] in '@/'):
71
raise AssertionError(fullpath)
68
assert not (fullpath[-1] in '@/')
72
69
info = os.lstat(fullpath)
73
70
if stat.S_ISLNK(info.st_mode):
74
71
yield (fullpath + '@', os.readlink(fullpath))
75
72
elif stat.S_ISREG(info.st_mode):
76
with open(fullpath, 'rb') as f:
78
yield (fullpath, file_bytes)
73
yield (fullpath, file(fullpath, 'rb').read())
80
75
warning("can't capture file %s with mode %#o",
81
76
fullpath, info.st_mode)