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_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)})
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_classify_path_returns_a_symlinkfound_if_symlink_to_regular_file():
    with tempfile.TemporaryDirectory() as path:
        filepath = os.path.join(path, 'regular_file')
        symlink_path = filepath + '_symlink'

        open(filepath, 'w').close()  # Equivalent to touch command
        os.symlink(filepath, symlink_path)

        assert classify_path(symlink_path) == SymlinkFound(path=symlink_path)
def test_scandir_return_a_symlinkfound_if_dir_contains_symlink_to_regular_file(
):
    with tempfile.TemporaryDirectory() as path:
        filepath = os.path.join(path, 'regular_file')
        symlink_path = filepath + '_symlink'

        open(filepath, 'w').close()  # Equivalent to touch command
        os.symlink(filepath, symlink_path)

        assert SymlinkFound(path=symlink_path) in set(scandir(path))
Exemple #7
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)
Exemple #8
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)