From 57b643daa7748bf12476a40ccedd465bd2eaf1f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Palancher?= Date: Wed, 23 Jul 2025 14:11:43 +0200 Subject: [PATCH 1/4] Controller: introduce action_create_import() Code is just moved without any modification. The goal is to reduce statements and complexity of huge action() function. --- lib/rift/Controller.py | 73 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/lib/rift/Controller.py b/lib/rift/Controller.py index 02684272..be9a6296 100644 --- a/lib/rift/Controller.py +++ b/lib/rift/Controller.py @@ -995,6 +995,44 @@ def action_sync(args, config): ) synchronizer.run() +def action_create_import(args, config): + """Action for 'create', 'import' and 'reimport' commands.""" + if args.command == 'create': + pkgname = args.name + elif args.command in ('import', 'reimport'): + rpm = RPM(args.file, config) + if not rpm.is_source: + raise RiftError(f"{args.file} is not a source RPM") + pkgname = rpm.name + + if args.maintainer is None: + raise RiftError("You must specify a maintainer") + + pkg = Package(pkgname, config, *staff_modules(config)) + if args.command == 'reimport': + pkg.load() + + if args.module: + pkg.module = args.module + if args.maintainer not in pkg.maintainers: + pkg.maintainers.append(args.maintainer) + if args.reason: + pkg.reason = args.reason + if args.origin: + pkg.origin = args.origin + + pkg.check_info() + pkg.write() + + if args.command in ('create', 'import'): + message(f"Package '{pkg.name}' has been created") + + if args.command in ('import', 'reimport'): + rpm.extract_srpm(pkg.dir, pkg.sourcesdir) + message(f"Package '{pkg.name}' has been {args.command}ed") + + return 0 + def create_staging_repo(config): """ Create and return staging temporary repository with a 2-tuple containing @@ -1049,40 +1087,7 @@ def action(config, args): # CREATE/IMPORT/REIMPORT if args.command in ['create', 'import', 'reimport']: - - if args.command == 'create': - pkgname = args.name - elif args.command in ('import', 'reimport'): - rpm = RPM(args.file, config) - if not rpm.is_source: - raise RiftError(f"{args.file} is not a source RPM") - pkgname = rpm.name - - if args.maintainer is None: - raise RiftError("You must specify a maintainer") - - pkg = Package(pkgname, config, *staff_modules(config)) - if args.command == 'reimport': - pkg.load() - - if args.module: - pkg.module = args.module - if args.maintainer not in pkg.maintainers: - pkg.maintainers.append(args.maintainer) - if args.reason: - pkg.reason = args.reason - if args.origin: - pkg.origin = args.origin - - pkg.check_info() - pkg.write() - - if args.command in ('create', 'import'): - message(f"Package '{pkg.name}' has been created") - - if args.command in ('import', 'reimport'): - rpm.extract_srpm(pkg.dir, pkg.sourcesdir) - message(f"Package '{pkg.name}' has been {args.command}ed") + return action_create_import(args, config) # BUILD elif args.command == 'build': From e278a7d2cbd36d24a21c50df1198668daba03352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Palancher?= Date: Thu, 24 Jul 2025 16:04:09 +0200 Subject: [PATCH 2/4] Tests: add tests for create action --- tests/Controller.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Controller.py b/tests/Controller.py index e2cb217e..26f5ab1f 100644 --- a/tests/Controller.py +++ b/tests/Controller.py @@ -44,6 +44,44 @@ def test_main_version(self): self.assert_except(SystemExit, "0", main, ['--version']) +class ControllerProjectActionCreateTest(RiftProjectTestCase): + """ + Tests class for Controller action create + """ + + def test_create_missing_pkg_module_reason(self): + """create without package, module or reason fails""" + for cmd in (['create', '-m', 'Great module', '-r', 'Good reason'], + ['create', 'pkg', '-r', 'Good reason'], + ['create', 'pkg', '-m', 'Great module']): + with self.assertRaisesRegex(SystemExit, "2"): + main(cmd) + + def test_create_missing_maintainer(self): + """create without maintainer""" + with self.assertRaisesRegex(RiftError, "You must specify a maintainer"): + main(['create', 'pkg', '-m', 'Great module', '-r', 'Good reason']) + + def test_create(self): + """simple create""" + main(['create', 'pkg', '-m', 'Great module', '-r', 'Good reason', + '--maintainer', 'Myself']) + pkg = Package('pkg', self.config, self.staff, self.modules) + pkg.load() + self.assertEqual(pkg.module, 'Great module') + self.assertEqual(pkg.reason, 'Good reason') + self.assertCountEqual(pkg.maintainers, ['Myself']) + os.unlink(pkg.metafile) + os.rmdir(os.path.dirname(pkg.metafile)) + + def test_create_unknown_maintainer(self): + """create with unknown maintainer fails""" + with self.assertRaisesRegex( + RiftError, "Maintainer 'Fail' is not defined"): + main(['create', 'pkg', '-m', 'Great module', '-r', 'Good reason', + '--maintainer', 'Fail']) + + class ControllerProjectTest(RiftProjectTestCase): """ Tests class for Controller From dd9e8e3c413e736e5ba749a72e120c142caa73ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Palancher?= Date: Fri, 25 Jul 2025 10:44:41 +0200 Subject: [PATCH 3/4] Tests: add tests for import action --- tests/Controller.py | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/Controller.py b/tests/Controller.py index 26f5ab1f..1f846b6d 100644 --- a/tests/Controller.py +++ b/tests/Controller.py @@ -82,6 +82,68 @@ def test_create_unknown_maintainer(self): '--maintainer', 'Fail']) +class ControllerProjectActionImportTest(RiftProjectTestCase): + """ + Tests class for Controller action import + """ + @property + def src_rpm(self): + return os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'materials', 'pkg-1.0-1.src.rpm' + ) + + @property + def bin_rpm(self): + return os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'materials', 'pkg-1.0-1.noarch.rpm' + ) + + def test_import_missing_pkg_module_reason(self): + """import without package, module or reason fails""" + for cmd in (['import', '-m', 'Great module', '-r', 'Good reason'], + ['import', 'pkg.src.rpm', '-r', 'Good reason'], + ['import', 'pkg.src.rpm', '-m', 'Great module']): + with self.assertRaisesRegex(SystemExit, "2"): + main(cmd) + + def test_import_missing_maintainer(self): + """import without maintainer""" + with self.assertRaisesRegex(RiftError, "You must specify a maintainer"): + main(['import', self.src_rpm, '-m', 'Great module', '-r', 'Good reason']) + + def test_import_bin_rpm(self): + """import binary rpm""" + with self.assertRaisesRegex( + RiftError, + ".*pkg-1.0-1.noarch.rpm is not a source RPM$"): + main(['import', self.bin_rpm, '-m', 'Great module', + '-r', 'Good reason', '--maintainer', 'Myself']) + + def test_import(self): + """simple import""" + main(['import', self.src_rpm, '-m', 'Great module', '-r', 'Good reason', + '--maintainer', 'Myself']) + pkg = Package('pkg', self.config, self.staff, self.modules) + pkg.load() + self.assertEqual(pkg.module, 'Great module') + self.assertEqual(pkg.reason, 'Good reason') + self.assertCountEqual(pkg.maintainers, ['Myself']) + spec = Spec(filepath=pkg.specfile) + spec.load() + self.assertEqual(spec.changelog_name, 'Myself - 1.0-1') + self.assertEqual(spec.version, '1.0') + self.assertEqual(spec.release, '1') + self.assertTrue(os.path.exists(f"{pkg.specfile}.orig")) + shutil.rmtree(os.path.dirname(pkg.metafile)) + + def test_import_unknown_maintainer(self): + """import with unknown maintainer fails""" + with self.assertRaisesRegex( + RiftError, "Maintainer 'Fail' is not defined"): + main(['import', self.src_rpm, '-m', 'Great module', + '-r', 'Good reason', '--maintainer', 'Fail']) + + class ControllerProjectTest(RiftProjectTestCase): """ Tests class for Controller From 38fd3511e9b646c5826f230acd15c2fd81b9b010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Palancher?= Date: Fri, 25 Jul 2025 11:29:57 +0200 Subject: [PATCH 4/4] Tests: add tests for reimport action --- tests/Controller.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/Controller.py b/tests/Controller.py index 1f846b6d..21ecd9d7 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 @@ -144,6 +144,45 @@ def test_import_unknown_maintainer(self): '-r', 'Good reason', '--maintainer', 'Fail']) +class ControllerProjectActionReimportTest(RiftProjectTestCase): + """ + Tests class for Controller actionre import + """ + @property + def src_rpm(self): + return os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'materials', 'pkg-1.0-1.src.rpm' + ) + + @property + def bin_rpm(self): + return os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'materials', 'pkg-1.0-1.noarch.rpm' + ) + + def test_reimport_missing_maintainer(self): + """reimport without maintainer""" + with self.assertRaisesRegex(RiftError, "You must specify a maintainer"): + main(['reimport', self.src_rpm, '-m', 'Great module', '-r', 'Good reason']) + + def test_reimport(self): + """simple reimport""" + self.make_pkg(name='pkg') + main(['reimport', self.src_rpm, '--maintainer', 'Myself']) + pkg = Package('pkg', self.config, self.staff, self.modules) + pkg.load() + self.assertEqual(pkg.module, 'Great module') + self.assertEqual(pkg.reason, 'Missing feature') + self.assertCountEqual(pkg.maintainers, ['Myself']) + spec = Spec(filepath=pkg.specfile) + spec.load() + self.assertEqual(spec.changelog_name, 'Myself - 1.0-1') + self.assertEqual(spec.version, '1.0') + self.assertEqual(spec.release, '1') + self.assertTrue(os.path.exists(f"{pkg.specfile}.orig")) + os.unlink(f"{pkg.specfile}.orig") + + class ControllerProjectTest(RiftProjectTestCase): """ Tests class for Controller