def test_scanner_find_directory_contents_on_dirfound(): found = set() def get_found_files(sender, signal): nonlocal found found.add(signal) FileFound.connect(get_found_files) SymlinkFound.connect(get_found_files) with tempfile.TemporaryDirectory() as path: filename = os.path.join(path, 'foo.txt') open(filename, 'w').close() # Touch foo.txt linkname = os.path.join(path, 'foo.lnk') os.symlink(filename, linkname) config = {} scanner = Scanner(config) scanner.prepare() scanner.start() DirFound(path=path).emit() ExitRequested().emit() scanner.join() assert found == set( {FileFound(path=filename), SymlinkFound(path=linkname)})
def test_dont_emit_exitrequested_if_there_are_pending_processing(): signals = list() def get_exit_requested(sender, signal): nonlocal signals signals.append(signal) ExitRequested.connect(get_exit_requested) stopper = Stopper(None) stopper.prepare() stopper.start() UserScanRequested(path=None).emit() DirFound(path=None).emit() FileFound(path=None).emit() SpecialFileFound(path=None).emit() SymlinkFound(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() # We lack one PathProcessingFinished sleep(1) assert not signals ExitRequested().emit() stopper.join()
def test_emit_exitrequested_if_all_files_were_processed(): signals = set() def get_exit_requested(sender, signal): nonlocal signals signals.add(signal) ExitRequested.connect(get_exit_requested) stopper = Stopper(None) stopper.prepare() stopper.start() UserScanRequested(path=None).emit() DirFound(path=None).emit() FileFound(path=None).emit() SpecialFileFound(path=None).emit() SymlinkFound(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() PathProcessingFinished(path=None).emit() stopper.join() assert signals == {ExitRequested()}
def test_scandir_return_a_list_of_signals_if_dir_contains_dotfile(): with tempfile.TemporaryDirectory() as path: with tempfile.NamedTemporaryFile(prefix='.', dir=path) \ as file, tempfile.TemporaryDirectory(dir=path) as subdir: assert set(scandir(path)) == { FileFound(path=file.name), DirFound(path=subdir) }
def classify_path(path): """ Generate a signal based on the nature of the given path. """ if os.path.islink(path): return SymlinkFound(path=path) elif os.path.isfile(path): return FileFound(path=path) elif os.path.isdir(path): return DirFound(path=path) else: return SpecialFileFound(path=path)
def scandir(path): """ Generate a signal for every file and directory found in path. """ with os.scandir(path) as dircontent: for entry in dircontent: if entry.is_symlink(): yield SymlinkFound(path=entry.path) elif entry.is_file(): yield FileFound(path=entry.path) elif entry.is_dir(): yield DirFound(path=entry.path) else: yield SpecialFileFound(path=entry.path)
def test_scandir_returns_a_dirfound_if_dir_contains_dir(): with tempfile.TemporaryDirectory() as path1: with tempfile.TemporaryDirectory(dir=path1) as path2: assert list(scandir(path1)) == [DirFound(path=path2)]
def test_classify_path_returns_a_dirfound_if_dir(): with tempfile.TemporaryDirectory() as path: assert classify_path(path) == DirFound(path=path)