Coverage for tests/unit_tests/common/test_apio_console.py: 100%

68 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1"""Test for the apio_console.py.""" 

2 

3import os 

4import pytest 

5from pathlib import Path 

6from apio.common import apio_console 

7from tests.conftest import ApioRunner 

8from apio.common.apio_console import ( 

9 FORCE_TERMINAL, 

10 cstyle, 

11 cunstyle, 

12 fatal_error, 

13) 

14 

15 

16def test_style_unstyle(): 

17 """Test the styling and unstyling functions""" 

18 

19 apio_console.configure(terminal_mode=FORCE_TERMINAL, theme_name="light") 

20 

21 # -- Test cstyle() 

22 assert cstyle("") == "" 

23 assert cstyle("", style="red") == "" 

24 assert cstyle("abc xyz", style="red") == "\x1b[31mabc xyz\x1b[0m" 

25 assert cstyle("abc xyz", style="cyan bold") == "\x1b[1;36mabc xyz\x1b[0m" 

26 assert cstyle("ab \n xy", style="cyan bold") == "\x1b[1;36mab \n xy\x1b[0m" 

27 

28 # -- Test cunstyle() with plain text. 

29 assert cunstyle("") == "" 

30 assert cunstyle("abc xyz") == "abc xyz" 

31 

32 # -- Test cunstyle() with colored text. 

33 assert cunstyle(cstyle("")) == "" 

34 assert cunstyle(cstyle("abc xyz")) == "abc xyz" 

35 assert cunstyle(cstyle("ab \n xy")) == "ab \n xy" 

36 

37 

38def test_fatal_error(apio_runner: ApioRunner): 

39 """Test the fatal_error() function.""" 

40 

41 with apio_runner.in_sandbox(): 

42 

43 # -- Test with a single error line 

44 with apio_runner.with_logger() as log: 

45 with pytest.raises(SystemExit) as e: 

46 fatal_error("test error line 1") 

47 assert e.value.code == 1 

48 assert "Error: test error line 1" in log.out 

49 assert "set env var APIO_DEBUG=1" in log.out 

50 

51 # -- Test with a multiple error lines 

52 with apio_runner.with_logger() as log: 

53 with pytest.raises(SystemExit) as e: 

54 fatal_error( 

55 "test error line 1", 

56 "test error line 2", 

57 ) 

58 assert e.value.code == 1 

59 assert "Error: test error line 1" in log.out 

60 assert "test error line 2" in log.out 

61 assert "Error: test error line 2" not in log.out 

62 assert "set env var APIO_DEBUG=1" in log.out 

63 

64 # -- Make an exception object with stack info. 

65 try: 

66 raise RuntimeError("my fake exception error") 

67 except RuntimeError as exp: 

68 test_exc: RuntimeError = exp 

69 

70 # -- Test with a cause exception only. 

71 with apio_runner.with_logger() as log: 

72 with pytest.raises(SystemExit) as e: 

73 fatal_error(cause=test_exc) 

74 assert e.value.code == 1 

75 assert "Error: test error line 1" not in log.out 

76 assert "my fake exception error" in log.out 

77 assert "set env var APIO_DEBUG=1" in log.out 

78 

79 # -- Test with no error text and no cause. Fatal error should 

80 # -- fail on assertion with the message 'No errors to print'. 

81 with apio_runner.with_logger() as log: 

82 with pytest.raises(SystemExit) as e: 

83 fatal_error(info="info-line") 

84 assert e.value.code == 1 

85 assert "Error: Unspecified error" in log.out 

86 assert "info line" not in log.out 

87 assert "set env var APIO_DEBUG=1" in log.out 

88 

89 # -- Test with a cause exception. 

90 with apio_runner.with_logger() as log: 

91 with pytest.raises(SystemExit) as e: 

92 fatal_error("test error line 1", cause=test_exc) 

93 assert e.value.code == 1 

94 assert "Error: test error line 1" in log.out 

95 assert "my fake exception error" in log.out 

96 assert "set env var APIO_DEBUG=1" in log.out 

97 

98 # -- Test with a cause exception and APIO_DEBUG=1. 

99 os.environ["APIO_DEBUG"] = "1" 

100 with apio_runner.with_logger() as log: 

101 with pytest.raises(SystemExit) as e: 

102 fatal_error("test error line 1", cause=test_exc) 

103 assert e.value.code == 1 

104 assert "Error: test error line 1" in log.out 

105 assert "my fake exception error" in log.out 

106 assert str(Path("unit_tests/common/test_apio_console.py")) in log.out 

107 assert "set env var APIO_DEBUG=1" not in log.out