1+ from functools import partial
2+ from typing import NamedTuple , List
3+ from unittest .mock import patch
14import pytest
25
6+ from pkgdev .scripts import run
7+ from snakeoil .contexts import chdir , os_environ
8+
9+ class Profile (NamedTuple ):
10+ """Profile record used to create profiles in a repository."""
11+ path : str
12+ arch : str
13+ status : str = 'stable'
14+ deprecated : bool = False
15+ defaults : List [str ] = None
16+ eapi : str = '5'
317
418class TestPkgdevShowkwParseArgs :
519
@@ -17,3 +31,145 @@ def test_unknown_arches(self, capsys, tool, make_repo):
1731 captured = capsys .readouterr ()
1832 assert captured .err .strip () == (
1933 "pkgdev showkw: error: unknown arch: 'unknown' (choices: amd64)" )
34+
35+ class TestPkgdevShowkw :
36+
37+ script = partial (run , 'pkgdev' )
38+
39+ def _create_repo (self , make_repo ):
40+ repo = make_repo (arches = ['amd64' , 'ia64' , 'mips' , 'x86' ])
41+ repo .create_profiles ([
42+ Profile ('default/linux/amd64' , 'amd64' ),
43+ Profile ('default/linux/x86' , 'x86' ),
44+ Profile ('default/linux/ia64' , 'ia64' , 'dev' ),
45+ Profile ('default/linux/mips' , 'mips' , 'exp' ),
46+ ])
47+ return repo
48+
49+ def _run_and_parse (self , capsys , * args ):
50+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , "--format" , "presto" , * args ]), \
51+ pytest .raises (SystemExit ) as excinfo :
52+ self .script ()
53+ assert excinfo .value .code == None
54+ out , err = capsys .readouterr ()
55+ assert not err
56+ lines = out .split ('\n ' )
57+ table_columns = [s .strip () for s in lines [1 ].split ('|' )][1 :]
58+ return {
59+ ver : dict (zip (table_columns , values ))
60+ for ver , * values in map (lambda s : map (str .strip , s .split ('|' )), lines [3 :- 1 ])
61+ }
62+
63+ def test_match (self , capsys , make_repo ):
64+ repo = self ._create_repo (make_repo )
65+ repo .create_ebuild ('foo/bar-0' )
66+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , '-r' , repo .location , 'foo/bar' ]), \
67+ pytest .raises (SystemExit ) as excinfo :
68+ self .script ()
69+ assert excinfo .value .code == None
70+ out , err = capsys .readouterr ()
71+ assert not err
72+ assert out .split ('\n ' )[0 ] == "keywords for foo/bar:"
73+
74+ def test_match_short_name (self , capsys , make_repo ):
75+ repo = self ._create_repo (make_repo )
76+ repo .create_ebuild ('foo/bar-0' )
77+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , '-r' , repo .location , 'bar' ]), \
78+ pytest .raises (SystemExit ) as excinfo :
79+ self .script ()
80+ assert excinfo .value .code == None
81+ out , err = capsys .readouterr ()
82+ assert not err
83+ assert out .split ('\n ' )[0 ] == "keywords for foo/bar:"
84+
85+ def test_match_cwd_repo (self , capsys , make_repo ):
86+ repo = self ._create_repo (make_repo )
87+ repo .create_ebuild ('foo/bar-0' )
88+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , 'foo/bar' ]), \
89+ pytest .raises (SystemExit ) as excinfo , \
90+ chdir (repo .location ):
91+ self .script ()
92+ assert excinfo .value .code == None
93+ out , err = capsys .readouterr ()
94+ assert not err
95+ assert out .split ('\n ' )[0 ] == "keywords for foo/bar:"
96+
97+ def test_match_cwd_pkg (self , capsys , make_repo ):
98+ repo = self ._create_repo (make_repo )
99+ repo .create_ebuild ('foo/bar-0' )
100+ with patch ('sys.argv' , ['pkgdev' , 'showkw' ]), \
101+ pytest .raises (SystemExit ) as excinfo , \
102+ chdir (repo .location + '/foo/bar' ):
103+ self .script ()
104+ assert excinfo .value .code == None
105+ _ , err = capsys .readouterr ()
106+ assert not err
107+
108+ def test_no_matches (self , capsys , make_repo ):
109+ repo = self ._create_repo (make_repo )
110+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , '-r' , repo .location , 'foo/bar' ]), \
111+ pytest .raises (SystemExit ) as excinfo :
112+ self .script ()
113+ assert excinfo .value .code == 1
114+ out , err = capsys .readouterr ()
115+ assert not out
116+ assert err .strip () == "pkgdev showkw: no matches for 'foo/bar'"
117+
118+ def test_match_stable (self , capsys , make_repo ):
119+ repo = self ._create_repo (make_repo )
120+ repo .create_ebuild ('foo/bar-0' , keywords = ('~amd64' , '~ia64' , '~mips' , 'x86' ))
121+ res = self ._run_and_parse (capsys , '-r' , repo .location , 'foo/bar' , '--stable' )
122+ assert set (res .keys ()) == {'0' }
123+ assert {'amd64' , 'ia64' , 'mips' , 'x86' } & res ['0' ].keys () == {'amd64' , 'x86' }
124+
125+ def test_match_unstable (self , capsys , make_repo ):
126+ repo = self ._create_repo (make_repo )
127+ repo .create_ebuild ('foo/bar-0' , keywords = ('~amd64' , '~ia64' , '~mips' , 'x86' ))
128+ res = self ._run_and_parse (capsys , '-r' , repo .location , 'foo/bar' , '--unstable' )
129+ assert set (res .keys ()) == {'0' }
130+ assert {'amd64' , 'ia64' , 'mips' , 'x86' } <= res ['0' ].keys ()
131+
132+ def test_match_specific_arch (self , capsys , make_repo ):
133+ repo = self ._create_repo (make_repo )
134+ repo .create_ebuild ('foo/bar-0' , keywords = ('~amd64' , '~ia64' , '~mips' , 'x86' ))
135+ res = self ._run_and_parse (capsys , '-r' , repo .location , 'foo/bar' , '--arch' , 'amd64' )
136+ assert set (res .keys ()) == {'0' }
137+ assert {'amd64' , 'ia64' , 'mips' , 'x86' } & res ['0' ].keys () == {'amd64' }
138+
139+ def test_match_specific_multiple_arch (self , capsys , make_repo ):
140+ repo = self ._create_repo (make_repo )
141+ repo .create_ebuild ('foo/bar-0' , keywords = ('~amd64' , '~ia64' , '~mips' , 'x86' ))
142+ res = self ._run_and_parse (capsys , '-r' , repo .location , 'foo/bar' , '--arch' , 'amd64,mips' )
143+ assert set (res .keys ()) == {'0' }
144+ assert {'amd64' , 'ia64' , 'mips' , 'x86' } & res ['0' ].keys () == {'amd64' , 'mips' }
145+
146+ def test_correct_keywords_status (self , capsys , make_repo ):
147+ repo = self ._create_repo (make_repo )
148+ repo .create_ebuild ('foo/bar-0' , keywords = ('amd64' , '~ia64' , '~mips' , 'x86' ))
149+ repo .create_ebuild ('foo/bar-1' , keywords = ('~amd64' , '-mips' , '~x86' ))
150+ repo .create_ebuild ('foo/bar-2' , keywords = ('-*' , 'amd64' , '-x86' ), eapi = 8 , slot = 2 )
151+ res = self ._run_and_parse (capsys , '-r' , repo .location , 'foo/bar' )
152+ assert set (res .keys ()) == {'0' , '1' , '2' }
153+ assert dict (amd64 = '+' , ia64 = '~' , mips = '~' , x86 = '+' , slot = '0' ).items () <= res ['0' ].items ()
154+ assert dict (amd64 = '~' , ia64 = 'o' , mips = '-' , x86 = '~' , slot = '0' ).items () <= res ['1' ].items ()
155+ assert dict (amd64 = '+' , ia64 = '*' , mips = '*' , x86 = '-' , slot = '2' , eapi = '8' ).items () <= res ['2' ].items ()
156+
157+ @pytest .mark .parametrize (('arg' , 'expected' ),
158+ (
159+ ('--stable' , {'amd64' , 'x86' }),
160+ ('--unstable' , {'amd64' , 'ia64' , 'mips' , 'x86' }),
161+ ('--only-unstable' , {'ia64' , 'mips' }),
162+ )
163+ )
164+ def test_collapse (self , capsys , make_repo , arg , expected ):
165+ repo = self ._create_repo (make_repo )
166+ repo .create_ebuild ('foo/bar-0' , keywords = ('amd64' , '~ia64' , '~mips' , '~x86' ))
167+ repo .create_ebuild ('foo/bar-1' , keywords = ('~amd64' , '~ia64' , '~mips' , 'x86' ))
168+ with patch ('sys.argv' , ['pkgdev' , 'showkw' , '-r' , repo .location , 'foo/bar' , "--collapse" , arg ]), \
169+ pytest .raises (SystemExit ) as excinfo :
170+ self .script ()
171+ out , err = capsys .readouterr ()
172+ assert excinfo .value .code == None
173+ assert not err
174+ arches = set (out .split ('\n ' )[0 ].split ())
175+ assert arches == expected
0 commit comments