From 96677683aea755c98de43cfec6ec1a913bf466a1 Mon Sep 17 00:00:00 2001 From: Oliver Andrich Date: Wed, 4 Oct 2023 14:22:07 +0200 Subject: [PATCH] Added basic tests for runserver and runserver_plus. --- .../management/commands/tailwind.py | 4 +-- tests/conftest.py | 2 ++ tests/test_management_commands.py | 30 ++++++++++++++++++- tox.ini | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/django_tailwind_cli/management/commands/tailwind.py b/src/django_tailwind_cli/management/commands/tailwind.py index ed7e3e0..8a4be1d 100644 --- a/src/django_tailwind_cli/management/commands/tailwind.py +++ b/src/django_tailwind_cli/management/commands/tailwind.py @@ -89,10 +89,10 @@ def handle(self, *_args: Any, **kwargs: Any) -> None: self.build() elif label == "watch": self.watch() - elif label == "runserver": # pragma: no cover + elif label == "runserver": kwargs["runserver_cmd"] = "runserver" self.runserver(**kwargs) - elif label == "runserver_plus": # pragma: no cover + elif label == "runserver_plus": if importlib.util.find_spec("django_extensions") and importlib.util.find_spec("werkzeug"): kwargs["runserver_cmd"] = "runserver" self.runserver(**kwargs) diff --git a/tests/conftest.py b/tests/conftest.py index cdf405b..92e1f0c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,8 @@ def mocked_calls(request: Any, mocker: MockerFixture): marker = request.node.get_closest_marker("mock_network_and_subprocess") if marker: mocker.resetall() + mocker.patch("multiprocessing.Process.start") + mocker.patch("multiprocessing.Process.join") mocker.patch("subprocess.run") mocker.patch("urllib.request.urlopen") mocker.patch("shutil.copyfileobj") diff --git a/tests/test_management_commands.py b/tests/test_management_commands.py index 727c12c..560682f 100644 --- a/tests/test_management_commands.py +++ b/tests/test_management_commands.py @@ -23,12 +23,23 @@ def test_download_cli(settings: LazySettings, tmpdir: Any): assert config.get_full_cli_path().exists() +@pytest.mark.mock_network_and_subprocess +def test_download_cli_without_tailwind_cli_path(settings: LazySettings, tmpdir: Any): + settings.TAILWIND_CLI_PATH = None + settings.BASE_DIR = tmpdir + config = Config() + + assert not config.get_full_cli_path().exists() + call_command("tailwind", "build") + assert config.get_full_cli_path().exists() + + def test_calling_unknown_subcommand(): """Unknown subcommands to the tailwind management command raise a `CommandError`.""" with pytest.raises( CommandError, - match=r"invalid choice: 'notavalidcommand' \(choose from 'build', 'watch', 'list_templates', 'runserver', 'runserver_plus'\)", + match=r"invalid choice: 'notavalidcommand' \(choose from 'build', 'watch', 'list_templates', 'runserver', 'runserver_plus'\)", # noqa: E501 ): call_command("tailwind", "notavalidcommand") @@ -184,6 +195,23 @@ def test_get_watch_cmd(settings: LazySettings): assert "--input" in TailwindCommand().get_watch_cmd() +@pytest.mark.mock_network_and_subprocess +def test_runserver(): + call_command("tailwind", "runserver") + + +@pytest.mark.mock_network_and_subprocess +def test_runserver_plus_with_django_extensions_installed(): + call_command("tailwind", "runserver_plus") + + +@pytest.mark.mock_network_and_subprocess +def test_runserver_plus_without_django_extensions_installed(mocker: MockerFixture): + mocker.patch.dict(sys.modules, {"django_extensions": None}) + with pytest.raises(CommandError, match=r"Missing dependencies."): + call_command("tailwind", "runserver_plus") + + def test_list_project_templates(capsys: Any): call_command("tailwind", "list_templates") captured = capsys.readouterr() diff --git a/tox.ini b/tox.ini index 773fb9e..2fe9272 100644 --- a/tox.ini +++ b/tox.ini @@ -23,6 +23,6 @@ deps = django32: Django>=3.2,<4.0 django41: Django>=4.1,<4.2 django42: Django>=4.2,<5.0 +extras = django-extensions commands = - django-admin --version pytest tests/ --no-header --import-mode importlib