From b94a92ec0aa2209dbc4e73f954e3336792376080 Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Mon, 13 Nov 2023 22:30:02 -0800 Subject: [PATCH] fix: Tailwind fails if `lektor build` run from outside the Lektor project directory (#4) * fix: type annotation * test: run tests from outside the Lektor project dir Two changes to the tests: 1. Run Lektor from cwd rather than the project directory (setting the $LEKTOR_PROJECT environment variable so that Lektor can find the project). 2. Rather than just checking for "tailwindcss" in the output CSS file, check for one of the classes used in the example project (e.g. ".flex") to ensure that tailwind actually found the source files. * fix(lektor build): chdir to project dir before running tailwind --- lektor_tailwind.py | 3 ++- tests/test_plugin.py | 58 ++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/lektor_tailwind.py b/lektor_tailwind.py index 84d6d1f..968f21d 100644 --- a/lektor_tailwind.py +++ b/lektor_tailwind.py @@ -22,7 +22,7 @@ def __init__(self, env, id): self.watch = False self.css_path = config.get("css_path", "static/style.css") self.input_css = os.path.join(self.env.root_path, "assets", self.css_path) - self.tailwind: threading.Thread | None = None + self.tailwind: subprocess.Popen | None = None def on_setup_env(self, **extra): self.init_tailwindcss() @@ -65,6 +65,7 @@ def compile_css(self, output_path: str): "--minify", ], check=True, + cwd=self.env.root_path, ) def on_server_spawn(self, **extra): diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 10e1015..38a63a1 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -17,12 +17,12 @@ @pytest.fixture -def tmp_project(tmp_path): - """Chdir into a temporary directory containing a copy of the example - test project. +def tmp_project_path(tmp_path, monkeypatch): + """Copy the example test project to a temporary location. The source files for the lektor_tailwind plugin are copied into the the ``packages`` directory of the temporary project. + """ testdir = Path(__file__).parent example = testdir / "example" @@ -41,20 +41,23 @@ def tmp_project(tmp_path): for fn in PACKAGE_FILES: shutil.copy(srcdir / fn, packagedir / fn) - save_cwd = os.getcwd() - try: - os.chdir(project) - yield project - finally: - os.chdir(save_cwd) + monkeypatch.setenv("LEKTOR_PROJECT", os.fspath(project)) + + return project @pytest.fixture -def slow_build(tmp_project): +def output_css_path(tmp_project_path): + """Where to expect the built CSS.""" + return tmp_project_path / "_build/static/style.css" + + +@pytest.fixture +def slow_build(tmp_project_path): """Delay Lektor server's build_all.""" build_delay = 0.5 - plugin = tmp_project / "packages/slow_build" + plugin = tmp_project_path / "packages/slow_build" plugin.mkdir(parents=True) plugin.joinpath("setup.py").write_text(dedent(""" from setuptools import setup @@ -160,39 +163,42 @@ def _communicate1(self, timeout): @pytest.fixture -def lektor_server(tmp_project): +def lektor_server(tmp_project_path): with LektorServerFixture() as fixture: yield fixture -OUTPUT_CSS = Path("_build/static/style.css") - - @pytest.mark.parametrize("prune", ("--prune", "--no-prune")) -def test_build_builds_css(tmp_project, prune): +def test_build_builds_css(tmp_project_path, prune, output_css_path): subprocess.run(("lektor", "build", prune), check=True, timeout=30) - assert "tailwindcss" in OUTPUT_CSS.read_text() + # Check for utility class name rather than just "tailwindcss" + # to confirm that tailwind actually found the project source files. + assert ".flex" in output_css_path.read_text() -def test_server_rebuilds_css_when_template_updated(lektor_server): +def test_server_rebuilds_css_when_template_updated( + lektor_server, tmp_project_path, output_css_path +): lektor_server.wait_for_build() - assert "tailwindcss" in OUTPUT_CSS.read_text() + assert ".flex" in output_css_path.read_text() - with open("templates/layout.html", "a") as fp: + with open(tmp_project_path / "templates/layout.html", "a") as fp: fp.write("""
\n""") lektor_server.wait_for_build() - assert "SENTINEL" in OUTPUT_CSS.read_text() + assert "SENTINEL" in output_css_path.read_text() @pytest.mark.usefixtures("slow_build") # excercise race condition -def test_server_rebuilds_css_when_input_css_updated(lektor_server): +def test_server_rebuilds_css_when_input_css_updated( + lektor_server, tmp_project_path, output_css_path +): lektor_server.wait_for_build() - assert "tailwindcss" in OUTPUT_CSS.read_text() + assert ".flex" in output_css_path.read_text() - with open("assets/static/style.css", "a") as fp: + with open(tmp_project_path / "assets/static/style.css", "a") as fp: fp.write(".SENTINEL { color: red }\n") lektor_server.wait_for_build() - assert ".SENTINEL" in OUTPUT_CSS.read_text() - assert "tailwindcss" in OUTPUT_CSS.read_text() + assert ".SENTINEL" in output_css_path.read_text() + assert ".flex" in output_css_path.read_text()