Skip to content

Commit

Permalink
Remove _KNACK_TEST_FORCE_ENABLE_COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli committed Mar 12, 2021
1 parent 4c51d0e commit e5e0529
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
8 changes: 2 additions & 6 deletions knack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

logger = get_logger(__name__)

# Temporarily force color to be enabled even when out_file is not stdout.
# This is only intended for testing purpose.
_KNACK_TEST_FORCE_ENABLE_COLOR = False


class CLI(object): # pylint: disable=too-many-instance-attributes
""" The main driver for the CLI """
Expand Down Expand Up @@ -209,7 +205,7 @@ def invoke(self, args, initial_invocation_data=None, out_file=None):
exit_code = 0
try:
out_file = out_file or self.out_file
if out_file is sys.stdout and self._should_init_colorama or _KNACK_TEST_FORCE_ENABLE_COLOR:
if out_file is sys.stdout and self._should_init_colorama:
self.init_debug_log.append("Init colorama.")
import colorama
colorama.init()
Expand Down Expand Up @@ -253,7 +249,7 @@ def invoke(self, args, initial_invocation_data=None, out_file=None):
finally:
self.raise_event(EVENT_CLI_POST_EXECUTE)

if self._should_init_colorama or _KNACK_TEST_FORCE_ENABLE_COLOR:
if self._should_init_colorama:
import colorama
colorama.deinit()

Expand Down
19 changes: 9 additions & 10 deletions tests/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import mock
except ImportError:
from unittest import mock
from threading import Lock

from knack.arguments import ArgumentsContext
from knack.commands import CLICommand, CLICommandsLoader, CommandGroup
from knack.commands import CLICommandsLoader, CommandGroup

from tests.util import DummyCLI, redirect_io, disable_color
from tests.util import DummyCLI, redirect_io, assert_in_multi_line, disable_color


def example_handler(arg1, arg2=None, arg3=None):
Expand Down Expand Up @@ -80,7 +79,7 @@ def test_deprecate_command_group_help(self):
cmd4 [Deprecated] : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_deprecate_command_help_hidden(self):
Expand All @@ -100,7 +99,7 @@ def test_deprecate_command_help_hidden(self):
--arg -a : Allowed values: 1, 2, 3.
--arg3
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_deprecate_command_plain_execute(self):
Expand Down Expand Up @@ -211,7 +210,7 @@ def test_deprecate_command_group_help_plain(self):
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_deprecate_command_group_help_hidden(self):
Expand All @@ -229,7 +228,7 @@ def test_deprecate_command_group_help_hidden(self):
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_deprecate_command_group_help_expiring(self):
Expand All @@ -243,7 +242,7 @@ def test_deprecate_command_group_help_expiring(self):
This command group has been deprecated and will be removed in version '1.0.0'. Use
'alt-group4' instead.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
@disable_color
Expand Down Expand Up @@ -282,7 +281,7 @@ def test_deprecate_command_implicitly(self):
This command is implicitly deprecated because command group 'group1' is deprecated and
will be removed in a future release. Use 'alt-group1' instead.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)


class TestArgumentDeprecation(unittest.TestCase):
Expand Down Expand Up @@ -352,7 +351,7 @@ def test_deprecate_arguments_command_help(self):
--opt4
--opt5
""".format(self.cli_ctx.name)
self.assertTrue(actual.startswith(expected))
assert_in_multi_line(expected, actual)

@redirect_io
def test_deprecate_arguments_execute(self):
Expand Down
14 changes: 7 additions & 7 deletions tests/test_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from knack.arguments import ArgumentsContext
from knack.commands import CLICommandsLoader, CommandGroup

from tests.util import DummyCLI, redirect_io, remove_space
from tests.util import DummyCLI, redirect_io, assert_in_multi_line


def example_handler(arg1, arg2=None, arg3=None):
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_experimental_command_implicitly_execute(self):
self.cli_ctx.invoke('grp1 cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "Command group 'grp1' is experimental and under development."
self.assertIn(remove_space(expected), remove_space(actual))
self.assertIn(expected, actual)

@redirect_io
def test_experimental_command_group_help(self):
Expand All @@ -83,15 +83,15 @@ def test_experimental_command_group_help(self):
cmd1 [Experimental] : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_experimental_command_plain_execute(self):
""" Ensure general warning displayed when running experimental command. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "This command is experimental and under development."
self.assertIn(remove_space(expected), remove_space(actual))
self.assertIn(expected, actual)


