Coverage for tests/test_cli/test_utils.py: 100%
35 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1from pathlib import Path
3import click
4import pytest
6from prisma.cli import utils
7from tests.utils import Runner
10class PrismaCLI(utils.PrismaCLI):
11 base_package = 'tests.test_cli.commands'
12 folder = Path(__file__).parent / 'commands'
15@pytest.fixture
16def ctx(cli: PrismaCLI) -> click.Context:
17 return cli.make_context('prisma py', ['example'])
20@pytest.fixture
21def cli() -> PrismaCLI:
22 return PrismaCLI()
25@pytest.fixture
26def runner(runner: Runner, cli: PrismaCLI) -> Runner:
27 runner.default_cli = cli
28 return runner
31def test_missing_cli_attr(runner: Runner) -> None:
32 """Loading command that does not have a cli attribute raises an error"""
33 result = runner.invoke(['missing_cli_attr'])
34 assert isinstance(result.exception, AssertionError)
35 assert str(result.exception) == (
36 'Expected command module tests.test_cli.commands.missing_cli_attr ' 'to contain a "cli" attribute'
37 )
40def test_wrong_cli_type(runner: Runner) -> None:
41 """Loading command with a cli attribute that is not a click command raises an error"""
42 result = runner.invoke(['wrong_cli_type'])
43 assert isinstance(result.exception, AssertionError)
44 assert str(result.exception) == (
45 'Expected command module attribute tests.test_cli.commands.wrong_cli_type.cli '
46 "to be a <class 'click.core.Command'> instance but got <class 'function'> "
47 'instead'
48 )
51def test_example_command(runner: Runner) -> None:
52 """Basic correctly implemented command"""
53 result = runner.invoke(['example'])
54 assert result.output == 'Hello from example command!\n'
55 assert result.exit_code == 0
58def test_list_commands(cli: PrismaCLI, ctx: click.Context) -> None:
59 """List commands ignores private modules"""
60 commands = cli.list_commands(ctx)
61 assert 'foo' in commands
62 assert 'example' in commands
63 assert '_private' not in commands