1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 20:52:43 +00:00

fixed devserver

so it does crash on build failure anymore. also made tests a bit faster
by adding a wait parameter to the autoreload method.
This commit is contained in:
Bastian Venthur
2024-04-24 21:47:59 +02:00
parent 4d4630867d
commit 0236dbf782
3 changed files with 53 additions and 18 deletions

View File

@@ -1,5 +1,10 @@
# Changelog # Changelog
## [unreleased]
* Fixed devsever so it does not crash when the (re-)build fails anymore
* updated dependencies
## [2.2.1] -- 2023-11-11 ## [2.2.1] -- 2023-11-11
* fixed `suggests` to blag-doc * fixed `suggests` to blag-doc

View File

@@ -52,7 +52,7 @@ def get_last_modified(dirs: list[str]) -> float:
return last_mtime return last_mtime
def autoreload(args: argparse.Namespace) -> NoReturn: def autoreload(args: argparse.Namespace, wait: int=1) -> NoReturn:
"""Start the autoreloader. """Start the autoreloader.
This method monitors the given directories for changes (i.e. the This method monitors the given directories for changes (i.e. the
@@ -66,6 +66,9 @@ def autoreload(args: argparse.Namespace) -> NoReturn:
---------- ----------
args args
contains the input-, template- and static dir contains the input-, template- and static dir
wait
number of seconds the devsever waits before checking for updated
content
""" """
dirs = [args.input_dir, args.template_dir, args.static_dir] dirs = [args.input_dir, args.template_dir, args.static_dir]
@@ -74,12 +77,18 @@ def autoreload(args: argparse.Namespace) -> NoReturn:
# loop to avoid serving stale contents # loop to avoid serving stale contents
last_mtime = 0.0 last_mtime = 0.0
while True: while True:
mtime = get_last_modified(dirs) # make sure the devsever does not crash when the build fails with an
if mtime > last_mtime: # exception
last_mtime = mtime try:
logger.info("Change detected, rebuilding...") mtime = get_last_modified(dirs)
blag.build(args) if mtime > last_mtime:
time.sleep(1) last_mtime = mtime
logger.info("Change detected, rebuilding...")
blag.build(args)
time.sleep(wait)
except Exception:
logger.exception("Error occurred during rebuild:")
logger.info("Devserver did not crash, you may continue editing.")
def serve(args: argparse.Namespace) -> None: def serve(args: argparse.Namespace) -> None:

View File

@@ -8,10 +8,10 @@ import threading
import time import time
from argparse import Namespace from argparse import Namespace
import pytest
from blag import devserver from blag import devserver
WAITTIME = 0.1
def test_get_last_modified(cleandir: str) -> None: def test_get_last_modified(cleandir: str) -> None:
"""Test get_last_modified.""" """Test get_last_modified."""
@@ -19,13 +19,13 @@ def test_get_last_modified(cleandir: str) -> None:
t1 = devserver.get_last_modified(["content"]) t1 = devserver.get_last_modified(["content"])
# wait a bit, create a file and measure again # wait a bit, create a file and measure again
time.sleep(0.1) time.sleep(WAITTIME)
with open("content/test", "w") as fh: with open("content/test", "w") as fh:
fh.write("boo") fh.write("boo")
t2 = devserver.get_last_modified(["content"]) t2 = devserver.get_last_modified(["content"])
# wait a bit and take time again # wait a bit and take time again
time.sleep(0.1) time.sleep(WAITTIME)
t3 = devserver.get_last_modified(["content"]) t3 = devserver.get_last_modified(["content"])
assert t2 > t1 assert t2 > t1
@@ -40,14 +40,14 @@ def test_autoreload_builds_immediately(args: Namespace) -> None:
t = threading.Thread( t = threading.Thread(
target=devserver.autoreload, target=devserver.autoreload,
args=(args,), args=(args, WAITTIME),
daemon=True, daemon=True,
) )
t0 = devserver.get_last_modified(["build"]) t0 = devserver.get_last_modified(["build"])
t.start() t.start()
# try for 5 seconds... # try for 5 seconds...
for i in range(5): for i in range(5):
time.sleep(1) time.sleep(WAITTIME)
t1 = devserver.get_last_modified(["build"]) t1 = devserver.get_last_modified(["build"])
print(t1) print(t1)
if t1 > t0: if t1 > t0:
@@ -55,14 +55,11 @@ def test_autoreload_builds_immediately(args: Namespace) -> None:
assert t1 > t0 assert t1 > t0
@pytest.mark.filterwarnings(
"ignore::pytest.PytestUnhandledThreadExceptionWarning"
)
def test_autoreload(args: Namespace) -> None: def test_autoreload(args: Namespace) -> None:
"""Test autoreload.""" """Test autoreload."""
t = threading.Thread( t = threading.Thread(
target=devserver.autoreload, target=devserver.autoreload,
args=(args,), args=(args, WAITTIME),
daemon=True, daemon=True,
) )
t.start() t.start()
@@ -75,8 +72,32 @@ def test_autoreload(args: Namespace) -> None:
# try for 5 seconds to see if we rebuild once... # try for 5 seconds to see if we rebuild once...
for i in range(5): for i in range(5):
time.sleep(1) time.sleep(WAITTIME)
t1 = devserver.get_last_modified(["build"]) t1 = devserver.get_last_modified(["build"])
if t1 > t0: if t1 > t0:
break break
assert t1 > t0 assert t1 > t0
def test_autoreload_does_not_crash(args: Namespace) -> None:
"""Test autoreload does not crash if build fails."""
t = threading.Thread(
target=devserver.autoreload,
args=(args, WAITTIME),
daemon=True,
)
t.start()
t0 = devserver.get_last_modified(["build"])
# create a file that causes build to crash
with open("content/test.md", "w") as fh:
fh.write("date: ")
# try for 5 seconds to see if we rebuild once...
for i in range(5):
time.sleep(WAITTIME)
t1 = devserver.get_last_modified(["build"])
if t1 > t0:
break
assert t.is_alive()