forked from orbit-oss/flask
Allow mode='rt' on open_resource() helper
This commit is contained in:
parent
05a4e15ee4
commit
ad709be46e
2 changed files with 27 additions and 0 deletions
|
|
@ -1055,6 +1055,8 @@ class _PackageBoundObject(object):
|
||||||
subfolders use forward slashes as separator.
|
subfolders use forward slashes as separator.
|
||||||
:param mode: resource file opening mode, default is 'rb'.
|
:param mode: resource file opening mode, default is 'rb'.
|
||||||
"""
|
"""
|
||||||
|
if mode == 'rt':
|
||||||
|
mode = 'r'
|
||||||
if mode not in ("r", "rb"):
|
if mode not in ("r", "rb"):
|
||||||
raise ValueError("Resources can only be opened for reading")
|
raise ValueError("Resources can only be opened for reading")
|
||||||
return open(os.path.join(self.root_path, resource), mode)
|
return open(os.path.join(self.root_path, resource), mode)
|
||||||
|
|
|
||||||
|
|
@ -1014,6 +1014,7 @@ class TestSafeJoin(object):
|
||||||
print(flask.safe_join(*args))
|
print(flask.safe_join(*args))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestHelpers(object):
|
class TestHelpers(object):
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"debug, expected_flag, expected_default_flag",
|
"debug, expected_flag, expected_default_flag",
|
||||||
|
|
@ -1060,3 +1061,27 @@ class TestHelpers(object):
|
||||||
assert rv.status_code == 200
|
assert rv.status_code == 200
|
||||||
assert rv.data == b"Hello"
|
assert rv.data == b"Hello"
|
||||||
assert rv.mimetype == "text/html"
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue