Add unit tests for suffix generation for downloads
The pushpin is my standard high unicode value codepoint. Some things handle umlauts etc fine but choke on higher values. The behavior seems fine enough I guess. Some people might prefer " (2)" instead of "_2". Also maybe we should start at 1 instead of 2?
This commit is contained in:
parent
f9186b3b55
commit
aa4c8e4126
|
|
@ -500,6 +500,9 @@ class AbstractDownloadItem(QObject):
|
|||
"""
|
||||
assert self._filename is not None
|
||||
path, file = os.path.split(self._filename)
|
||||
# Pull out filename extension which could be a two part one like
|
||||
# `tar.gz`. Use a more restrictive character set for the first part of
|
||||
# a two part extension to avoid matching numbers.
|
||||
match = re.fullmatch(r'(.+?)((\.[a-z]+)?\.[^.]+)', file)
|
||||
if match:
|
||||
base, suffix = match[1], match[2]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from qutebrowser.browser import downloads, qtnetworkdownloads
|
||||
|
|
@ -113,6 +115,33 @@ def test_sanitized_filenames(raw, expected,
|
|||
assert item._filename.endswith(expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('filename, expected', [
|
||||
("noext", "noext_2"),
|
||||
("simple.gif", "simple_2.gif"),
|
||||
("twoparts.tar.gz", "twoparts_2.tar.gz"),
|
||||
("many.dots.in.the.name", "many.dots.in_2.the.name"),
|
||||
("a. space.gif", "a. space_2.gif"),
|
||||
("non-ascii-📍.gif", "non-ascii-📍_2.gif"),
|
||||
("non-ascii.📍", "non-ascii_2.📍"),
|
||||
("non-ascii.📍.gif", "non-ascii.📍_2.gif"),
|
||||
("numbers_22.10.05.jpeg", "numbers_22.10.05_2.jpeg"),
|
||||
("Sentance..gif", "Sentance._2.gif"),
|
||||
])
|
||||
def test_generated_filename_suffix(
|
||||
filename, expected, config_stub, download_tmpdir, monkeypatch
|
||||
):
|
||||
manager = downloads.AbstractDownloadManager()
|
||||
item = downloads.AbstractDownloadItem(manager=manager)
|
||||
|
||||
# Abstract methods
|
||||
item._ensure_can_set_filename = lambda *args: True
|
||||
item._after_set_filename = lambda *args: True
|
||||
|
||||
item._set_filename(filename)
|
||||
item._find_next_filename()
|
||||
assert os.path.basename(item._filename) == expected
|
||||
|
||||
|
||||
class TestConflictingDownloads:
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
Loading…
Reference in New Issue