Fix static file check and associated tests
This commit is contained in:
parent
a2565adcba
commit
d4f26e8536
2 changed files with 14 additions and 18 deletions
|
|
@ -85,8 +85,7 @@ def check_static_dir() -> None:
|
||||||
"web app."
|
"web app."
|
||||||
)
|
)
|
||||||
expected = [
|
expected = [
|
||||||
os.path.isdir(os.path.join(STATIC_PATH, "js")),
|
os.path.isdir(os.path.join(STATIC_PATH, "assets")),
|
||||||
os.path.isdir(os.path.join(STATIC_PATH, "css")),
|
|
||||||
os.path.isfile(os.path.join(STATIC_PATH, "index.html")),
|
os.path.isfile(os.path.join(STATIC_PATH, "index.html")),
|
||||||
]
|
]
|
||||||
if not all(expected):
|
if not all(expected):
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,7 @@ def mock_static_dir():
|
||||||
It contains enough files to be recognised as a webapp.
|
It contains enough files to be recognised as a webapp.
|
||||||
"""
|
"""
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
os.makedirs(os.path.join(tmpdir, "js"))
|
os.makedirs(os.path.join(tmpdir, "assets"))
|
||||||
os.makedirs(os.path.join(tmpdir, "css"))
|
|
||||||
with open(os.path.join(tmpdir, "index.html"), "w", encoding="utf-8") as f_obj:
|
with open(os.path.join(tmpdir, "index.html"), "w", encoding="utf-8") as f_obj:
|
||||||
f_obj.write("<mock>mock</mock>")
|
f_obj.write("<mock>mock</mock>")
|
||||||
yield tmpdir
|
yield tmpdir
|
||||||
|
|
@ -95,21 +94,21 @@ def test_check_static_dir(mock_static_dir, mocker):
|
||||||
# First run shouldn't error as the mock dir has enough files.
|
# First run shouldn't error as the mock dir has enough files.
|
||||||
serve_static_files.check_static_dir()
|
serve_static_files.check_static_dir()
|
||||||
|
|
||||||
js_path = os.path.join(mock_static_dir, "js")
|
asset_path = os.path.join(mock_static_dir, "assets")
|
||||||
index_path = os.path.join(mock_static_dir, "index.html")
|
index_path = os.path.join(mock_static_dir, "index.html")
|
||||||
|
|
||||||
# Remove javascript dir and check error.
|
# Remove asset dir and check error.
|
||||||
shutil.rmtree(js_path)
|
shutil.rmtree(asset_path)
|
||||||
with pytest.raises(FileNotFoundError):
|
with pytest.raises(FileNotFoundError):
|
||||||
serve_static_files.check_static_dir()
|
serve_static_files.check_static_dir()
|
||||||
# replace javascript dir with file, it should still error.
|
# replace asset dir with file, it should still error.
|
||||||
shutil.copyfile(index_path, js_path)
|
shutil.copyfile(index_path, asset_path)
|
||||||
with pytest.raises(FileNotFoundError):
|
with pytest.raises(FileNotFoundError):
|
||||||
serve_static_files.check_static_dir()
|
serve_static_files.check_static_dir()
|
||||||
|
|
||||||
# Return it to a folder and check everything works again
|
# Return it to a folder and check everything works again
|
||||||
os.remove(js_path)
|
os.remove(asset_path)
|
||||||
os.makedirs(js_path)
|
os.makedirs(asset_path)
|
||||||
serve_static_files.check_static_dir()
|
serve_static_files.check_static_dir()
|
||||||
|
|
||||||
# But it errors again if index.html is removed.
|
# But it errors again if index.html is removed.
|
||||||
|
|
@ -152,13 +151,11 @@ def test_add_static_files(mock_static_dir, mocker):
|
||||||
assert second_wrapped().headers["Pragma"] == "no-cache"
|
assert second_wrapped().headers["Pragma"] == "no-cache"
|
||||||
|
|
||||||
# Also should have mounted both dirs
|
# Also should have mounted both dirs
|
||||||
assert mock_app.mount.call_count == 2
|
assert mock_app.mount.call_count == 1
|
||||||
mounted_paths = [call.args[0] for call in mock_app.mount.call_args_list]
|
mounted_path = mock_app.mount.call_args.args[0]
|
||||||
mounted_dirs = [call.args[1].directory for call in mock_app.mount.call_args_list]
|
mounted_dir = mock_app.mount.call_args.args[1].directory
|
||||||
assert "/css/" in mounted_paths
|
assert "/assets/" in mounted_path
|
||||||
assert "/js/" in mounted_paths
|
assert os.path.join(mock_static_dir, "assets") in mounted_dir
|
||||||
assert os.path.join(mock_static_dir, "css") in mounted_dirs
|
|
||||||
assert os.path.join(mock_static_dir, "js") in mounted_dirs
|
|
||||||
|
|
||||||
|
|
||||||
def test_add_static_files_with_scan_dir(mock_static_dir, mocker):
|
def test_add_static_files_with_scan_dir(mock_static_dir, mocker):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue