Allow mode='rt' on open_resource() helper

This commit is contained in:
icreatedanaccount 2019-05-06 10:56:35 -04:00 committed by David Lord
parent 05a4e15ee4
commit ad709be46e
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 27 additions and 0 deletions

View file

@ -1055,6 +1055,8 @@ class _PackageBoundObject(object):
subfolders use forward slashes as separator.
:param mode: resource file opening mode, default is 'rb'.
"""
if mode == 'rt':
mode = 'r'
if mode not in ("r", "rb"):
raise ValueError("Resources can only be opened for reading")
return open(os.path.join(self.root_path, resource), mode)

View file

@ -1014,6 +1014,7 @@ class TestSafeJoin(object):
print(flask.safe_join(*args))
class TestHelpers(object):
@pytest.mark.parametrize(
"debug, expected_flag, expected_default_flag",
@ -1060,3 +1061,27 @@ class TestHelpers(object):
assert rv.status_code == 200
assert rv.data == b"Hello"
assert rv.mimetype == "text/html"
@pytest.mark.parametrize('mode', [
'r',
'rb',
'rt'
])
def test_open_resource(self, mode):
app = flask.Flask(__name__)
with app.open_resource('static/index.html', mode) as f:
assert '<h1>Hello World!</h1>' in str(f.read())
@pytest.mark.parametrize('mode', [
'w',
'x',
'a',
'b',
't',
'+'
])
def test_open_resource_exceptions(self, mode):
app = flask.Flask(__name__)
with pytest.raises(ValueError):
app.open_resource('static/index.html', mode)