Coverage for tests/test_deprecation.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from typing import Optional 

2from harbor_cli.deprecation import ( 

3 get_deprecated_params, 

4 check_deprecated_option, 

5 used_deprecated, 

6 Deprecated, 

7) 

8 

9import typer 

10from typer.testing import CliRunner 

11from pytest import LogCaptureFixture 

12from pytest_mock import MockerFixture 

13 

14app = typer.Typer() 

15runner = CliRunner(mix_stderr=False) 

16 

17 

18def test_check_deprecated_option( 

19 mocker: MockerFixture, caplog: LogCaptureFixture 

20) -> None: 

21 @app.command() 

22 def main( 

23 ctx: typer.Context, 

24 option1: Optional[str] = typer.Option( 

25 None, 

26 "--option1", 

27 "-o", 

28 Deprecated("--long-option1", replacement="--option1"), 

29 help="Option 1.", 

30 ), 

31 option2: Optional[str] = typer.Option( 

32 None, 

33 "--option2", 

34 "-O", 

35 "--long-option2", 

36 help="Option 1.", 

37 ), 

38 ) -> None: 

39 # test everything inside this command for simplicity 

40 deprecated = get_deprecated_params(ctx) 

41 assert len(deprecated) == 1 

42 assert deprecated[0] == "--long-option1" 

43 assert deprecated[0].replacement == "--option1" 

44 

45 used = used_deprecated(ctx) 

46 assert len(used) == 1 

47 assert used[0] == "--long-option1" 

48 

49 check_deprecated_option(ctx) 

50 

51 args = ["--long-option1", "arg", "--long-option2", "arg"] 

52 # patch sys.argv to simulate the user passing in the deprecated option 

53 mocker.patch("sys.argv", args) 

54 

55 result = runner.invoke(app, args) 

56 

57 # We don't test the _exact_ message because it's not important 

58 assert "--long-option1" in caplog.text # deprecated 

59 assert "--option1" in caplog.text # replacement 

60 assert "deprecated" in caplog.text