From d8b4101eae8d7b650c9fd27d4f6495a032187279 Mon Sep 17 00:00:00 2001 From: bright Date: Sat, 18 May 2024 07:38:46 +0000 Subject: [PATCH] color: fix have_fg not re assign to true MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In method _parse the value of this variable 'have_fg ' is always False, Maybe reassign it to True is lost. I guess the author’s original intention was: if set some value in gitconfig file(for ex: text = black red ul), the first is bg color, the second is fg color, and the last one is attr. Change-Id: I372698fe625db4c1fdaa94ea7f193a80a850ecb9 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/425997 Reviewed-by: Mike Frysinger Tested-by: Bright Ma Commit-Queue: Josip Sokcevic --- color.py | 1 + tests/fixtures/test.gitconfig | 8 ++++ tests/test_color.py | 74 +++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tests/test_color.py diff --git a/color.py b/color.py index 77517156..9d0ce7c3 100644 --- a/color.py +++ b/color.py @@ -210,6 +210,7 @@ class Coloring: if have_fg: bg = a else: + have_fg = True fg = a elif is_attr(a): attr = a diff --git a/tests/fixtures/test.gitconfig b/tests/fixtures/test.gitconfig index 9b3f2574..4c7111d4 100644 --- a/tests/fixtures/test.gitconfig +++ b/tests/fixtures/test.gitconfig @@ -11,3 +11,11 @@ intk = 10k intm = 10m intg = 10g + +[color "status"] + one = yellow + two = magenta cyan + three = black red ul + reset = reset + none + empty = diff --git a/tests/test_color.py b/tests/test_color.py new file mode 100644 index 00000000..91a1bf25 --- /dev/null +++ b/tests/test_color.py @@ -0,0 +1,74 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Unittests for the color.py module.""" + +import os +import unittest + +import color +import git_config + + +def fixture(*paths): + """Return a path relative to test/fixtures.""" + return os.path.join(os.path.dirname(__file__), "fixtures", *paths) + + +class ColoringTests(unittest.TestCase): + """tests of the Coloring class.""" + + def setUp(self): + """Create a GitConfig object using the test.gitconfig fixture.""" + config_fixture = fixture("test.gitconfig") + self.config = git_config.GitConfig(config_fixture) + color.SetDefaultColoring("true") + self.color = color.Coloring(self.config, "status") + + def test_Color_Parse_all_params_none(self): + """all params are None""" + val = self.color._parse(None, None, None, None) + self.assertEqual("", val) + + def test_Color_Parse_first_parameter_none(self): + """check fg & bg & attr""" + val = self.color._parse(None, "black", "red", "ul") + self.assertEqual("\x1b[4;30;41m", val) + + def test_Color_Parse_one_entry(self): + """check fg""" + val = self.color._parse("one", None, None, None) + self.assertEqual("\033[33m", val) + + def test_Color_Parse_two_entry(self): + """check fg & bg""" + val = self.color._parse("two", None, None, None) + self.assertEqual("\033[35;46m", val) + + def test_Color_Parse_three_entry(self): + """check fg & bg & attr""" + val = self.color._parse("three", None, None, None) + self.assertEqual("\033[4;30;41m", val) + + def test_Color_Parse_reset_entry(self): + """check reset entry""" + val = self.color._parse("reset", None, None, None) + self.assertEqual("\033[m", val) + + def test_Color_Parse_empty_entry(self): + """check empty entry""" + val = self.color._parse("none", "blue", "white", "dim") + self.assertEqual("\033[2;34;47m", val) + val = self.color._parse("empty", "green", "white", "bold") + self.assertEqual("\033[1;32;47m", val)