Skip to content

Commit

Permalink
Added basic tests for runserver and runserver_plus.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverandrich committed Oct 4, 2023
1 parent ec0a4ef commit 9667768
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/django_tailwind_cli/management/commands/tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
30 changes: 29 additions & 1 deletion tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9667768

Please sign in to comment.