Skip to content

Commit 9714e47

Browse files
committed
cli: add support for NOCOLOR to disable colors
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent 96ab6d8 commit 9714e47

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

src/pkgdev/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,8 @@ def parse_known_args(self, args=None, namespace=None):
142142
except (repo_errors.InitializationError, IOError) as exc:
143143
self.error(str(exc))
144144

145+
if os.getenv('NOCOLOR'):
146+
namespace.color = False
147+
145148
# parse command line args to override config defaults
146149
return super().parse_known_args(args, namespace)

tests/scripts/test_pkgdev_showkw.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import textwrap
12
from functools import partial
2-
from typing import NamedTuple, List
3+
from typing import List, NamedTuple
34
from unittest.mock import patch
5+
46
import pytest
7+
from snakeoil.contexts import chdir, os_environ
58

6-
from snakeoil.contexts import chdir
79
from pkgdev.scripts import run
810

11+
912
class Profile(NamedTuple):
1013
"""Profile record used to create profiles in a repository."""
1114
path: str
@@ -16,26 +19,49 @@ class Profile(NamedTuple):
1619
eapi: str = '5'
1720

1821
class TestPkgdevShowkwParseArgs:
22+
args = ('showkw', '--config', 'no')
1923

2024
def test_missing_target(self, capsys, tool):
2125
with pytest.raises(SystemExit):
22-
tool.parse_args(['showkw', '--config', 'no'])
26+
tool.parse_args(self.args)
2327
captured = capsys.readouterr()
2428
assert captured.err.strip() == (
2529
'pkgdev showkw: error: missing target argument and not in a supported repo')
2630

2731
def test_unknown_arches(self, capsys, tool, make_repo):
2832
repo = make_repo(arches=['amd64'])
2933
with pytest.raises(SystemExit):
30-
tool.parse_args(['showkw', '--config', 'no', '-a', 'unknown', '-r', repo.location])
34+
tool.parse_args([*self.args, '-a', 'unknown', '-r', repo.location])
3135
captured = capsys.readouterr()
3236
assert captured.err.strip() == (
3337
"pkgdev showkw: error: unknown arch: 'unknown' (choices: amd64)")
3438

39+
def test_no_color(self, tool, make_repo, tmp_path):
40+
repo = make_repo(arches=['amd64'])
41+
repo.create_ebuild('foo/bar-0', keywords=('x86'))
42+
43+
(config_file := tmp_path / 'pkgcheck.conf').write_text(textwrap.dedent('''\
44+
[DEFAULT]
45+
showkw.color = true
46+
'''))
47+
48+
def parse(*args):
49+
options, _ = tool.parse_args(['showkw', '-r', repo.location, 'foo/bar',
50+
'--config', str(config_file), *args])
51+
return options
52+
53+
assert parse().color is True
54+
with os_environ(NOCOLOR='1'):
55+
# NOCOLOR overrides config file
56+
assert parse().color is False
57+
# cmd line option overrides NOCOLOR
58+
assert parse('--color', 'n').color is False
59+
assert parse('--color', 'y').color is True
60+
3561
class TestPkgdevShowkw:
3662

3763
script = staticmethod(partial(run, 'pkgdev'))
38-
base_args = ['pkgdev', 'showkw', '--config', 'n']
64+
base_args = ('pkgdev', 'showkw', '--config', 'n', '--color', 'n')
3965

4066
def _create_repo(self, make_repo):
4167
repo = make_repo(arches=['amd64', 'ia64', 'mips', 'x86'])
@@ -51,7 +77,7 @@ def _run_and_parse(self, capsys, *args):
5177
with patch('sys.argv', [*self.base_args, "--format", "presto", *args]), \
5278
pytest.raises(SystemExit) as excinfo:
5379
self.script()
54-
assert excinfo.value.code == None
80+
assert excinfo.value.code is None
5581
out, err = capsys.readouterr()
5682
assert not err
5783
lines = out.split('\n')
@@ -67,7 +93,7 @@ def test_match(self, capsys, make_repo):
6793
with patch('sys.argv', [*self.base_args, '-r', repo.location, 'foo/bar']), \
6894
pytest.raises(SystemExit) as excinfo:
6995
self.script()
70-
assert excinfo.value.code == None
96+
assert excinfo.value.code is None
7197
out, err = capsys.readouterr()
7298
assert not err
7399
assert out.split('\n')[0] == "keywords for foo/bar:"
@@ -78,7 +104,7 @@ def test_match_short_name(self, capsys, make_repo):
78104
with patch('sys.argv', [*self.base_args, '-r', repo.location, 'bar']), \
79105
pytest.raises(SystemExit) as excinfo:
80106
self.script()
81-
assert excinfo.value.code == None
107+
assert excinfo.value.code is None
82108
out, err = capsys.readouterr()
83109
assert not err
84110
assert out.split('\n')[0] == "keywords for foo/bar:"
@@ -90,7 +116,7 @@ def test_match_cwd_repo(self, capsys, make_repo):
90116
pytest.raises(SystemExit) as excinfo, \
91117
chdir(repo.location):
92118
self.script()
93-
assert excinfo.value.code == None
119+
assert excinfo.value.code is None
94120
out, err = capsys.readouterr()
95121
assert not err
96122
assert out.split('\n')[0] == "keywords for foo/bar:"
@@ -102,7 +128,7 @@ def test_match_cwd_pkg(self, capsys, make_repo):
102128
pytest.raises(SystemExit) as excinfo, \
103129
chdir(repo.location + '/foo/bar'):
104130
self.script()
105-
assert excinfo.value.code == None
131+
assert excinfo.value.code is None
106132
_, err = capsys.readouterr()
107133
assert not err
108134

@@ -155,13 +181,11 @@ def test_correct_keywords_status(self, capsys, make_repo):
155181
assert dict(amd64='~', ia64='o', mips='-', x86='~', slot='0').items() <= res['1'].items()
156182
assert dict(amd64='+', ia64='*', mips='*', x86='-', slot='2', eapi='8').items() <= res['2'].items()
157183

158-
@pytest.mark.parametrize(('arg', 'expected'),
159-
(
160-
('--stable', {'amd64', 'x86'}),
161-
('--unstable', {'amd64', 'ia64', 'mips', 'x86'}),
162-
('--only-unstable', {'ia64', 'mips'}),
163-
)
164-
)
184+
@pytest.mark.parametrize(('arg', 'expected'), (
185+
pytest.param('--stable', {'amd64', 'x86'}, id='stable'),
186+
pytest.param('--unstable', {'amd64', 'ia64', 'mips', 'x86'}, id='unstable'),
187+
pytest.param('--only-unstable', {'ia64', 'mips'}, id='only-unstable'),
188+
))
165189
def test_collapse(self, capsys, make_repo, arg, expected):
166190
repo = self._create_repo(make_repo)
167191
repo.create_ebuild('foo/bar-0', keywords=('amd64', '~ia64', '~mips', '~x86'))
@@ -170,7 +194,7 @@ def test_collapse(self, capsys, make_repo, arg, expected):
170194
pytest.raises(SystemExit) as excinfo:
171195
self.script()
172196
out, err = capsys.readouterr()
173-
assert excinfo.value.code == None
197+
assert excinfo.value.code is None
174198
assert not err
175199
arches = set(out.split('\n')[0].split())
176200
assert arches == expected

0 commit comments

Comments
 (0)