• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

berserkhmdvhb / charfinder / 15835271300

23 Jun 2025 09:13PM UTC coverage: 99.189% (+0.5%) from 98.704%
15835271300

push

github

berserkhmdvhb
fixed usage of normalization_profile in codebase

9 of 10 new or added lines in 6 files covered. (90.0%)

4 existing lines in 3 files now uncovered.

1467 of 1479 relevant lines covered (99.19%)

7.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.74
/src/charfinder/cli/diagnostics.py
1
"""
2
Diagnostics utilities for user-facing CLI debug output.
3

4
Provides human-readable runtime diagnostics when the `--debug` flag is passed.
5

6
Behavior:
7
    - Output is printed directly to stdout (if show=True).
8
    - ANSI coloring is applied based on `--color` flag or terminal support.
9
    - Output includes CLI arguments, match diagnostics, and .env file(s).
10
    - Output is always logged (level DEBUG).
11
"""
12

13
# ---------------------------------------------------------------------
14
# Imports
15
# ---------------------------------------------------------------------
16

17
import os
8✔
18
from argparse import Namespace
8✔
19

20
from dotenv import dotenv_values
8✔
21

22
from charfinder.cli.diagnostics_match import print_match_diagnostics
8✔
23
from charfinder.config.constants import ENV_DEBUG_ENV_LOAD
8✔
24
from charfinder.config.messages import (
8✔
25
    MSG_DEBUG_ARG_ITEM,
26
    MSG_DEBUG_DOTENV_EMPTY,
27
    MSG_DEBUG_DOTENV_END,
28
    MSG_DEBUG_DOTENV_LOADED_FILES,
29
    MSG_DEBUG_DOTENV_READ_ERROR,
30
    MSG_DEBUG_DOTENV_SELECTED,
31
    MSG_DEBUG_DOTENV_START,
32
    MSG_DEBUG_ENV_VAR,
33
    MSG_DEBUG_NO_DOTENV_FOUND,
34
    MSG_DEBUG_NORMALIZED_QUERY_TOKENS,
35
    MSG_DEBUG_PARSED_ARGS,
36
    MSG_DEBUG_SECTION_END,
37
    MSG_DEBUG_SECTION_START,
38
)
39
from charfinder.config.settings import resolve_dotenv_path
8✔
40
from charfinder.config.types import MatchDiagnosticsInfo
8✔
41
from charfinder.utils.formatter import echo
8✔
42
from charfinder.utils.logger_styles import format_debug
8✔
43
from charfinder.utils.normalizer import normalize
8✔
44

45
__all__ = [
8✔
46
    "print_debug_diagnostics",
47
    "print_dotenv_debug",
48
]
49

50
# ---------------------------------------------------------------------
51
# Internal Utility
52
# ---------------------------------------------------------------------
53

54

55
def _debug_echo(msg: str, *, use_color: bool, show: bool = True) -> None:
8✔
56
    """Wrapper around echo for debug diagnostics."""
57
    echo(
8✔
58
        msg,
59
        style=lambda m: format_debug(m, use_color=use_color),
60
        show=show,
61
        log=True,
62
        log_method="debug",
63
    )
64

65

66
# ---------------------------------------------------------------------
67
# Diagnostics Functions
68
# ---------------------------------------------------------------------
69

70

71
def print_debug_diagnostics(
8✔
72
    args: Namespace,
73
    *,
74
    match_info: MatchDiagnosticsInfo | None = None,
75
    use_color: bool = False,
76
    show: bool = True,
77
) -> None:
78
    """
79
    Print structured diagnostics when `--debug` is active.
80

81
    Includes:
82
    - CLI arguments as parsed
83
    - Fuzzy/exact match info if provided
84
    - Loaded .env file details
85

86
    Args:
87
        args: Parsed CLI arguments (argparse.Namespace)
88
        match_info: Match context returned by matcher, if available
89
        use_color: Whether to apply ANSI formatting
90
        show: If True, print to terminal; always logged.
91
    """
92
    _debug_echo(msg=MSG_DEBUG_SECTION_START, use_color=use_color, show=show)
8✔
93

