Пример #1
0
def test_scanner_find_regular_file_with_classify_path_on_userscanrequested():
    found = set()

    def get_found_files(sender, signal):
        nonlocal found
        found.add(signal)

    FileFound.connect(get_found_files)
    DirFound.connect(get_found_files)
    SpecialFileFound.connect(get_found_files)
    SymlinkFound.connect(get_found_files)

    with patch('engorgio.entities.scanner.classify_path') as classify_path:
        with tempfile.TemporaryDirectory() as path:
            filename = os.path.join(path, 'foo.txt')
            open(filename, 'w').close()  # Touch foo.txt

            classify_path.return_value = FileFound(path=filename)

            config = {}
            scanner = Scanner(config)
            scanner.prepare()
            scanner.start()
            UserScanRequested(path=filename).emit()
            ExitRequested().emit()
            scanner.join()

            classify_path.assert_called_once_with(filename)
            assert found == set({FileFound(path=filename)})
Пример #2
0
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)})
Пример #3
0
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()
Пример #4
0
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()}
Пример #5
0
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)
            }
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
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)]
Пример #9
0
def test_classify_path_returns_a_dirfound_if_dir():
    with tempfile.TemporaryDirectory() as path:
        assert classify_path(path) == DirFound(path=path)