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

1from pathlib import Path 

2 

3import click 

4import pytest 

5 

6from prisma.cli import utils 

7from tests.utils import Runner 

8 

9 

10class PrismaCLI(utils.PrismaCLI): 

11 base_package = 'tests.test_cli.commands' 

12 folder = Path(__file__).parent / 'commands' 

13 

14 

15@pytest.fixture 

16def ctx(cli: PrismaCLI) -> click.Context: 

17 return cli.make_context('prisma py', ['example']) 

18 

19 

20@pytest.fixture 

21def cli() -> PrismaCLI: 

22 return PrismaCLI() 

23 

24 

25@pytest.fixture 

26def runner(runner: Runner, cli: PrismaCLI) -> Runner: 

27 runner.default_cli = cli 

28 return runner 

29 

30 

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 ) 

38 

39 

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 ) 

49 

50 

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 

56 

57 

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