From fa1ac8d93cdf5061a5df91b5f8ffe63e078018a5 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 3 Mar 2018 13:00:27 +1300 Subject: [PATCH] Move download_stub to helpers/fixtures I am adding support for downloading dependant assets in browser/greasemonkey and want to mock the download manager for testing. --- tests/helpers/fixtures.py | 9 +++++ tests/helpers/stubs.py | 48 +++++++++++++++++++++++- tests/unit/browser/test_adblock.py | 60 +----------------------------- 3 files changed, 58 insertions(+), 59 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 193a40a8a..7c9fc93ae 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -523,3 +523,12 @@ class ModelValidator: @pytest.fixture def model_validator(qtmodeltester): return ModelValidator(qtmodeltester) + + +@pytest.fixture +def download_stub(win_registry, tmpdir, stubs): + """Register a FakeDownloadManager.""" + stub = stubs.FakeDownloadManager(tmpdir) + objreg.register('qtnetwork-download-manager', stub) + yield + objreg.delete('qtnetwork-download-manager') diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 3957a670a..0167926bd 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -22,6 +22,8 @@ """Fake objects/stubs.""" from unittest import mock +import contextlib +import shutil import attr from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject, QUrl @@ -29,7 +31,7 @@ from PyQt5.QtNetwork import (QNetworkRequest, QAbstractNetworkCache, QNetworkCacheMetaData) from PyQt5.QtWidgets import QCommonStyle, QLineEdit, QWidget, QTabBar -from qutebrowser.browser import browsertab +from qutebrowser.browser import browsertab, downloads from qutebrowser.utils import usertypes from qutebrowser.mainwindow import mainwindow @@ -558,3 +560,47 @@ class HTTPPostStub(QObject): def post(self, url, data=None): self.url = url self.data = data + + +class FakeDownloadItem(QObject): + + """Mock browser.downloads.DownloadItem.""" + + finished = pyqtSignal() + + def __init__(self, fileobj, name, parent=None): + super().__init__(parent) + self.fileobj = fileobj + self.name = name + self.successful = True + + +class FakeDownloadManager: + + """Mock browser.downloads.DownloadManager.""" + + def __init__(self, tmpdir): + self._tmpdir = tmpdir + + @contextlib.contextmanager + def _open_fileobj(self, target): + """Ensure a DownloadTarget's fileobj attribute is available.""" + if isinstance(target, downloads.FileDownloadTarget): + target.fileobj = open(target.filename, 'wb') + try: + yield target.fileobj + finally: + target.fileobj.close() + else: + yield target.fileobj + + def get(self, url, target, **kwargs): + """Return a FakeDownloadItem instance with a fileobj. + + The content is copied from the file the given url links to. + """ + with self._open_fileobj(target): + download_item = FakeDownloadItem(target.fileobj, name=url.path()) + with (self._tmpdir / url.path()).open('rb') as fake_url_file: + shutil.copyfileobj(fake_url_file, download_item.fileobj) + return download_item diff --git a/tests/unit/browser/test_adblock.py b/tests/unit/browser/test_adblock.py index a6bbcd8ce..e3b0e4576 100644 --- a/tests/unit/browser/test_adblock.py +++ b/tests/unit/browser/test_adblock.py @@ -21,16 +21,13 @@ import os import os.path import zipfile -import shutil import logging -import contextlib import pytest -from PyQt5.QtCore import pyqtSignal, QUrl, QObject +from PyQt5.QtCore import QUrl -from qutebrowser.browser import adblock, downloads -from qutebrowser.utils import objreg +from qutebrowser.browser import adblock pytestmark = pytest.mark.usefixtures('qapp', 'config_tmpdir') @@ -70,59 +67,6 @@ def basedir(fake_args): fake_args.basedir = None -class FakeDownloadItem(QObject): - - """Mock browser.downloads.DownloadItem.""" - - finished = pyqtSignal() - - def __init__(self, fileobj, name, parent=None): - super().__init__(parent) - self.fileobj = fileobj - self.name = name - self.successful = True - - -class FakeDownloadManager: - - """Mock browser.downloads.DownloadManager.""" - - def __init__(self, tmpdir): - self._tmpdir = tmpdir - - @contextlib.contextmanager - def _open_fileobj(self, target): - """Ensure a DownloadTarget's fileobj attribute is available.""" - if isinstance(target, downloads.FileDownloadTarget): - target.fileobj = open(target.filename, 'wb') - try: - yield target.fileobj - finally: - target.fileobj.close() - else: - yield target.fileobj - - def get(self, url, target, **kwargs): - """Return a FakeDownloadItem instance with a fileobj. - - The content is copied from the file the given url links to. - """ - with self._open_fileobj(target): - download_item = FakeDownloadItem(target.fileobj, name=url.path()) - with (self._tmpdir / url.path()).open('rb') as fake_url_file: - shutil.copyfileobj(fake_url_file, download_item.fileobj) - return download_item - - -@pytest.fixture -def download_stub(win_registry, tmpdir): - """Register a FakeDownloadManager.""" - stub = FakeDownloadManager(tmpdir) - objreg.register('qtnetwork-download-manager', stub) - yield - objreg.delete('qtnetwork-download-manager') - - def create_zipfile(directory, files, zipname='test'): """Return a path to a newly created zip file.