1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# Copyright (C) 2013 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import unittest
import morphlib
def dummy_top_down_walker(root, treedict):
'''Something that imitates os.walk, but with a dict'''
subdirs = [k for k in treedict if isinstance(treedict[k], dict)]
files = [k for k in treedict if not isinstance(treedict[k], dict)]
yield root, subdirs, files
for subdir in subdirs:
subwalker = dummy_top_down_walker(os.path.join(root, subdir),
treedict[subdir])
for result in subwalker:
yield result
class InvertPathsTests(unittest.TestCase):
def setUp(self):
self.flat_tree = {"foo": None, "bar": None, "baz": None}
self.nested_tree = {
"foo": {
"bar": None,
"baz": None,
},
"fs": {
"btrfs": None,
"ext2": None,
"ext3": None,
"ext4": None,
"nfs": None,
},
}
def test_flat_lists_single_files(self):
walker = dummy_top_down_walker('.', self.flat_tree)
self.assertEqual(sorted(["./foo", "./bar", "./baz"]),
sorted(morphlib.fsutils.invert_paths(walker, [])))
def test_flat_excludes_listed_files(self):
walker = dummy_top_down_walker('.', self.flat_tree)
self.assertTrue(
"./bar" not in morphlib.fsutils.invert_paths(walker, ["./bar"]))
def test_nested_excludes_listed_files(self):
walker = dummy_top_down_walker('.', self.nested_tree)
excludes = ["./foo/bar", "./fs/nfs"]
found = frozenset(morphlib.fsutils.invert_paths(walker, excludes))
self.assertTrue(all(path not in found for path in excludes))
def test_nested_excludes_whole_dir(self):
walker = dummy_top_down_walker('.', self.nested_tree)
found = frozenset(morphlib.fsutils.invert_paths(walker, ["./foo"]))
unexpected = ("./foo", "./foo/bar", "./foo/baz")
self.assertTrue(all(path not in found for path in unexpected))
def test_lower_mount_precludes(self):
walker = dummy_top_down_walker('.', {
"tmp": {
"morph": {
"staging": {
"build": None,
"inst": None,
},
},
"ccache": {
"0": None
},
},
"bin": {
},
})
found = frozenset(morphlib.fsutils.invert_paths(
walker, [
"./tmp/morph/staging/build",
"./tmp/morph/staging/inst",
"./tmp",
]))
expected = ("./bin",)
self.assertEqual(sorted(found), sorted(expected))
|