def test_recursive_scandir_yields_startpath_if_file_or_subdir(): with tempdir('dir/file') as tmp_dir: assert list(files.recursive_scandir('file', root=tmp_dir / 'dir')) == ['file'] assert list(files.recursive_scandir( 'dir', root=tmp_dir)) == ['dir', 'dir/file'] assert list(files.recursive_scandir(tmp_dir / 'dir')) == ['file']
def test_recursive_scandir(): with tempdir('file', 'dir/', 'dir/subfile') as tmp_path: found_paths = {str(p): p for p in files.recursive_scandir(tmp_path)} assert found_paths.keys() == {'file', 'dir', 'dir/subfile'} assert found_paths['dir'].is_dir is True assert found_paths['file'].is_dir is False assert found_paths['dir/subfile'].is_dir is False
def test_canonical_path(): with tempdir('file', links={'link': 'file'}) as testdir: assert files.canonical_path(testdir / 'file') == str(testdir / 'file') assert files.canonical_path(testdir / 'file', root=testdir) == str(testdir / 'file') assert files.canonical_path('file', root=testdir) == str(testdir / 'file') assert files.canonical_path('link', root=testdir) == str(testdir / 'file') assert files.canonical_path('.', root='.') == os.getcwd() assert files.canonical_path(b'.') == os.getcwdb()
def test_recursive_scandir_filters(): def no_fizz(path): return int(path.name) % 2 != 0 def no_buzz(path): return int(path.name) % 3 != 0 with tempdir(*[str(i) for i in range(10)]) as tmp_path: objs = { str(o): o for o in files.recursive_scandir(tmp_path, filters=(no_fizz, no_buzz)) } assert objs.keys() == {'1', '5', '7'}
def test_recursive_scandir_symlinks(): links = { 'root/link': 'other/file', 'root/dirlink': 'other/', } with tempdir(links=links) as tmp_path: root_path = tmp_path / 'root' objs = { str(o): o for o in files.recursive_scandir('.', root=root_path) } assert objs.keys() == {'link', 'dirlink/file', 'dirlink'} assert objs['dirlink'].is_dir assert not objs['dirlink/file'].is_dir assert not objs['link'].is_dir assert os.path.samefile(root_path / objs['dirlink/file'], root_path / objs['link'])
def test_circular_symlink_filter(): links = { 'root/safe_always': 'root/links_to_files_are_safe', 'root/safe_once': 'other/safe_once/', 'root/above_known_root': 'other/', 'root/below_known_root': 'other/safe_once/child/', 'root/back_to_root': 'root/', 'root/safe_despite_common_suffix': 'other/safe_once_2/' } with tempdir(links=links) as testpath: filter_allows = files.circular_symlink_filter(root=testpath / 'root') assert filter_allows('not_a_link') assert filter_allows('safe_always') and filter_allows('safe_always') assert filter_allows('safe_once') and not filter_allows( 'safe_once') # no idempotence! assert not filter_allows('above_known_root') assert not filter_allows('below_known_root') assert not filter_allows('back_to_root') assert filter_allows('safe_despite_common_suffix')
def test_recursive_scandir_maxdepth(): with tempdir('dir/', 'dir/subdir/subfile') as tmp_path: found_paths = set(files.recursive_scandir(tmp_path, max_depth=1)) assert found_paths == {'dir', 'dir/subdir'}
def wrapper(*args, **kwargs): with helpers.tempdir() as tempdir: with mock.patch.object(sqlite, 'DB_BASEDIR', tempdir): func(*args, **kwargs)