Skip to content

Commit d8b4101

Browse files
brightLUCI
authored andcommitted
color: fix have_fg not re assign to true
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 <[email protected]> Tested-by: Bright Ma <[email protected]> Commit-Queue: Josip Sokcevic <[email protected]>
1 parent 1c53b0f commit d8b4101

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

color.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def _parse(self, opt, fg, bg, attr):
210210
if have_fg:
211211
bg = a
212212
else:
213+
have_fg = True
213214
fg = a
214215
elif is_attr(a):
215216
attr = a

tests/fixtures/test.gitconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@
1111
intk = 10k
1212
intm = 10m
1313
intg = 10g
14+
15+
[color "status"]
16+
one = yellow
17+
two = magenta cyan
18+
three = black red ul
19+
reset = reset
20+
none
21+
empty =

tests/test_color.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright (C) 2024 The Android Open Source Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Unittests for the color.py module."""
16+
17+
import os
18+
import unittest
19+
20+
import color
21+
import git_config
22+
23+
24+
def fixture(*paths):
25+
"""Return a path relative to test/fixtures."""
26+
return os.path.join(os.path.dirname(__file__), "fixtures", *paths)
27+
28+
29+
class ColoringTests(unittest.TestCase):
30+
"""tests of the Coloring class."""
31+
32+
def setUp(self):
33+
"""Create a GitConfig object using the test.gitconfig fixture."""
34+
config_fixture = fixture("test.gitconfig")
35+
self.config = git_config.GitConfig(config_fixture)
36+
color.SetDefaultColoring("true")
37+
self.color = color.Coloring(self.config, "status")
38+
39+
def test_Color_Parse_all_params_none(self):
40+
"""all params are None"""
41+
val = self.color._parse(None, None, None, None)
42+
self.assertEqual("", val)
43+
44+
def test_Color_Parse_first_parameter_none(self):
45+
"""check fg & bg & attr"""
46+
val = self.color._parse(None, "black", "red", "ul")
47+
self.assertEqual("\x1b[4;30;41m", val)
48+
49+
def test_Color_Parse_one_entry(self):
50+
"""check fg"""
51+
val = self.color._parse("one", None, None, None)
52+
self.assertEqual("\033[33m", val)
53+
54+
def test_Color_Parse_two_entry(self):
55+
"""check fg & bg"""
56+
val = self.color._parse("two", None, None, None)
57+
self.assertEqual("\033[35;46m", val)
58+
59+
def test_Color_Parse_three_entry(self):
60+
"""check fg & bg & attr"""
61+
val = self.color._parse("three", None, None, None)
62+
self.assertEqual("\033[4;30;41m", val)
63+
64+
def test_Color_Parse_reset_entry(self):
65+
"""check reset entry"""
66+
val = self.color._parse("reset", None, None, None)
67+
self.assertEqual("\033[m", val)
68+
69+
def test_Color_Parse_empty_entry(self):
70+
"""check empty entry"""
71+
val = self.color._parse("none", "blue", "white", "dim")
72+
self.assertEqual("\033[2;34;47m", val)
73+
val = self.color._parse("empty", "green", "white", "bold")
74+
self.assertEqual("\033[1;32;47m", val)

0 commit comments

Comments
 (0)