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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# standard imports
import os
import shutil
import tempfile
import unittest
# objects to test
from bsie.utils.filewalker import list_files
## code ##
def touch(path, text='<test content>'):
# create folders
os.makedirs(os.path.dirname(path), exist_ok=True)
# create file
with open(path, 'wt') as ofile:
ofile.write(text)
class TestListFiles(unittest.TestCase):
def setUp(self):
# set up directory structure
# <root>
# - zero*
# - foo
# - hello*
# - remote -> foobar/xyz
# - bar
# - world*
# - xyz*
# - bar
# - fst
# - abc*
# - zyx*
# - snd
# - cba*
# - xyz*
# - foobar
# - xyz*
# - hello*
# - world -> bar/snd
self.testdir = tempfile.mkdtemp(prefix='bsie-test-')
touch(os.path.join(self.testdir, 'zero'))
touch(os.path.join(self.testdir, 'foo', 'hello'))
touch(os.path.join(self.testdir, 'foo', 'bar', 'world'))
touch(os.path.join(self.testdir, 'foo', 'bar', 'xyz'))
touch(os.path.join(self.testdir, 'bar', 'fst', 'abc'))
touch(os.path.join(self.testdir, 'bar', 'fst', 'zyx'))
touch(os.path.join(self.testdir, 'bar', 'snd', 'cba'))
touch(os.path.join(self.testdir, 'bar', 'snd', 'xyz'))
touch(os.path.join(self.testdir, 'foobar', 'xyz'))
touch(os.path.join(self.testdir, 'foobar', 'hello'))
os.symlink(
os.path.join(self.testdir, 'bar', 'snd'),
os.path.join(self.testdir, 'foobar', 'world'))
os.symlink(
os.path.join(self.testdir, 'foobar', 'xyz'),
os.path.join(self.testdir, 'foo', 'remote'))
def tearDown(self):
# remove testing dirs
shutil.rmtree(self.testdir, ignore_errors=True)
def test_list_files(self):
# list_files lists all files beneath root
roots = [
os.path.join(self.testdir, 'foo'),
os.path.join(self.testdir, 'bar'),
os.path.join(self.testdir, 'foobar'),
os.path.join(self.testdir, 'zero'),
]
self.assertSetEqual(set(list_files(roots, recursive=True, follow_symlinks=True)), {
os.path.join(self.testdir, 'bar', 'fst', 'abc'),
os.path.join(self.testdir, 'bar', 'fst', 'zyx'),
os.path.join(self.testdir, 'bar', 'snd', 'cba'),
os.path.join(self.testdir, 'bar', 'snd', 'xyz'),
os.path.join(self.testdir, 'foo', 'bar', 'world'),
os.path.join(self.testdir, 'foo', 'bar', 'xyz'),
os.path.join(self.testdir, 'foo', 'hello'),
os.path.join(self.testdir, 'foo', 'remote'),
os.path.join(self.testdir, 'foobar', 'hello'),
os.path.join(self.testdir, 'foobar', 'world', 'cba'),
os.path.join(self.testdir, 'foobar', 'world', 'xyz'),
os.path.join(self.testdir, 'foobar', 'xyz'),
os.path.join(self.testdir, 'zero'),
})
# list_files lists respects root
self.assertSetEqual(set(list_files(
roots=[os.path.join(self.testdir, 'foo')], recursive=True, follow_symlinks=True)), {
os.path.join(self.testdir, 'foo', 'bar', 'world'),
os.path.join(self.testdir, 'foo', 'bar', 'xyz'),
os.path.join(self.testdir, 'foo', 'hello'),
os.path.join(self.testdir, 'foo', 'remote'),
})
# list_files lists respects recursive flag (lists only files in root!)
self.assertSetEqual(set(list_files(roots, recursive=False, follow_symlinks=True)), {
os.path.join(self.testdir, 'zero'),
})
# list_files lists respects symlink flag
# lists symlinked files but does not dive into symlinked folders
self.assertSetEqual(set(list_files(roots, recursive=True, follow_symlinks=False)), {
os.path.join(self.testdir, 'bar', 'fst', 'abc'),
os.path.join(self.testdir, 'bar', 'fst', 'zyx'),
os.path.join(self.testdir, 'bar', 'snd', 'cba'),
os.path.join(self.testdir, 'bar', 'snd', 'xyz'),
os.path.join(self.testdir, 'foo', 'bar', 'world'),
os.path.join(self.testdir, 'foo', 'bar', 'xyz'),
os.path.join(self.testdir, 'foo', 'hello'),
os.path.join(self.testdir, 'foo', 'remote'),
os.path.join(self.testdir, 'foobar', 'hello'),
os.path.join(self.testdir, 'foobar', 'xyz'),
os.path.join(self.testdir, 'zero'),
})
## main ##
if __name__ == '__main__':
unittest.main()
## EOF ##
|