예제 #1
0
    def test_format_styled_text_theme(self):
        from azure.cli.core.style import Style, format_styled_text
        styled_text = [
            (Style.PRIMARY, "White: Primary text color\n"),
            (Style.SECONDARY, "Bright Black: Secondary text color\n"),
        ]
        formatted = format_styled_text(styled_text)
        excepted = """\x1b[0mWhite: Primary text color
\x1b[90mBright Black: Secondary text color
\x1b[0m"""
        self.assertEqual(formatted, excepted)

        # Color is turned off via param
        formatted = format_styled_text(styled_text, theme='none')
        excepted_plaintext = ("White: Primary text color\n"
                              "Bright Black: Secondary text color\n")
        self.assertEqual(formatted, excepted_plaintext)

        # Color is turned off via function attribute
        format_styled_text.theme = 'none'
        formatted = format_styled_text(styled_text)
        self.assertEqual(formatted, excepted_plaintext)

        # Function attribute is overridden by param
        format_styled_text.theme = 'dark'
        formatted = format_styled_text(styled_text, theme='none')
        self.assertEqual(formatted, excepted_plaintext)

        # Specify theme with Enum
        formatted = format_styled_text(styled_text, theme=Theme.NONE)
        self.assertEqual(formatted, excepted_plaintext)

        delattr(format_styled_text, "theme")
예제 #2
0
    def test_format_styled_text_legacy_powershell(self,
                                                  is_modern_terminal_mock,
                                                  get_parent_proc_name_mock):
        """Verify bright/dark blue is replaced with the default color in legacy powershell.exe terminal."""
        styled_text = [(Style.ACTION,
                        "Blue: Commands, parameters, and system inputs")]

        # Try to delete _is_legacy_powershell cache
        try:
            delattr(format_styled_text, '_is_legacy_powershell')
        except AttributeError:
            pass

        # When theme is 'none', no need to call is_modern_terminal and get_parent_proc_name
        formatted = format_styled_text(styled_text, theme='none')
        is_modern_terminal_mock.assert_not_called()
        get_parent_proc_name_mock.assert_not_called()
        assert formatted == """Blue: Commands, parameters, and system inputs"""

        excepted = """\x1b[0mBlue: Commands, parameters, and system inputs\x1b[0m"""
        formatted = format_styled_text(styled_text, theme='dark')
        assert formatted == excepted

        formatted = format_styled_text(styled_text, theme='light')
        assert formatted == excepted
예제 #3
0
    def test_format_styled_text(self):
        # Test list input
        styled_text = [
            (Style.PRIMARY, "White: Primary text color\n"),
            (Style.SECONDARY, "Bright Black: Secondary text color\n"),
            (Style.IMPORTANT, "Bright Magenta: Important text color\n"),
            (Style.ACTION, "Bright Blue: Commands, parameters, and system inputs\n"),
            (Style.HYPERLINK, "Bright Cyan: Hyperlink\n"),
            (Style.ERROR, "Bright Red: Error message indicator\n"),
            (Style.SUCCESS, "Bright Green: Success message indicator\n"),
            (Style.WARNING, "Bright Yellow: Warning message indicator\n"),
        ]
        formatted = format_styled_text(styled_text)
        excepted = """\x1b[0mWhite: Primary text color
\x1b[90mBright Black: Secondary text color
\x1b[95mBright Magenta: Important text color
\x1b[94mBright Blue: Commands, parameters, and system inputs
\x1b[96mBright Cyan: Hyperlink
\x1b[91mBright Red: Error message indicator
\x1b[92mBright Green: Success message indicator
\x1b[93mBright Yellow: Warning message indicator
\x1b[0m"""
        self.assertEqual(formatted, excepted)

        # Test str input
        styled_text = "Primary text color"
        formatted = format_styled_text(styled_text)
        excepted = "\x1b[0mPrimary text color\x1b[0m"
        self.assertEqual(formatted, excepted)

        # Test tuple input
        styled_text = (Style.PRIMARY, "Primary text color")
        formatted = format_styled_text(styled_text)
        excepted = "\x1b[0mPrimary text color\x1b[0m"
        self.assertEqual(formatted, excepted)
예제 #4
0
    def test_format_styled_text_on_error(self):
        # Test invalid style
        from azure.cli.core.azclierror import CLIInternalError
        with self.assertRaisesRegex(CLIInternalError, "Invalid style."):
            format_styled_text([("invalid_style", "dummy text")])

        # Test invalid styled style
        with self.assertRaisesRegex(CLIInternalError, "Invalid styled text."):
            format_styled_text([(Style.PRIMARY, )])
        with self.assertRaisesRegex(CLIInternalError, "Invalid styled text."):
            format_styled_text(["dummy text"])
예제 #5
0
    def test_format_styled_text(self):
        from azure.cli.core.style import Style, format_styled_text
        styled_text = [
            (Style.PRIMARY, "Bright White: Primary text color\n"),
            (Style.SECONDARY, "White: Secondary text color\n"),
            (Style.IMPORTANT, "Bright Magenta: Important text color\n"),
            (Style.ACTION,
             "Bright Blue: Commands, parameters, and system inputs\n"),
            (Style.HYPERLINK, "Bright Cyan: Hyperlink\n"),
            (Style.ERROR, "Bright Red: Error message indicator\n"),
            (Style.SUCCESS, "Bright Green: Success message indicator\n"),
            (Style.WARNING, "Bright Yellow: Warning message indicator\n"),
        ]
        formatted = format_styled_text(styled_text)
        excepted = """\x1b[97mBright White: Primary text color
\x1b[90mWhite: Secondary text color
\x1b[95mBright Magenta: Important text color
\x1b[94mBright Blue: Commands, parameters, and system inputs
\x1b[96mBright Cyan: Hyperlink
\x1b[91mBright Red: Error message indicator
\x1b[92mBright Green: Success message indicator
\x1b[93mBright Yellow: Warning message indicator
\x1b[39m"""
        self.assertEqual(formatted, excepted)

        # Test invalid style
        from azure.cli.core.azclierror import CLIInternalError
        with self.assertRaisesRegex(CLIInternalError, "Invalid style."):
            format_styled_text([(
                "invalid_style",
                "dummy text",
            )])

        # Test invalid styled style
        with self.assertRaisesRegex(CLIInternalError, "Invalid styled text."):
            format_styled_text([(Style.PRIMARY, )])
        with self.assertRaisesRegex(CLIInternalError, "Invalid styled text."):
            format_styled_text(["dummy text"])