class TestCommandGroupExperimental(unittest.TestCase):
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_experimental_command_group_help_plain(self):
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertIn(remove_space(expected), remove_space(actual))
assert_in_multi_line(expected, actual)

@redirect_io
def test_experimental_command_implicitly(self):
Expand All @@ -150,7 +150,7 @@ def test_experimental_command_implicitly(self):
Long summary here. Still long summary.
Command group 'group1' is experimental and under development.
""".format(self.cli_ctx.name)
self.assertIn(remove_space(expected), remove_space(actual))
assert_in_multi_line(expected, actual)


class TestArgumentExperimental(unittest.TestCase):
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_experimental_arguments_command_help(self):
--arg1 [Experimental] [Required] : Arg1.
Argument '--arg1' is experimental and under development.
""".format(self.cli_ctx.name)
self.assertIn(remove_space(expected), remove_space(actual))
assert_in_multi_line(expected, actual)

@redirect_io
def test_experimental_arguments_execute(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from knack.arguments import ArgumentsContext
from knack.commands import CLICommandsLoader, CommandGroup

from tests.util import DummyCLI, redirect_io, disable_color
from tests.util import DummyCLI, redirect_io, assert_in_multi_line, disable_color


def example_handler(arg1, arg2=None, arg3=None):
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_preview_command_group_help(self):
cmd1 [Preview] : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
def test_preview_command_plain_execute(self):
Expand Down Expand Up @@ -170,7 +170,7 @@ def test_preview_command_group_help_plain(self):
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
@disable_color
Expand Down Expand Up @@ -202,7 +202,7 @@ def test_preview_command_implicitly(self):
Command group 'group1' is in preview. It may be changed/removed in a future
release.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)


class TestArgumentPreview(unittest.TestCase):
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_preview_arguments_command_help(self):
--arg1 [Preview] [Required] : Arg1.
Argument '--arg1' is in preview. It may be changed/removed in a future release.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
assert_in_multi_line(expected, actual)

@redirect_io
@disable_color
Expand Down
27 changes: 18 additions & 9 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import mock
except ImportError:
from unittest import mock
import logging
import os
import re
import shutil
import sys
import tempfile
import shutil
import os
from io import StringIO
import logging
from knack.log import CLI_LOGGER_NAME

from knack.cli import CLI, CLICommandsLoader, CommandInvoker
from knack.log import CLI_LOGGER_NAME

TEMP_FOLDER_NAME = "knack_temp"

Expand All @@ -33,8 +34,7 @@ def wrapper(self):
cli_logger.handlers.clear()

sys.stdout = sys.stderr = self.io = StringIO()
with mock.patch("knack.cli._KNACK_TEST_FORCE_ENABLE_COLOR", True):
func(self)
func(self)
self.io.close()
sys.stdout = original_stdout
sys.stderr = original_stderr
Expand All @@ -54,8 +54,17 @@ def wrapper(self):
return wrapper


def remove_space(str):
return str.replace(' ', '').replace('\n', '')
def _remove_control_sequence(string):
return re.sub(r'\x1b[^m]+m', '', string)


def _remove_whitespace(string):
return re.sub(r'\s', '', string)


def assert_in_multi_line(sub_string, string):
# assert sub_string is in string, with all whitespaces, line breaks and control sequences ignored
assert _remove_whitespace(sub_string) in _remove_control_sequence(_remove_whitespace(string))


class MockContext(CLI):
Expand All @@ -77,7 +86,7 @@ def get_cli_version(self):
def __init__(self, **kwargs):
kwargs['config_dir'] = new_temp_folder()
super().__init__(**kwargs)
# Force colorama to initialize
# Force to enable color
self.enable_color = True


Expand Down

0 comments on commit e5e0529

Please sign in to comment.