1+ import textwrap
12from functools import partial
2- from typing import NamedTuple , List
3+ from typing import List , NamedTuple
34from unittest .mock import patch
5+
46import pytest
7+ from snakeoil .contexts import chdir , os_environ
58
6- from snakeoil .contexts import chdir
79from pkgdev .scripts import run
810
11+
912class 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
1821class 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+
3561class 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