~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/workingtree_implementations/test_walkdirs.py

  • Committer: Alexander Belchenko
  • Date: 2007-11-19 22:54:30 UTC
  • mfrom: (3006 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3008.
  • Revision ID: bialix@ukr.net-20071119225430-x0ewosrsagis0yno
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
# tests to write:
26
26
# type mismatches - file to link, dir, dir to file, link, link to file, dir
27
27
 
 
28
class DirBlock:
 
29
    """Object representation of the tuples returned by dirstate."""
 
30
 
 
31
    def __init__(self, tree, file_path, file_name=None, id=None,
 
32
                 inventory_kind=None, stat=None, disk_kind='unknown'):
 
33
        self.file_path = file_path
 
34
        self.abspath = tree.abspath(file_path)
 
35
        self.relpath = tree.relpath(file_path)
 
36
        if file_name == None:
 
37
           file_name = os.path.split(file_path)[-1]
 
38
           if len(file_name) == 0:
 
39
               file_name = os.path.split(file_path)[-2]
 
40
        self.file_name = file_name
 
41
        self.id = id
 
42
        self.inventory_kind = inventory_kind
 
43
        self.stat = stat
 
44
        self.disk_kind = disk_kind
 
45
 
 
46
    def as_tuple(self):
 
47
         return (self.relpath, self.file_name, self.disk_kind,
 
48
                 self.stat, self.id, self.inventory_kind)
 
49
 
 
50
    def as_dir_tuple(self):
 
51
         return (self.relpath, self.id)
 
52
 
 
53
    def __str__(self):
 
54
        return """
 
55
file_path      = %r
 
56
abspath        = %r
 
57
relpath        = %r
 
58
file_name      = %r
 
59
id             = %r
 
60
inventory_kind = %r
 
61
stat           = %r
 
62
disk_kind      = %r""" % (self.file_path, self.abspath, self.relpath,
 
63
        self.file_name, self.id, self.inventory_kind, self.stat,
 
64
        self.disk_kind)
 
65
 
 
66
 
28
67
class TestWalkdirs(TestCaseWithWorkingTree):
29
68
 
30
 
    def get_tree_with_unknowns(self):
 
69
    added='added'
 
70
    missing='missing'
 
71
    unknown='unknown'
 
72
 
 
73
    def get_tree(self, file_status, prefix=None):
31
74
        tree = self.make_branch_and_tree('.')
32
 
        self.build_tree([
33
 
            'unknown file',
34
 
            'unknown dir/',
35
 
            'unknown dir/a file',
36
 
            ])
37
 
        u_f_stat = os.lstat('unknown file')
38
 
        u_d_stat = os.lstat('unknown dir')
39
 
        u_d_f_stat = os.lstat('unknown dir/a file')
 
75
        dirblocks = []
 
76
        paths = [
 
77
            file_status + ' file',
 
78
            file_status + ' dir/',
 
79
            file_status + ' dir/a file',
 
80
            file_status + ' empty dir/',
 
81
            ]
 
82
        self.build_tree(paths)
 
83
 
 
84
        def add_dirblock(path, kind):
 
85
            dirblock = DirBlock(tree, path)
 
86
            if file_status != self.unknown:
 
87
                dirblock.id = 'a ' + str(path).replace('/','-') + '-id'
 
88
                dirblock.inventory_kind = kind
 
89
            if file_status != self.missing:
 
90
                dirblock.disk_kind = kind
 
91
                dirblock.stat = os.lstat(dirblock.relpath)
 
92
            dirblocks.append(dirblock)
 
93
 
 
94
        add_dirblock(paths[0], 'file')
 
95
        add_dirblock(paths[1], 'directory')
 
96
        add_dirblock(paths[2], 'file')
 
97
        add_dirblock(paths[3], 'directory')
 
98
 
 
99
        if file_status != self.unknown:
 
100
            tree.add(paths, [db.id for db in dirblocks])
 
101
 
 
102
        if file_status == self.missing:
 
103
            # now make the files be missing
 
104
            tree.bzrdir.root_transport.delete(dirblocks[0].relpath)
 
105
            tree.bzrdir.root_transport.delete_tree(dirblocks[1].relpath)
 
106
            tree.bzrdir.root_transport.delete_tree(dirblocks[3].relpath)
 
107
 
40
108
        expected_dirblocks = [
41
109
            (('', tree.path2id('')),
42
 
             [
43
 
              ('unknown dir', 'unknown dir', 'directory', u_d_stat, None, None),
44
 
              ('unknown file', 'unknown file', 'file', u_f_stat, None, None),
45
 
             ]
46
 
            ),
47
 
            (('unknown dir', None),
48
 
             [('unknown dir/a file', 'a file', 'file', u_d_f_stat, None, None),
49
 
             ]
 
110
             [dirblocks[1].as_tuple(), dirblocks[3].as_tuple(),
 
111
              dirblocks[0].as_tuple()]
 
112
            ),
 
113
            (dirblocks[1].as_dir_tuple(),
 
114
             [dirblocks[2].as_tuple()]
 
115
            ),
 
116
            (dirblocks[3].as_dir_tuple(),
 
117
             []
50
118
            ),
51
119
            ]
 
120
        if prefix:
 
121
            expected_dirblocks = [e for e in expected_dirblocks
 
122
                if len(e) > 0 and len(e[0]) > 0 and e[0][0] == prefix]
52
123
        return tree, expected_dirblocks
53
 
    
 
124
 
 
125
    def _test_walkdir(self, file_status, prefix=""):
 
126
        result = []
 
127
        tree, expected_dirblocks = self.get_tree(file_status, prefix)
 
128
        tree.lock_read()
 
129
        for dirinfo, dirblock in tree.walkdirs(prefix):
 
130
            result.append((dirinfo, list(dirblock)))
 
131
        tree.unlock()
 
132
 
 
133
        # check each return value for debugging ease.
 
134
        for pos, item in enumerate(expected_dirblocks):
 
135
            result_pos = []
 
136
            if len(result) > pos:
 
137
                result_pos = result[pos]
 
138
            self.assertEqual(item, result_pos)
 
139
        self.assertEqual(expected_dirblocks, result)
 
140
 
54
141
    def test_walkdir_unknowns(self):
55
142
        """unknown files and directories should be reported by walkdirs."""
56
 
        # test that its iterable by iterating:
57
 
        result = []
58
 
        tree, expected_dirblocks = self.get_tree_with_unknowns()
59
 
        tree.lock_read()
60
 
        for dirinfo, dirblock in tree.walkdirs():
61
 
            result.append((dirinfo, list(dirblock)))
62
 
        tree.unlock()
63
 
        # check each return value for debugging ease.
64
 
        for pos, item in enumerate(expected_dirblocks):
65
 
            self.assertEqual(item, result[pos])
66
 
        self.assertEqual(len(expected_dirblocks), len(result))
 
143
        self._test_walkdir(self.unknown)
67
144
 
68
145
    def test_walkdir_from_unknown_dir(self):
69
146
        """Doing a walkdir when the requested prefix is unknown but on disk."""
70
 
        result = []
71
 
        tree, expected_dirblocks = self.get_tree_with_unknowns()
72
 
        tree.lock_read()
73
 
        for dirinfo, dirblock in tree.walkdirs('unknown dir'):
74
 
            result.append((dirinfo, list(dirblock)))
75
 
        tree.unlock()
76
 
        # check each return value for debugging ease.
77
 
        for pos, item in enumerate(expected_dirblocks[1:]):
78
 
            self.assertEqual(item, result[pos])
79
 
        self.assertEqual(len(expected_dirblocks) - 1, len(result))
 
147
        self._test_walkdir(self.unknown, 'unknown dir')
80
148
 
81
 
    def get_tree_with_missings(self):
82
 
        tree = self.make_branch_and_tree('.')
83
 
        paths = [
84
 
            'missing file',
85
 
            'missing dir/',
86
 
            'missing dir/a file',
87
 
            ]
88
 
        ids = [
89
 
            'a file',
90
 
            'a dir',
91
 
            'a dir-a file',
92
 
            ]
93
 
        self.build_tree(paths)
94
 
        tree.add(paths, ids)
95
 
        # now make the files be missing
96
 
        tree.bzrdir.root_transport.delete_tree('missing dir')
97
 
        tree.bzrdir.root_transport.delete('missing file')
98
 
        expected_dirblocks = [
99
 
            (('', tree.path2id('')),
100
 
             [
101
 
              ('missing dir', 'missing dir', 'unknown', None, 'a dir', 'directory'),
102
 
              ('missing file', 'missing file', 'unknown', None, 'a file', 'file'),
103
 
             ]
104
 
            ),
105
 
            (('missing dir', 'a dir'),
106
 
             [('missing dir/a file', 'a file', 'unknown', None, 'a dir-a file', 'file'),
107
 
             ]
108
 
            ),
109
 
            ]
110
 
        return tree, expected_dirblocks
111
 
    
112
149
    def test_walkdir_missings(self):
113
150
        """missing files and directories should be reported by walkdirs."""
114
 
        # test that its iterable by iterating:
115
 
        result = []
116
 
        tree, expected_dirblocks = self.get_tree_with_missings()
117
 
        tree.lock_read()
118
 
        for dirinfo, dirblock in tree.walkdirs():
119
 
            result.append((dirinfo, list(dirblock)))
120
 
        tree.unlock()
121
 
        # check each return value for debugging ease.
122
 
        for pos, item in enumerate(expected_dirblocks):
123
 
            self.assertEqual(item, result[pos])
124
 
        self.assertEqual(len(expected_dirblocks), len(result))
 
151
        self._test_walkdir(self.missing)
 
152
 
 
153
    def test_walkdir_from_dir(self):
 
154
        """Doing a walkdir when the requested prefix is known and on disk."""
 
155
        self._test_walkdir(self.added, 'added dir')
 
156
 
 
157
    def test_walkdir_from_empty_dir(self):
 
158
        """Doing a walkdir when the requested prefix is empty dir."""
 
159
        self._test_walkdir(self.added, 'added empty dir')
125
160
 
126
161
    def test_walkdir_from_missing_dir(self):
127
162
        """Doing a walkdir when the requested prefix is missing but on disk."""
128
 
        result = []
129
 
        tree, expected_dirblocks = self.get_tree_with_missings()
130
 
        tree.lock_read()
131
 
        for dirinfo, dirblock in tree.walkdirs('missing dir'):
132
 
            result.append((dirinfo, list(dirblock)))
133
 
        tree.unlock()
134
 
        # check each return value for debugging ease.
135
 
        for pos, item in enumerate(expected_dirblocks[1:]):
136
 
            self.assertEqual(item, result[pos])
137
 
        self.assertEqual(len(expected_dirblocks[1:]), len(result))
 
163
        self._test_walkdir(self.missing, 'missing dir')
138
164
 
139
165
    def test_walkdirs_type_changes(self):
140
166
        """Walkdir shows the actual kinds on disk and the recorded kinds."""