Skip to content

Commit c4f3b5d

Browse files
authored
Merge pull request #24 from rezib/pr/action-query
Controller: introduce action_query()
2 parents 50d59b7 + 1d57454 commit c4f3b5d

File tree

2 files changed

+111
-61
lines changed

2 files changed

+111
-61
lines changed

lib/rift/Controller.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,58 @@ def action_sync(args, config):
10351035
)
10361036
synchronizer.run()
10371037

1038+
def action_query(args, config):
1039+
"""Action for 'query' command."""
1040+
staff, modules = staff_modules(config)
1041+
pkglist = sorted(Package.list(config, staff, modules, args.packages),
1042+
key=attrgetter('name'))
1043+
1044+
tbl = TextTable()
1045+
tbl.fmt = args.fmt or '%name %module %maintainers %version %release '\
1046+
'%modulemanager'
1047+
tbl.show_header = args.headers
1048+
tbl.color = True
1049+
1050+
supported_keys = set(('name', 'module', 'origin', 'reason', 'tests',
1051+
'version', 'arch', 'release', 'changelogname',
1052+
'changelogtime', 'maintainers', 'modulemanager',
1053+
'buildrequires'))
1054+
diff_keys = set(tbl.pattern_fields()) - supported_keys
1055+
if diff_keys:
1056+
raise RiftError(f"Unknown placeholder(s): {', '.join(diff_keys)} "
1057+
f"(supported keys are: {', '.join(supported_keys)})")
1058+
1059+
for pkg in pkglist:
1060+
logging.debug('Loading package %s', pkg.name)
1061+
try:
1062+
pkg.load()
1063+
spec = Spec(config=config)
1064+
if args.spec:
1065+
spec.filepath = pkg.specfile
1066+
spec.load()
1067+
except RiftError as exp:
1068+
logging.error("%s: %s", pkg.name, str(exp))
1069+
continue
1070+
1071+
date = str(time.strftime("%Y-%m-%d", time.localtime(spec.changelog_time)))
1072+
modulemanager = staff.get(modules.get(pkg.module).get('manager')[0])
1073+
tbl.append({'name': pkg.name,
1074+
'module': pkg.module,
1075+
'origin': pkg.origin,
1076+
'reason': pkg.reason,
1077+
'tests': str(len(list(pkg.tests()))),
1078+
'version': spec.version,
1079+
'arch': spec.arch,
1080+
'release': spec.release,
1081+
'changelogname': spec.changelog_name,
1082+
'changelogtime': date,
1083+
'buildrequires': spec.buildrequires,
1084+
'modulemanager': modulemanager['email'],
1085+
'maintainers': ', '.join(pkg.maintainers)})
1086+
print(tbl)
1087+
1088+
return 0
1089+
10381090
def create_staging_repo(config):
10391091
"""
10401092
Create and return staging temporary repository with a 2-tuple containing
@@ -1149,55 +1201,9 @@ def action(config, args):
11491201
elif args.command == 'validdiff':
11501202
return action_validdiff(args, config)
11511203

1204+
# QUERY
11521205
elif args.command == 'query':
1153-
1154-
staff, modules = staff_modules(config)
1155-
pkglist = sorted(Package.list(config, staff, modules, args.packages),
1156-
key=attrgetter('name'))
1157-
1158-
tbl = TextTable()
1159-
tbl.fmt = args.fmt or '%name %module %maintainers %version %release '\
1160-
'%modulemanager'
1161-
tbl.show_header = args.headers
1162-
tbl.color = True
1163-
1164-
supported_keys = set(('name', 'module', 'origin', 'reason', 'tests',
1165-
'version', 'arch', 'release', 'changelogname',
1166-
'changelogtime', 'maintainers', 'modulemanager',
1167-
'buildrequires'))
1168-
diff_keys = set(tbl.pattern_fields()) - supported_keys
1169-
if diff_keys:
1170-
raise RiftError(f"Unknown placeholder(s): {', '.join(diff_keys)} "
1171-
f"(supported keys are: {', '.join(supported_keys)})")
1172-
1173-
for pkg in pkglist:
1174-
logging.debug('Loading package %s', pkg.name)
1175-
try:
1176-
pkg.load()
1177-
spec = Spec(config=config)
1178-
if args.spec:
1179-
spec.filepath = pkg.specfile
1180-
spec.load()
1181-
except RiftError as exp:
1182-
logging.error("%s: %s", pkg.name, str(exp))
1183-
continue
1184-
1185-
date = str(time.strftime("%Y-%m-%d", time.localtime(spec.changelog_time)))
1186-
modulemanager = staff.get(modules.get(pkg.module).get('manager')[0])
1187-
tbl.append({'name': pkg.name,
1188-
'module': pkg.module,
1189-
'origin': pkg.origin,
1190-
'reason': pkg.reason,
1191-
'tests': str(len(list(pkg.tests()))),
1192-
'version': spec.version,
1193-
'arch': spec.arch,
1194-
'release': spec.release,
1195-
'changelogname': spec.changelog_name,
1196-
'changelogtime': date,
1197-
'buildrequires': spec.buildrequires,
1198-
'modulemanager': modulemanager['email'],
1199-
'maintainers': ', '.join(pkg.maintainers)})
1200-
print(tbl)
1206+
return action_query(args, config)
12011207

12021208
elif args.command == 'changelog':
12031209

tests/Controller.py

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from unittest.mock import patch, Mock
99
import subprocess
1010
from io import StringIO
11+
import textwrap
1112

1213
from TestUtils import (
1314
make_temp_dir, RiftTestCase, RiftProjectTestCase
@@ -44,22 +45,11 @@ def test_main_version(self):
4445
self.assert_except(SystemExit, "0", main, ['--version'])
4546

4647

47-
class ControllerProjectTest(RiftProjectTestCase):
48+
class ControllerProjectActionQueryTest(RiftProjectTestCase):
4849
"""
49-
Tests class for Controller
50+
Tests class for Controller action query
5051
"""
5152

52-
def _check_qemuuserstatic(self):
53-
"""Skip the test if none qemu-$arch-static executable is found for all
54-
architectures declared in project configuration."""
55-
if not any(
56-
[
57-
os.path.exists(f"/usr/bin/qemu-{arch}-static")
58-
for arch in self.config.get('arch')
59-
]
60-
):
61-
self.skipTest("qemu-user-static is not available")
62-
6353
def test_action_query(self):
6454
"""simple 'rift query' is ok """
6555
self.assertEqual(main(['query']), 0)
@@ -77,6 +67,60 @@ def test_action_query_on_bad_pkg(self):
7767
self.make_pkg(name='pkg2', metadata={})
7868
self.assertEqual(main(['query']), 0)
7969

70+
@patch('sys.stdout', new_callable=StringIO)
71+
def test_action_query_output_default(self, mock_stdout):
72+
self.make_pkg(name="pkg1")
73+
self.make_pkg(name="pkg2", version='2.1', release='3')
74+
self.assertEqual(main(['query']), 0)
75+
self.assertIn(
76+
"NAME MODULE MAINTAINERS VERSION RELEASE MODULEMANAGER",
77+
mock_stdout.getvalue())
78+
self.assertIn(textwrap.dedent("""
79+
---- ------ ----------- ------- ------- -------------
80+
pkg1 Great module Myself 1.0 1 buddy@somewhere.org
81+
pkg2 Great module Myself 2.1 3 buddy@somewhere.org
82+
"""),
83+
mock_stdout.getvalue())
84+
85+
@patch('sys.stdout', new_callable=StringIO)
86+
def test_action_query_output_format(self, mock_stdout):
87+
self.make_pkg(name="pkg1")
88+
self.make_pkg(name="pkg2", version='2.1', release='3')
89+
self.assertEqual(
90+
main([
91+
'query', '--format',
92+
'%name %module %origin %reason %tests %version %arch %release '
93+
'%changelogname %changelogtime %maintainers %modulemanager '
94+
'%buildrequires']), 0)
95+
self.assertIn(
96+
"NAME MODULE ORIGIN REASON TESTS VERSION ARCH "
97+
"RELEASE CHANGELOGNAME CHANGELOGTIME "
98+
"MAINTAINERS MODULEMANAGER BUILDREQUIRES",
99+
mock_stdout.getvalue())
100+
self.assertIn(textwrap.dedent("""
101+
---- ------ ------ ------ ----- ------- ---- ------- ------------- ------------- ----------- ------------- -------------
102+
pkg1 Great module Vendor Missing feature 0 1.0 noarch 1 Myself <buddy@somewhere.org> 1.0-1 2019-02-26 Myself buddy@somewhere.org br-package
103+
pkg2 Great module Vendor Missing feature 0 2.1 noarch 3 Myself <buddy@somewhere.org> 2.1-3 2019-02-26 Myself buddy@somewhere.org br-package
104+
"""),
105+
mock_stdout.getvalue())
106+
107+
108+
class ControllerProjectTest(RiftProjectTestCase):
109+
"""
110+
Tests class for Controller
111+
"""
112+
113+
def _check_qemuuserstatic(self):
114+
"""Skip the test if none qemu-$arch-static executable is found for all
115+
architectures declared in project configuration."""
116+
if not any(
117+
[
118+
os.path.exists(f"/usr/bin/qemu-{arch}-static")
119+
for arch in self.config.get('arch')
120+
]
121+
):
122+
self.skipTest("qemu-user-static is not available")
123+
80124
@patch('rift.Controller.remove_packages')
81125
@patch('rift.Controller.validate_pkgs')
82126
@patch('rift.Controller.get_packages_from_patch')

0 commit comments

Comments
 (0)