Exemple #1
0
def test_add_linking_unknown(mocker: MockerFixture):
    api = mocker.Mock(spec=TransmissionApi)
    api.add_torrent_with_files.return_value = CommandResult(error="unknown",
                                                            success=False)
    api.add_torrent.return_value = CommandResult(error="unknown",
                                                 success=False)
    add_service = AddService(api)

    fs = MockFilesystem({"test_path"})
    path = Path("/", "test_path")
    file = MetainfoFile(
        {
            "name": "meaningless",
            "info_hash": "meaningless and necessary",
            "info": {
                "length": 5
            },
        },
        path,
    )
    command = LinkingAddCommand(add_service, fs,
                                {TorrentData(file, Path("/some/place"))})

    output: LinkingAddOutput = command.run()

    assert fs.exists(path)
    assert output.failed_torrents == {file: "unknown"}
Exemple #2
0
def test_linking_add_dry_run_display(mocker: MockerFixture, capsys):
    api = mocker.Mock(spec=TransmissionApi)
    api.add_torrent_with_files.return_value = CommandResult(success=True)
    api.add_torrent.return_value = CommandResult(success=True)
    add_service = AddService(api)

    fs = MockFilesystem({"test_path"})
    path = Path("/", "test_path")
    file = MetainfoFile(
        {
            "name": "meaningless",
            "info_hash": "meaningless and necessary",
            "info": {
                "length": 5
            },
        },
        path,
    )
    torrent_data = {TorrentData(file, Path("/some/place")), TorrentData(file)}
    command = LinkingAddCommand(add_service, fs, torrent_data)

    output: LinkingAddOutput = command.dry_run()
    output.dry_run_display()

    result = capsys.readouterr().out

    assert (result == "\n".join([
        "Would add 1 torrents with data:",
        "meaningless at /some/place",
        "Would add 1 torrents without data:",
        "meaningless",
    ]) + "\n")
Exemple #3
0
def test_linking_add_run_display_failed(mocker: MockerFixture, capsys):
    api = mocker.Mock(spec=TransmissionApi)
    api.add_torrent_with_files.return_value = CommandResult(error="unknown",
                                                            success=False)
    api.add_torrent.return_value = CommandResult(error="unknown",
                                                 success=False)
    add_service = AddService(api)

    fs = MockFilesystem({"test_path"})
    locator: TorrentDataLocator = DefaultTorrentDataLocator(fs)
    find_service = FindService(locator)
    path = Path("/", "test_path")
    file = MetainfoFile(
        {
            "name": "meaningless",
            "info_hash": "meaningless and necessary",
            "info": {
                "length": 5
            },
        },
        path,
    )
    command = LinkingAddCommand(add_service, fs,
                                {TorrentData(file, Path("/some/place"))})

    output: LinkingAddOutput = command.run()
    output.display()

    result = capsys.readouterr().out

    assert result == "\n".join(["1 failed:", "meaningless because: unknown"
                                ]) + "\n"
Exemple #4
0
def test_linking_add_run_display_duplicated(mocker: MockerFixture, capsys):
    api = mocker.Mock(spec=TransmissionApi)
    api.add_torrent_with_files.return_value = CommandResult(success=False,
                                                            error="duplicate")
    api.add_torrent.return_value = CommandResult(success=False,
                                                 error="duplicate")
    add_service = AddService(api)

    fs = MockFilesystem({"test_path"})
    path = Path("/", "test_path")
    file = MetainfoFile(
        {
            "name": "meaningless",
            "info_hash": "meaningless and necessary",
            "info": {
                "length": 5
            },
        },
        path,
    )
    command = LinkingAddCommand(add_service, fs,
                                {TorrentData(file, Path("/some/place"))})

    output: LinkingAddOutput = command.run()
    output.display()

    result = capsys.readouterr().out

    assert result == "\n".join(["There are 1 duplicates:", "meaningless"
                                ]) + "\n"
Exemple #5
0
def test_add_linking_success(mocker: MockerFixture):
    api = mocker.Mock(spec=TransmissionApi)
    api.add_torrent_with_files.return_value = CommandResult(success=True)
    api.add_torrent.return_value = CommandResult(success=True)
    add_service = AddService(api)

    fs = MockFilesystem({"test_path"})
    locator: TorrentDataLocator = DefaultTorrentDataLocator(fs)
    find_service = FindService(locator)
    path = Path("/", "test_path")
    file = MetainfoFile(
        {
            "name": "meaningless",
            "info_hash": "meaningless and necessary",
            "info": {
                "length": 5
            },
        },
        path,
    )
    command = LinkingAddCommand(add_service, fs,
                                {TorrentData(file, Path("/some/place"))})

    output: LinkingAddOutput = command.run()

    assert not fs.exists(path)
    assert output.linked_torrents == {file: Path("/some/place")}
Exemple #6
0
def add_factory(argv: Sequence[str],
                dependencies: Mapping) -> CommandFactoryResult:
    client = dependencies["client"]
    reader: MetainfoIO = dependencies["metainfo_reader"]

    # parse arguments
    from clutchless.spec import add as add_command

    args = docopt(doc=add_command.__doc__, argv=argv)
    fs: Filesystem = dependencies["fs"]
    if not args["--delete"]:
        fs = DryRunFilesystem()

    metainfo_file_paths = collect_metainfo_paths(fs, args["<metainfo>"])
    metainfo_files = {reader.from_path(path) for path in metainfo_file_paths}

    data_directories = get_valid_directories(fs, args["-d"])
    file_locator = MultipleDirectoryFileLocator(data_directories, fs)
    data_reader = DefaultTorrentDataReader(fs)
    data_locator = CustomTorrentDataLocator(file_locator, data_reader)

    add_service = AddService(client)

    # action
    command: Command = AddCommand(add_service, fs, metainfo_files)
    if len(data_directories) > 0:
        find_service = FindService(data_locator)
        if not args["--force"]:
            add_service = LinkOnlyAddService(client)

        torrent_data: Iterable[TorrentData] = find_service.find(metainfo_files)
        response = input("Continue? [Y/N]:")
        if response.strip().lower() != "y":
            raise RuntimeError("User decided not to continue")

        command = LinkingAddCommand(add_service, fs, torrent_data)
    return command, args