diff --git a/lib/rift/Controller.py b/lib/rift/Controller.py index 0268427..0d2b74b 100644 --- a/lib/rift/Controller.py +++ b/lib/rift/Controller.py @@ -995,6 +995,38 @@ def action_sync(args, config): ) synchronizer.run() +def action_changelog(args, config): + """Action for 'changelog' command.""" + staff, modules = staff_modules(config) + if args.maintainer is None: + raise RiftError("You must specify a maintainer") + + pkg = Package(args.package, config, staff, modules) + pkg.load() + + author = f"{args.maintainer} <{staff.get(args.maintainer)['email']}>" + + # Format comment. + # Grab bullet, insert one if not found. + bullet = "-" + match = re.search(r'^([^\s\w])\s', args.comment, re.UNICODE) + if match: + bullet = match.group(1) + else: + args.comment = bullet + " " + args.comment + + if args.comment.find("\n") == -1: + wrapopts = {"subsequent_indent": (len(bullet) + 1) * " ", + "break_long_words": False, + "break_on_hyphens": False} + args.comment = textwrap.fill(args.comment, 80, **wrapopts) + + logging.info("Adding changelog record for '%s'", author) + Spec(pkg.specfile, + config=config).add_changelog_entry(author, args.comment, + bump=getattr(args, 'bump', False)) + return 0 + def create_staging_repo(config): """ Create and return staging temporary repository with a 2-tuple containing @@ -1154,36 +1186,9 @@ def action(config, args): 'maintainers': ', '.join(pkg.maintainers)}) print(tbl) + # CHANGELOG elif args.command == 'changelog': - - staff, modules = staff_modules(config) - if args.maintainer is None: - raise RiftError("You must specify a maintainer") - - pkg = Package(args.package, config, staff, modules) - pkg.load() - - author = f"{args.maintainer} <{staff.get(args.maintainer)['email']}>" - - # Format comment. - # Grab bullet, insert one if not found. - bullet = "-" - match = re.search(r'^([^\s\w])\s', args.comment, re.UNICODE) - if match: - bullet = match.group(1) - else: - args.comment = bullet + " " + args.comment - - if args.comment.find("\n") == -1: - wrapopts = {"subsequent_indent": (len(bullet) + 1) * " ", - "break_long_words": False, - "break_on_hyphens": False} - args.comment = textwrap.fill(args.comment, 80, **wrapopts) - - logging.info("Adding changelog record for '%s'", author) - Spec(pkg.specfile, - config=config).add_changelog_entry(author, args.comment, - bump=getattr(args, 'bump', False)) + return action_changelog(args, config) # GERRIT elif args.command == 'gerrit': diff --git a/tests/Controller.py b/tests/Controller.py index e2cb217..21973a5 100644 --- a/tests/Controller.py +++ b/tests/Controller.py @@ -20,7 +20,7 @@ make_parser, ) from rift.Package import Package -from rift.RPM import RPM +from rift.RPM import RPM, Spec from rift.run import RunResult from rift import RiftError @@ -653,6 +653,63 @@ def test_action_sync_missing_output_parent(self): main(['sync']) +class ControllerProjectActionChangelogTest(RiftProjectTestCase): + """ + Tests class for Controller action changelog + """ + + def test_action_changelog_without_pkg(self): + """changelog without package fails """ + with self.assertRaisesRegex(SystemExit, "2"): + main(['changelog']) + + def test_action_changelog_without_comment(self): + """changelog without comment fails """ + with self.assertRaisesRegex(SystemExit, "2"): + main(['changelog', 'pkg']) + + def test_action_changelog_without_maintainer(self): + """changelog without maintainer """ + with self.assertRaisesRegex(RiftError, "You must specify a maintainer"): + main(['changelog', 'pkg', '-c', 'basic change']) + + def test_action_changelog_pkg_not_found(self): + """changelog package not found""" + with self.assertRaisesRegex( + RiftError, + "Package 'pkg' directory does not exist"): + main(['changelog', 'pkg', '-c', 'basic change', '-t', 'Myself']) + + def test_action_changelog(self): + """simple changelog""" + self.make_pkg() + self.assertEqual( + main(['changelog', 'pkg', '-c', 'basic change', '-t', 'Myself']), 0) + spec = Spec(filepath=self.pkgspecs['pkg']) + spec.load() + self.assertEqual(spec.changelog_name, 'Myself - 1.0-1') + self.assertEqual(spec.version, '1.0') + self.assertEqual(spec.release, '1') + + def test_action_changelog_bump(self): + """simple changelog with bump""" + self.make_pkg() + self.assertEqual( + main(['changelog', 'pkg', '-c', 'basic change', '-t', 'Myself', '--bump']), + 0) + spec = Spec(filepath=self.pkgspecs['pkg']) + spec.load() + self.assertEqual(spec.changelog_name, 'Myself - 1.0-2') + self.assertEqual(spec.version, '1.0') + self.assertEqual(spec.release, '2') + + def test_action_changelog_unknown_maintainer(self): + """changelog with unknown maintainer""" + self.make_pkg() + with self.assertRaises(TypeError): + main(['changelog', 'pkg', '-c', 'basic change', '-t', 'Fail']) + + class ControllerArgumentsTest(RiftTestCase): """ Arguments parsing tests for Controller module"""