94
    _debug_echo(msg=MSG_DEBUG_PARSED_ARGS, use_color=use_color, show=show)
8✔
95
    for key, value in sorted(vars(args).items()):
8✔
96
        _debug_echo(
8✔
97
            msg=MSG_DEBUG_ARG_ITEM.format(key=key, value=value), use_color=use_color, show=show
98
        )
99

100
    _debug_echo(
8✔
101
        msg=MSG_DEBUG_ENV_VAR.format(
102
            env_var=ENV_DEBUG_ENV_LOAD, value=os.getenv(ENV_DEBUG_ENV_LOAD, "0")
103
        ),
104
        use_color=use_color,
105
        show=show,
106
    )
107

108
    query_tokens = getattr(args, "option_query", None) or getattr(args, "positional_query", None)
8✔
109
    if query_tokens and hasattr(args, "normalization_profile"):
8✔
NEW
110
        normalized_tokens = [normalize(q, profile=args.normalization_profile) for q in query_tokens]
×
UNCOV
111
        _debug_echo(
×
112
            msg=MSG_DEBUG_NORMALIZED_QUERY_TOKENS.format(tokens=normalized_tokens),
113
            use_color=use_color,
114
            show=show,
115
        )
116

117
    if match_info:
8✔
118
        print_match_diagnostics(
8✔
119
            args=args,
120
            match_info=match_info,
121
            use_color=use_color,
122
            show=show,
123
        )
124

125
    _debug_echo(msg=MSG_DEBUG_DOTENV_LOADED_FILES, use_color=use_color, show=show)
8✔
126
    print_dotenv_debug(use_color=use_color, show=show)
8✔
127

128
    _debug_echo(msg=MSG_DEBUG_SECTION_END, use_color=use_color, show=show)
8✔
129

130

131
def print_dotenv_debug(*, use_color: bool = False, show: bool = True) -> None:
8✔
132
    """
133
    Print details of the resolved .env file and its contents.
134

135
    Intended for CLI `--debug` output (diagnostics only).
136

137
    Args:
138
        use_color (bool): Whether to apply ANSI formatting.
139
        show: If True, print to terminal; always logged.
140

141
    Raises:
142
        OSError: If reading the .env file fails due to IO issues.
143
        UnicodeDecodeError: If the file contains non-decodable bytes.
144
    """
145
    dotenv_path = resolve_dotenv_path()
8✔
146

147
    _debug_echo(msg=MSG_DEBUG_DOTENV_START, use_color=use_color, show=show)
8✔
148

149
    if not dotenv_path:
8✔
150
        _debug_echo(msg=MSG_DEBUG_NO_DOTENV_FOUND, use_color=use_color, show=show)
8✔
151
        _debug_echo(
8✔
152
            "Environment variables may only be coming from the OS.",
153
            use_color=use_color,
154
            show=show,
155
        )
156
        _debug_echo(msg=MSG_DEBUG_DOTENV_END, use_color=use_color, show=show)
8✔
157
        return
8✔
158

159
    _debug_echo(
8✔
160
        msg=MSG_DEBUG_DOTENV_SELECTED.format(path=dotenv_path), use_color=use_color, show=show
161
    )
162

163
    try:
8✔
164
        values = dotenv_values(dotenv_path=dotenv_path)
8✔
165

166
        if not values:
8✔
167
            _debug_echo(
8✔
168
                msg=MSG_DEBUG_DOTENV_EMPTY,
169
                use_color=use_color,
170
                show=show,
171
            )
172
        else:
173
            for key, value in values.items():
8✔
174
                _debug_echo(
8✔
175
                    msg=MSG_DEBUG_ARG_ITEM.format(key=key, value=value),
176
                    use_color=use_color,
177
                    show=show,
178
                )
179

180
    except (OSError, UnicodeDecodeError) as exc:
8✔
181
        _debug_echo(
8✔
182
            msg=MSG_DEBUG_DOTENV_READ_ERROR.format(error=exc), use_color=use_color, show=show
183
        )
184

185
    _debug_echo(msg=MSG_DEBUG_DOTENV_END, use_color=use_color, show=show)
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc