From 747acfe7fad3a0fa70d88b6fa8d2891fd0eecb15 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Tue, 26 Jun 2018 16:24:37 +1200 Subject: [PATCH] Makes sanitize_filenames platform dependant. Hopefully having the posix bad_chars list so minimal won't ruin anyones day. --- qutebrowser/utils/utils.py | 10 ++++++++-- tests/unit/browser/webkit/test_downloads.py | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 8beb21279..f3ab6b5ee 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -505,10 +505,16 @@ def sanitize_filename(name, replacement='_'): encoding = sys.getfilesystemencoding() name = force_encoding(name, encoding) - # Bad characters taken from Windows, there are even fewer on Linux # See also # https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words - bad_chars = '\\/:*?"<>|' + if is_windows: + bad_chars = '\\/:*?"<>|' + elif is_mac: + # Colons can be confusing in finder https://superuser.com/a/326627 + bad_chars = '/:' + else: + bad_chars = '/' + for bad_char in bad_chars: name = name.replace(bad_char, replacement) return name diff --git a/tests/unit/browser/webkit/test_downloads.py b/tests/unit/browser/webkit/test_downloads.py index 68573002c..8dc80f92c 100644 --- a/tests/unit/browser/webkit/test_downloads.py +++ b/tests/unit/browser/webkit/test_downloads.py @@ -22,6 +22,14 @@ import pytest from qutebrowser.browser import downloads, qtnetworkdownloads +@pytest.fixture(autouse=True) +def is_platform(monkeypatch, platform="windows"): + for p in ["mac", "linux", "posix", "windows"]: + monkeypatch.setattr( + 'qutebrowser.utils.utils.is_{}'.format(p), + p == platform) + + def test_download_model(qapp, qtmodeltester, config_stub, cookiejar_and_cache, fake_args): """Simple check for download model internals.""" @@ -98,11 +106,14 @@ class TestDownloadTarget: assert isinstance(obj, downloads._DownloadTarget) -@pytest.mark.parametrize('raw, expected', [ - ('http://foo/bar', 'bar'), - ('A *|<>\\: bear!', 'A ______ bear!') +@pytest.mark.parametrize('raw, platform, expected', [ + ('http://foo/bar', 'windows', 'bar'), + ('A *|<>\\: bear!', 'windows', 'A ______ bear!'), + ('A *|<>\\: bear!', 'posix', 'A *|<>\\: bear!') ]) -def test_sanitized_filenames(raw, expected, config_stub, download_tmpdir): +def test_sanitized_filenames(raw, platform, expected, config_stub, + download_tmpdir, monkeypatch): + is_platform(monkeypatch, platform) manager = downloads.AbstractDownloadManager() target = downloads.FileDownloadTarget(str(download_tmpdir)) item = downloads.AbstractDownloadItem()