Coverage for tests/test_shell.py: 100.00%

17 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-16 20:19 +0200

1"""Tests for the shell formatters.""" 

2 

3from __future__ import annotations 

4 

5from textwrap import dedent 

6from typing import TYPE_CHECKING 

7 

8if TYPE_CHECKING: 

9 import pytest 

10 from markdown import Markdown 

11 

12 

13def test_output_markdown(md: Markdown) -> None: 

14 """Assert Markdown is converted to HTML. 

15 

16 Parameters: 

17 md: A Markdown instance (fixture). 

18 """ 

19 html = md.convert( 

20 dedent( 

21 """ 

22 ```sh exec="yes" 

23 echo "**Bold!**" 

24 ``` 

25 """, 

26 ), 

27 ) 

28 assert html == "<p><strong>Bold!</strong></p>" 

29 

30 

31def test_output_html(md: Markdown) -> None: 

32 """Assert HTML is injected as is. 

33 

34 Parameters: 

35 md: A Markdown instance (fixture). 

36 """ 

37 html = md.convert( 

38 dedent( 

39 """ 

40 ```sh exec="yes" html="yes" 

41 echo "**Bold!**" 

42 ``` 

43 """, 

44 ), 

45 ) 

46 assert html == "<p>**Bold!**\n</p>" 

47 

48 

49def test_error_raised(md: Markdown, caplog: pytest.LogCaptureFixture) -> None: 

50 """Assert errors properly log a warning and return a formatted traceback. 

51 

52 Parameters: 

53 md: A Markdown instance (fixture). 

54 caplog: Pytest fixture to capture logs. 

55 """ 

56 html = md.convert( 

57 dedent( 

58 """ 

59 ```sh exec="yes" 

60 echo("wrong syntax") 

61 ``` 

62 """, 

63 ), 

64 ) 

65 assert "error" in html 

66 assert "Execution of sh code block exited with unexpected code 2" in caplog.text 

67 

68 

69def test_return_code(md: Markdown, caplog: pytest.LogCaptureFixture) -> None: 

70 """Assert return code is used correctly. 

71 

72 Parameters: 

73 md: A Markdown instance (fixture). 

74 caplog: Pytest fixture to capture logs. 

75 """ 

76 html = md.convert( 

77 dedent( 

78 """ 

79 ```sh exec="yes" returncode="1" 

80 echo Not in the mood 

81 exit 1 

82 ``` 

83 """, 

84 ), 

85 ) 

86 assert "Not in the mood" in html 

87 assert "exited with" not in caplog.text