diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 0f95c1c5d..ecfd456be 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -161,7 +161,7 @@ def init(args, crash_handler): QDesktopServices.setUrlHandler('https', open_desktopservices_url) QDesktopServices.setUrlHandler('qute', open_desktopservices_url) - _import_history() + objreg.get('history').import_txt() log.init.debug("Init done!") crash_handler.raise_crashdlg() @@ -479,27 +479,6 @@ def _init_modules(args, crash_handler): browsertab.init() -def _import_history(): - """Import a history text file into sqlite if it exists. - - In older versions of qutebrowser, history was stored in a text format. - This converts that file into the new sqlite format and removes it. - """ - path = os.path.join(standarddir.data(), 'history') - if not os.path.isfile(path): - return - - def action(): - with debug.log_time(log.init, 'Converting old history file to sqlite'): - objreg.get('web-history').read(path) - message.info('History import complete. Removing {}'.format(path)) - os.remove(path) - - # delay to give message time to appear before locking down for import - message.info('Converting {} to sqlite...'.format(path)) - QTimer.singleShot(100, action) - - class Quitter: """Utility class to quit/restart the QApplication. diff --git a/qutebrowser/browser/history.py b/qutebrowser/browser/history.py index c64674fdf..cb10c2bb2 100644 --- a/qutebrowser/browser/history.py +++ b/qutebrowser/browser/history.py @@ -22,10 +22,11 @@ import os import time -from PyQt5.QtCore import pyqtSlot, QUrl +from PyQt5.QtCore import pyqtSlot, QUrl, QTimer from qutebrowser.commands import cmdutils -from qutebrowser.utils import utils, objreg, log, qtutils, usertypes, message +from qutebrowser.utils import (utils, objreg, log, qtutils, usertypes, message, + debug, standarddir) from qutebrowser.misc import objects, sql @@ -217,7 +218,28 @@ class WebHistory(sql.SqlTable): return (url, title, float(atime), bool(redirect)) - def read(self, path): + def import_txt(self): + """Import a history text file into sqlite if it exists. + + In older versions of qutebrowser, history was stored in a text format. + This converts that file into the new sqlite format and removes it. + """ + path = os.path.join(standarddir.data(), 'history') + if not os.path.isfile(path): + return + + def action(): + with debug.log_time(log.init, 'Import old history file to sqlite'): + self._read(path) + message.info('History import complete. Removing {}' + .format(path)) + os.remove(path) + + # delay to give message time to appear before locking down for import + message.info('Converting {} to sqlite...'.format(path)) + QTimer.singleShot(100, action) + + def _read(self, path): """Import a text file into the sql database.""" with open(path, 'r', encoding='utf-8') as f: rows = [] diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index b852426d2..717ce4c18 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -385,6 +385,10 @@ class InstaTimer(QObject): def setInterval(self, interval): pass + @staticmethod + def singleShot(_interval, fun): + fun() + class FakeConfigType: diff --git a/tests/unit/browser/webkit/test_history.py b/tests/unit/browser/webkit/test_history.py index 16b86ca4a..5386dddd9 100644 --- a/tests/unit/browser/webkit/test_history.py +++ b/tests/unit/browser/webkit/test_history.py @@ -230,8 +230,9 @@ def test_init(backend, qapp, tmpdir, monkeypatch, cleanup_init): assert default_interface is None -def test_read(hist, tmpdir): - histfile = tmpdir / 'history' +def test_read(hist, data_tmpdir, monkeypatch, stubs): + monkeypatch.setattr(history, 'QTimer', stubs.InstaTimer) + histfile = data_tmpdir / 'history' # empty line is deliberate, to test skipping empty lines histfile.write('''12345 http://example.com/ title 12346 http://qutebrowser.org/ @@ -239,7 +240,7 @@ def test_read(hist, tmpdir): 68891-r http://example.com/path/other ''') - hist.read(str(histfile)) + hist.import_txt() assert list(hist) == [ ('http://example.com/', 'title', 12345, False), @@ -248,6 +249,8 @@ def test_read(hist, tmpdir): ('http://example.com/path/other', '', 68891, True) ] + assert not histfile.exists() + @pytest.mark.parametrize('line', [ 'xyz http://example.com/bad-timestamp', @@ -255,12 +258,15 @@ def test_read(hist, tmpdir): 'http://example.com/no-timestamp', '68891-r-r http://example.com/double-flag', ]) -def test_read_invalid(hist, tmpdir, line): - histfile = tmpdir / 'history' +def test_read_invalid(hist, data_tmpdir, line, monkeypatch, stubs): + monkeypatch.setattr(history, 'QTimer', stubs.InstaTimer) + histfile = data_tmpdir / 'history' histfile.write(line) with pytest.raises(Exception): - hist.read(str(histfile)) + hist.import_txt() + + assert histfile.exists() def test_debug_dump_history(hist, tmpdir):