Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview will serve most recent static files #26

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading