Coverage for tests/unit_tests/common/test_apio_console.py: 100%
68 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""Test for the apio_console.py."""
11import os
12import pytest
13from pathlib import Path
14from apio.common import apio_console
15from tests.conftest import ApioRunner
16from apio.common.apio_console import (
17 FORCE_TERMINAL,
18 cstyle,
19 cunstyle,
20 fatal_error,
21)
24def test_style_unstyle():
25 """Test the styling and unstyling functions"""
27 apio_console.configure(terminal_mode=FORCE_TERMINAL, theme_name="light")
29 # -- Test cstyle()
30 assert cstyle("") == ""
31 assert cstyle("", style="red") == ""
32 assert cstyle("abc xyz", style="red") == "\x1b[31mabc xyz\x1b[0m"
33 assert cstyle("abc xyz", style="cyan bold") == "\x1b[1;36mabc xyz\x1b[0m"
34 assert cstyle("ab \n xy", style="cyan bold") == "\x1b[1;36mab \n xy\x1b[0m"
36 # -- Test cunstyle() with plain text.
37 assert cunstyle("") == ""
38 assert cunstyle("abc xyz") == "abc xyz"
40 # -- Test cunstyle() with colored text.
41 assert cunstyle(cstyle("")) == ""
42 assert cunstyle(cstyle("abc xyz")) == "abc xyz"
43 assert cunstyle(cstyle("ab \n xy")) == "ab \n xy"
46def test_fatal_error(apio_runner: ApioRunner):
47 """Test the fatal_error() function."""
49 with apio_runner.in_sandbox():
51 # -- Test with a single error line
52 with apio_runner.with_logger() as log:
53 with pytest.raises(SystemExit) as e:
54 fatal_error("test error line 1")
55 assert e.value.code == 1
56 assert "Error: test error line 1" in log.out
57 assert "set env var APIO_DEBUG=1" in log.out
59 # -- Test with a multiple error lines
60 with apio_runner.with_logger() as log:
61 with pytest.raises(SystemExit) as e:
62 fatal_error(
63 "test error line 1",
64 "test error line 2",
65 )
66 assert e.value.code == 1
67 assert "Error: test error line 1" in log.out
68 assert "test error line 2" in log.out
69 assert "Error: test error line 2" not in log.out
70 assert "set env var APIO_DEBUG=1" in log.out
72 # -- Make an exception object with stack info.
73 try:
74 raise RuntimeError("my fake exception error")
75 except RuntimeError as exp:
76 test_exc: RuntimeError = exp
78 # -- Test with a cause exception only.
79 with apio_runner.with_logger() as log:
80 with pytest.raises(SystemExit) as e:
81 fatal_error(cause=test_exc)
82 assert e.value.code == 1
83 assert "Error: test error line 1" not in log.out
84 assert "my fake exception error" in log.out
85 assert "set env var APIO_DEBUG=1" in log.out
87 # -- Test with no error text and no cause. Fatal error should
88 # -- fail on assertion with the message 'No errors to print'.
89 with apio_runner.with_logger() as log:
90 with pytest.raises(SystemExit) as e:
91 fatal_error(info="info-line")
92 assert e.value.code == 1
93 assert "Error: Unspecified error" in log.out
94 assert "info line" not in log.out
95 assert "set env var APIO_DEBUG=1" in log.out
97 # -- Test with a cause exception.
98 with apio_runner.with_logger() as log:
99 with pytest.raises(SystemExit) as e:
100 fatal_error("test error line 1", cause=test_exc)
101 assert e.value.code == 1
102 assert "Error: test error line 1" in log.out
103 assert "my fake exception error" in log.out
104 assert "set env var APIO_DEBUG=1" in log.out
106 # -- Test with a cause exception and APIO_DEBUG=1.
107 os.environ["APIO_DEBUG"] = "1"
108 with apio_runner.with_logger() as log:
109 with pytest.raises(SystemExit) as e:
110 fatal_error("test error line 1", cause=test_exc)
111 assert e.value.code == 1
112 assert "Error: test error line 1" in log.out
113 assert "my fake exception error" in log.out
114 assert str(Path("unit_tests/common/test_apio_console.py")) in log.out
115 assert "set env var APIO_DEBUG=1" not in log.out