diff options
Diffstat (limited to 'test/utils/test_filewalker.py')
-rw-r--r-- | test/utils/test_filewalker.py | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/test/utils/test_filewalker.py b/test/utils/test_filewalker.py new file mode 100644 index 0000000..4aaba65 --- /dev/null +++ b/test/utils/test_filewalker.py @@ -0,0 +1,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 ## |