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:
parent
6412c88277
commit
024386d189
@ -183,12 +183,16 @@ class WebHistory(sql.SqlTable):
|
||||
"""Import a text file into the sql database."""
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
rows = []
|
||||
for line in f:
|
||||
for (i, line) in enumerate(f):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
row = self._parse_entry(line.strip())
|
||||
rows.append(row)
|
||||
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)
|
||||
|
||||
|
||||
|
@ -196,20 +196,16 @@ def test_init(backend, qapp, tmpdir, monkeypatch, cleanup_init):
|
||||
assert default_interface is None
|
||||
|
||||
|
||||
def test_read(hist, tmpdir, caplog):
|
||||
def test_read(hist, tmpdir):
|
||||
histfile = tmpdir / 'history'
|
||||
# empty line is deliberate, to test skipping empty lines
|
||||
histfile.write('''12345 http://example.com/ title
|
||||
12346 http://qutebrowser.org/
|
||||
67890 http://example.com/path
|
||||
|
||||
xyz http://example.com/bad-timestamp
|
||||
12345
|
||||
http://example.com/no-timestamp
|
||||
68891-r http://example.com/path/other
|
||||
68891-r-r http://example.com/double-flag''')
|
||||
68891-r http://example.com/path/other ''')
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
hist.read(str(histfile))
|
||||
hist.read(str(histfile))
|
||||
|
||||
assert list(hist) == [
|
||||
('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/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))
|
||||
|
Loading…
Reference in New Issue
Block a user