Added resource_filename function and tests.

This commit is contained in:
Antoni Boucher 2015-08-08 13:47:47 -04:00
parent ec5049f801
commit b8809f879d
3 changed files with 39 additions and 7 deletions

View File

@ -20,10 +20,9 @@
"""The directory browser page."""
import os
import pkg_resources
import qutebrowser
from qutebrowser.utils import jinja
from qutebrowser.utils.utils import resource_filename
def dirbrowser(urlstring):
@ -40,10 +39,8 @@ def dirbrowser(urlstring):
# pylint: disable=no-member
# https://bitbucket.org/logilab/pylint/issue/490/
folder = pkg_resources.resource_filename(qutebrowser.__name__,
'img/folder.png')
file = pkg_resources.resource_filename(qutebrowser.__name__,
'img/file.png')
folder = resource_filename('img/folder.png')
file = resource_filename('img/file.png')
def is_file(file):
return os.path.isfile(os.path.join(urlstring, file))
@ -59,7 +56,8 @@ def dirbrowser(urlstring):
files = sorted([{'name': file, 'absname': os.path.join(urlstring, file)}
for file in all_files if is_file(file)],
key=lambda v: v['name'].lower())
directories = sorted([{'name': file, 'absname': os.path.join(urlstring, file)}
directories = sorted([{'name': file, 'absname': os.path.join(urlstring,
file)}
for file in all_files if is_dir(file)],
key=lambda v: v['name'].lower())
html = template.render(title=title, url=urlstring, icon='', parent=parent,

View File

@ -89,6 +89,20 @@ def read_file(filename, binary=False):
return data
def resource_filename(filename):
"""Get the absolute filename of a file contained with qutebrowser.
Args:
filename: The filename.
Return:
The absolute filename.
"""
if hasattr(sys, 'frozen'):
return os.path.join(os.path.dirname(sys.executable), filename)
return pkg_resources.resource_filename(qutebrowser.__name__, filename)
def actute_warning():
"""Display a warning about the dead_actute issue if needed."""
# WORKAROUND (remove this when we bump the requirements to 5.3.0)

View File

@ -117,6 +117,26 @@ class TestReadFile:
assert content.splitlines()[0] == b"Hello World!"
class TestResourceFilename:
"""Test resource_filename."""
@pytest.fixture(autouse=True, params=[True, False])
def freezer(self, request, monkeypatch):
if request.param and not getattr(sys, 'frozen', False):
monkeypatch.setattr(sys, 'frozen', True, raising=False)
monkeypatch.setattr('sys.executable', qutebrowser.__file__)
elif not request.param and getattr(sys, 'frozen', False):
# Want to test unfrozen tests, but we are frozen
pytest.skip("Can't run with sys.frozen = True!")
def test_readfile(self):
"""Read a test file."""
filename = utils.resource_filename(os.path.join('utils', 'testfile'))
expected = os.path.abspath('./qutebrowser/utils/testfile')
assert expected == filename
class Patcher:
"""Helper for TestActuteWarning.