Fail on history file parsing errors.

Instead of skipping bad history lines during the import to sql, fail hard. We
don't want to delete the user's old history file if we couldn't parse all of
the lines.
This commit is contained in:
Ryan Roden-Corrent 2017-04-08 16:20:55 -04:00
parent 6412c88277
commit 024386d189
2 changed files with 24 additions and 10 deletions

View File

@ -183,12 +183,16 @@ class WebHistory(sql.SqlTable):
"""Import a text file into the sql database.""" """Import a text file into the sql database."""
with open(path, 'r', encoding='utf-8') as f: with open(path, 'r', encoding='utf-8') as f:
rows = [] rows = []
for line in f: for (i, line) in enumerate(f):
line = line.strip()
if not line:
continue
try: try:
row = self._parse_entry(line.strip()) row = self._parse_entry(line.strip())
rows.append(row) rows.append(row)
except ValueError: except ValueError:
log.init.warning('Skipping history line {}'.format(line)) raise Exception('Failed to parse line #{} of {}: "{}"'
.format(i, path, line))
self.insert_batch(rows) self.insert_batch(rows)

View File

@ -196,20 +196,16 @@ def test_init(backend, qapp, tmpdir, monkeypatch, cleanup_init):
assert default_interface is None assert default_interface is None
def test_read(hist, tmpdir, caplog): def test_read(hist, tmpdir):
histfile = tmpdir / 'history' histfile = tmpdir / 'history'
# empty line is deliberate, to test skipping empty lines
histfile.write('''12345 http://example.com/ title histfile.write('''12345 http://example.com/ title
12346 http://qutebrowser.org/ 12346 http://qutebrowser.org/
67890 http://example.com/path 67890 http://example.com/path
xyz http://example.com/bad-timestamp 68891-r http://example.com/path/other ''')
12345
http://example.com/no-timestamp
68891-r http://example.com/path/other
68891-r-r http://example.com/double-flag''')
with caplog.at_level(logging.WARNING): hist.read(str(histfile))
hist.read(str(histfile))
assert list(hist) == [ assert list(hist) == [
('http://example.com/', 'title', 12345, False), ('http://example.com/', 'title', 12345, False),
@ -217,3 +213,17 @@ def test_read(hist, tmpdir, caplog):
('http://example.com/path', '', 67890, False), ('http://example.com/path', '', 67890, False),
('http://example.com/path/other', '', 68891, True) ('http://example.com/path/other', '', 68891, True)
] ]
@pytest.mark.parametrize('line', [
'xyz http://example.com/bad-timestamp',
'12345',
'http://example.com/no-timestamp',
'68891-r-r http://example.com/double-flag',
])
def test_read_invalid(hist, tmpdir, line):
histfile = tmpdir / 'history'
histfile.write(line)
with pytest.raises(Exception):
hist.read(str(histfile))