File: test_cli_new.py

package info (click to toggle)
sphinx-theme-builder 0.2.0b2-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 448 kB
  • sloc: python: 2,227; sh: 19; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import subprocess
import sys
from pathlib import Path
from unittest import mock

from click import Group
from click.testing import CliRunner

from sphinx_theme_builder._internal.cli.new import _TEMPLATE_URL
from sphinx_theme_builder._internal.errors import DiagnosticError


class TestNewCommand:
    """`stb new`"""

    def test_aborts_when_setup_py_exists(self, runner: CliRunner, cli: Group) -> None:
        with runner.isolated_filesystem() as directory:
            (Path(directory) / "setup.py").write_text("")

            with mock.patch("subprocess.run") as mocked_run:
                process = runner.invoke(cli, ["new", directory])

        assert mocked_run.call_count == 0
        assert process.exit_code == 1, process

    def test_aborts_when_pyproject_toml_exists(
        self, runner: CliRunner, cli: Group
    ) -> None:
        with runner.isolated_filesystem() as directory:
            (Path(directory) / "pyproject.toml").write_text("")

            with mock.patch("subprocess.run") as mocked_run:
                process = runner.invoke(cli, ["new", directory])

        assert mocked_run.call_count == 0
        assert process.exit_code == 1, process

    def test_calls_cookiecutter(self, runner: CliRunner, cli: Group) -> None:
        with runner.isolated_filesystem() as directory:
            with mock.patch("subprocess.run") as mocked_run:
                process = runner.invoke(cli, ["new", directory])

        assert process.exit_code == 0, process
        assert mocked_run.call_count == 1
        assert mocked_run.call_args == mock.call(
            [sys.executable, "-m", "cookiecutter", "-o", directory, _TEMPLATE_URL],
            check=True,
        )

    def test_cookiecutter_failure(self, runner: CliRunner, cli: Group) -> None:
        with runner.isolated_filesystem() as directory:
            with mock.patch("subprocess.run") as mocked_run:
                mocked_run.side_effect = subprocess.CalledProcessError(
                    returncode=2, cmd="placeholder"
                )
                process = runner.invoke(cli, ["new", directory])

        assert mocked_run.call_count == 1
        assert mocked_run.call_args == mock.call(
            [sys.executable, "-m", "cookiecutter", "-o", directory, _TEMPLATE_URL],
            check=True,
        )

        assert process.exit_code == 1, process
        assert isinstance(process.exception, DiagnosticError)