Skip to content

Commit

Permalink
Removed use of colorama from debug printting
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Oct 30, 2020
1 parent 3e22a39 commit f4afff7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 120 deletions.
117 changes: 24 additions & 93 deletions lib/molecule/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,21 @@
import io
import os

import colorama
import pytest

from molecule import util
from molecule.console import console
from molecule.constants import MOLECULE_HEADER

colorama.init(autoreset=True)

def test_print_debug():

def test_print_debug(capsys):
util.print_debug("test_title", "test_data")
result, _ = capsys.readouterr()
title = [
colorama.Back.WHITE,
colorama.Style.BRIGHT,
colorama.Fore.BLACK,
"DEBUG: test_title",
colorama.Fore.RESET,
colorama.Back.RESET,
colorama.Style.RESET_ALL,
]
print("".join(title))

data = [
colorama.Fore.BLACK,
colorama.Style.BRIGHT,
"test_data",
colorama.Style.RESET_ALL,
colorama.Fore.RESET,
]
print("".join(data))
expected = "DEBUG: test_title:\ntest_data\n"
with console.capture() as capture:
util.print_debug("test_title", "test_data")

x, _ = capsys.readouterr()
assert x == result
result = util.strip_ansi_escape(capture.get())
assert result == expected


def test_print_environment_vars(capsys):
Expand All @@ -69,72 +50,22 @@ def test_print_environment_vars(capsys):
"MOLECULE_BAR": "bar",
"MOLECULE": None,
}
util.print_environment_vars(env)
result, _ = capsys.readouterr()

# Ansible Environment
title = [
colorama.Back.WHITE,
colorama.Style.BRIGHT,
colorama.Fore.BLACK,
"DEBUG: ANSIBLE ENVIRONMENT",
colorama.Fore.RESET,
colorama.Back.RESET,
colorama.Style.RESET_ALL,
]
print("".join(title))
data = [
colorama.Fore.BLACK,
colorama.Style.BRIGHT,
util.safe_dump({"ANSIBLE_FOO": "foo", "ANSIBLE_BAR": "bar"}),
colorama.Style.RESET_ALL,
colorama.Fore.RESET,
]
print("".join(data))

# Molecule Environment
title = [
colorama.Back.WHITE,
colorama.Style.BRIGHT,
colorama.Fore.BLACK,
"DEBUG: MOLECULE ENVIRONMENT",
colorama.Fore.RESET,
colorama.Back.RESET,
colorama.Style.RESET_ALL,
]
print("".join(title))
data = [
colorama.Fore.BLACK,
colorama.Style.BRIGHT,
util.safe_dump({"MOLECULE_FOO": "foo", "MOLECULE_BAR": "bar"}),
colorama.Style.RESET_ALL,
colorama.Fore.RESET,
]
print("".join(data))

# Shell Replay
title = [
colorama.Back.WHITE,
colorama.Style.BRIGHT,
colorama.Fore.BLACK,
"DEBUG: SHELL REPLAY",
colorama.Fore.RESET,
colorama.Back.RESET,
colorama.Style.RESET_ALL,
]
print("".join(title))
data = [
colorama.Fore.BLACK,
colorama.Style.BRIGHT,
"ANSIBLE_BAR=bar ANSIBLE_FOO=foo MOLECULE_BAR=bar MOLECULE_FOO=foo",
colorama.Style.RESET_ALL,
colorama.Fore.RESET,
]
print("".join(data))
print()
expected = """DEBUG: ANSIBLE ENVIRONMENT:
ANSIBLE_BAR: bar
ANSIBLE_FOO: foo
DEBUG: MOLECULE ENVIRONMENT:
MOLECULE_BAR: bar
MOLECULE_FOO: foo
DEBUG: SHELL REPLAY:
ANSIBLE_BAR=bar ANSIBLE_FOO=foo MOLECULE_BAR=bar MOLECULE_FOO=foo
"""

x, _ = capsys.readouterr()
assert x == result
with console.capture() as capture:
util.print_environment_vars(env)
result = util.strip_ansi_escape(capture.get())
assert result == expected


def test_sysexit():
Expand Down Expand Up @@ -180,8 +111,8 @@ def test_run_command_with_debug(mocker, patched_print_debug):
env = {"ANSIBLE_FOO": "foo", "MOLECULE_BAR": "bar"}
util.run_command(["ls"], debug=True, env=env)
x = [
mocker.call("ANSIBLE ENVIRONMENT", "---\nANSIBLE_FOO: foo\n"),
mocker.call("MOLECULE ENVIRONMENT", "---\nMOLECULE_BAR: bar\n"),
mocker.call("ANSIBLE ENVIRONMENT", "ANSIBLE_FOO: foo\n"),
mocker.call("MOLECULE ENVIRONMENT", "MOLECULE_BAR: bar\n"),
mocker.call("SHELL REPLAY", "ANSIBLE_FOO=foo MOLECULE_BAR=bar"),
]

Expand Down
34 changes: 7 additions & 27 deletions lib/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from subprocess import CompletedProcess
from typing import Any, Dict, List, Optional, Union

import colorama
import jinja2
import yaml
from rich.syntax import Syntax
Expand All @@ -53,25 +52,7 @@ def increase_indent(self, flow=False, indentless=False):

def print_debug(title: str, data: str) -> None:
"""Print debug information."""
title = "DEBUG: {}".format(title)
title_list = [
colorama.Back.WHITE,
colorama.Style.BRIGHT,
colorama.Fore.BLACK,
title,
colorama.Fore.RESET,
colorama.Back.RESET,
colorama.Style.RESET_ALL,
]
print("".join(title_list))
data_list = [
colorama.Fore.BLACK,
colorama.Style.BRIGHT,
data,
colorama.Style.RESET_ALL,
colorama.Fore.RESET,
]
print("".join(data_list))
console.print(f"DEBUG: {title}:\n{data}")


def print_environment_vars(env: Optional[Dict[str, str]]) -> None:
Expand All @@ -84,10 +65,12 @@ def print_environment_vars(env: Optional[Dict[str, str]]) -> None:
"""
if env:
ansible_env = {k: v for (k, v) in env.items() if "ANSIBLE_" in k}
print_debug("ANSIBLE ENVIRONMENT", safe_dump(ansible_env))
print_debug("ANSIBLE ENVIRONMENT", safe_dump(ansible_env, explicit_start=False))

molecule_env = {k: v for (k, v) in env.items() if "MOLECULE_" in k}
print_debug("MOLECULE ENVIRONMENT", safe_dump(molecule_env))
print_debug(
"MOLECULE ENVIRONMENT", safe_dump(molecule_env, explicit_start=False)
)

combined_env = ansible_env.copy()
combined_env.update(molecule_env)
Expand Down Expand Up @@ -203,18 +186,15 @@ def file_prepender(filename):
f.write(molecule_prepender(content))


def safe_dump(data: Any) -> str:
def safe_dump(data: Any, explicit_start=True) -> str:
"""
Dump the provided data to a YAML document and returns a string.
:param data: A string containing an absolute path to the file to parse.
:return: str
"""
# TODO(retr0h): Do we need to encode?
# yaml.dump(data) produces the document as a str object in both python
# 2 and 3.
return yaml.dump(
data, Dumper=SafeDumper, default_flow_style=False, explicit_start=True
data, Dumper=SafeDumper, default_flow_style=False, explicit_start=explicit_start
)


Expand Down

0 comments on commit f4afff7

Please sign in to comment.