Fix and test remaining PDF.js OSError issues
This commit is contained in:
parent
b5b4fda676
commit
616cd0a59c
@ -157,6 +157,9 @@ def get_pdfjs_res_and_path(path):
|
||||
content = utils.read_file(res_path, binary=True)
|
||||
except FileNotFoundError:
|
||||
raise PDFJSNotFound(path) from None
|
||||
except OSError as e:
|
||||
log.misc.warning("OSError while reading PDF.js file: {}".format(e))
|
||||
raise PDFJSNotFound(path) from None
|
||||
|
||||
return content, file_path
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
@ -132,7 +133,8 @@ class TestResources:
|
||||
os.path.expanduser('~/.local/share/qutebrowser/pdfjs/')]:
|
||||
read_system_mock.assert_any_call(path, ['web/test', 'test'])
|
||||
|
||||
def test_get_pdfjs_res_not_found(self, read_system_mock, read_file_mock):
|
||||
def test_get_pdfjs_res_not_found(self, read_system_mock, read_file_mock,
|
||||
caplog):
|
||||
read_system_mock.return_value = (None, None)
|
||||
read_file_mock.side_effect = FileNotFoundError
|
||||
|
||||
@ -140,6 +142,22 @@ class TestResources:
|
||||
match="Path 'web/test' not found"):
|
||||
pdfjs.get_pdfjs_res_and_path('web/test')
|
||||
|
||||
assert not caplog.records
|
||||
|
||||
def test_get_pdfjs_res_oserror(self, read_system_mock, read_file_mock,
|
||||
caplog):
|
||||
read_system_mock.return_value = (None, None)
|
||||
read_file_mock.side_effect = OSError("Message")
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
with pytest.raises(pdfjs.PDFJSNotFound,
|
||||
match="Path 'web/test' not found"):
|
||||
pdfjs.get_pdfjs_res_and_path('web/test')
|
||||
|
||||
assert len(caplog.records) == 1
|
||||
rec = caplog.records[0]
|
||||
assert rec.message == 'OSError while reading PDF.js file: Message'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path, expected', [
|
||||
('web/viewer.js', 'viewer.js'),
|
||||
@ -173,6 +191,23 @@ def test_read_from_system(names, expected_name, tmpdir):
|
||||
assert pdfjs._read_from_system(str(tmpdir), names) == expected
|
||||
|
||||
|
||||
def test_read_from_system_oserror(tmpdir, caplog):
|
||||
unreadable_file = tmpdir / 'unreadable'
|
||||
unreadable_file.ensure()
|
||||
unreadable_file.chmod(0)
|
||||
if os.access(str(unreadable_file), os.R_OK):
|
||||
# Docker container or similar
|
||||
pytest.skip("File was still readable")
|
||||
|
||||
expected = (None, None)
|
||||
with caplog.at_level(logging.WARNING):
|
||||
assert pdfjs._read_from_system(str(tmpdir), ['unreadable']) == expected
|
||||
|
||||
assert len(caplog.records) == 1
|
||||
rec = caplog.records[0]
|
||||
assert rec.message.startswith('OSError while reading PDF.js file:')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('available', [True, False])
|
||||
def test_is_available(available, mocker):
|
||||
mock = mocker.patch.object(pdfjs, 'get_pdfjs_res', autospec=True)
|
||||
|
Loading…
Reference in New Issue
Block a user