Skip to content

Commit

Permalink
chore: escape ANSI coloring in tests (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
hille721 authored Feb 6, 2024
1 parent 8297cdd commit 74eff41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/ansible_variables/utils/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
display = Display()


def escape_ansi(line):
"""The debug output contains ANSI codings, we need to remove them"""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)


@dataclass
class VariableSource:
"""Class for keeping track of an variable source item"""
Expand Down Expand Up @@ -51,12 +57,6 @@ def source_mapped(self) -> str:
def files(self) -> List[str]:
return self.parse_files_from_debug_log()

@staticmethod
def escape_ansi(line):
"""The debug output contains ANSI codings, we need to remove them"""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)

def parse_files_from_debug_log(self) -> List[str]:
"""The debug output from `variable_manager.get_vars()` contains all filenames
from which the variables were loaded.
Expand All @@ -70,7 +70,7 @@ def parse_files_from_debug_log(self) -> List[str]:
return files

for line in self.debuglog.splitlines():
found = re.search(r"Loading data from ([^\s]*)", self.escape_ansi(line))
found = re.search(r"Loading data from ([^\s]*)", escape_ansi(line))
if found:
files.extend(found.groups())

Expand Down
7 changes: 4 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ansible import constants as C

from ansible_variables.cli.variables import VariablesCLI, main
from ansible_variables.utils.vars import escape_ansi

C.set_constant("CONFIG_FILE", "tests/test_data/ansible.cfg")
C.set_constant("DEFAULT_HOST_LIST", "tests/test_data/inventory")
Expand Down Expand Up @@ -29,15 +30,15 @@ def test_cli_from_all(capsys):
variables_cli = VariablesCLI(["ansible-variables", server, "--var", "from_all"])
variables_cli.run()
captured = "".join(capsys.readouterr().out.splitlines())
assert "from_all: hello - inventory group_vars/all" == captured
assert "from_all: hello - inventory group_vars/all" == escape_ansi(captured)


def test_cli_from_all_v(capsys):
variables_cli = VariablesCLI(["ansible-variables", "server1", "--var", "from_all", "-v"])
variables_cli.run()
captured = "".join(capsys.readouterr().out.splitlines())
assert "from_all: hello - inventory group_vars/all" in captured
assert "tests/test_data/inventory/group_vars/all/all" in captured
assert "from_all: hello - inventory group_vars/all" in escape_ansi(captured)
assert "tests/test_data/inventory/group_vars/all/all" in escape_ansi(captured)


def test_cli_from_all_vvv(capsys):
Expand Down

0 comments on commit 74eff41

Please sign in to comment.