Skip to content

Commit

Permalink
Preview will serve most recent static files
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Feb 3, 2024
1 parent d3c5560 commit 5b6b84f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion htmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ def preview(
assert app.static_folder is not None
combine_and_minify_js(Path(app.static_folder))

app.run(debug=True, host=host, port=port)
# reload when static files change
# werkzeug will re-run the terminal command
# Which causes the above combine_and_minify_*() to run
# and recreate combined.min.css/combined.min.js files
static_path = site.project_dir / 'static'
extra_files = static_path.iterdir()
app.run(debug=True, host=host, port=port, extra_files=extra_files)


@cli.command('templates', short_help='Create any missing templates')
Expand Down
63 changes: 63 additions & 0 deletions tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,66 @@ def test_preview_no_css_minify_no_js_minify(run_start: CliRunner) -> None:
for status, url in urls:
response = requests.get(url, timeout=0.01)
assert response.status_code == status


def test_preview_reload_css(run_start: CliRunner) -> None: # noqa: ARG001
url = 'http://localhost:9090/static/combined.min.css'
new_style = 'p {color: red;}'
expected = new_style.replace(' ', '').replace(';', '')
with run_preview():
response = requests.get(url, timeout=0.01)
before = response.text
assert expected not in before
css_path = Path('static') / 'style.css'
with css_path.open('a') as css_file:
css_file.write('\n' + new_style + '\n')

# Ensure new style is available after reload
read_timeout = False
after = before
while after == before:
try:
response = requests.get(url, timeout=0.1)
except requests.exceptions.ReadTimeout:
# happens during restart
read_timeout = True
else:
after = response.text

assert read_timeout
assert before != after
assert expected in after


def test_preview_reload_js(run_start: CliRunner) -> None: # noqa: ARG001
url = 'http://localhost:9090/static/combined.min.js'
new_js = 'document.getElementByTagName("body");'
expected = new_js
# Need to create before running preview since no .js files exist
js_path = Path('static') / 'script.js'
with js_path.open('w') as js_file:
js_file.write('document.getElementByTagName("div");')

with run_preview():
response = requests.get(url, timeout=0.01)
before = response.text
assert expected not in before

with js_path.open('w') as js_file:
js_file.write('\n' + new_js + '\n')

# Ensure new style is available after reload
read_timeout = False
after = before
while after == before:
try:
response = requests.get(url, timeout=0.1)
except requests.exceptions.ReadTimeout:
# happens during restart
read_timeout = True
else:
after = response.text

assert read_timeout
assert before != after
assert expected in after

0 comments on commit 5b6b84f

Please sign in to comment.