From 57cf67d074a1294bb85194a1485bb22672de5c2f Mon Sep 17 00:00:00 2001 From: Ondřej Nosek Date: Apr 14 2025 23:15:37 +0000 Subject: `rhpkg mockbuild` won't show a hint with '--target' When the build target is not found, a hint suggesting '--target' option is incorrect, because such an option is not supported for `mockbuild`. The 'target' option is only possible for `mock-config`. For `mockbuild`, a modified hint is shown suggesting '--release' option instead. JIRA: RHELCMP-13842 Signed-off-by: Ondřej Nosek --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 32ad4ff..96420cf 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -2974,13 +2974,15 @@ class Commands(object): self._cleanup_tmp_dir(tmpdir) # Not to be confused with mockconfig the property - def mock_config(self, target=None, arch=None): + def mock_config(self, target=None, arch=None, standalone=None): """Generate a mock config based on branch data. :param str target: an alternative build target, otherwise default build target will be used. :param str arch: an alternative arch, otherwise local system arch will be used. + :param bool standalone: If True, mock_config is run in standalone mode, + not as part of the mockbuild command :return: the mock config content got from Koji. :rtype: str """ @@ -2994,8 +2996,14 @@ class Commands(object): # Figure out if we have a valid build target build_target = self.anon_kojisession.getBuildTarget(target) if not build_target: - raise rpkgError('Unknown build target: %s\n' - 'Consider using the --target option' % target) + if standalone: + raise rpkgError('Unknown build target: %s\n' + 'Consider using/change the --target option' % target) + else: + # 'target' passed to the 'mock_config' method is always None when + # not standalone - use self.target + raise rpkgError('Unknown build target: %s\n' + 'Consider using/change the "x-pkg --release" option' % self.target) try: repoid = self.anon_kojisession.getRepo( diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 2a0c138..6cf1656 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -2564,7 +2564,7 @@ class cliClient(object): raise rpkgError(e) def mock_config(self): - print(self.cmd.mock_config(self.args.target, self.args.arch)) + print(self.cmd.mock_config(self.args.target, self.args.arch, standalone=True)) def module_build(self): """Builds a module using MBS""" diff --git a/tests/test_cli.py b/tests/test_cli.py index dd1a2be..ae532b0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2029,6 +2029,23 @@ class TestMockbuild(CliTestCase): cli.cmd.srpmname] self.mock_run_command.assert_called_with(expected_cmd) + def test_fail_if_target_not_exists_message(self): + self.anon_kojisession_patcher = patch( + 'pyrpkg.Commands.anon_kojisession', + new_callable=PropertyMock) + self.mock_anon_kojisession = self.anon_kojisession_patcher.start() + self.kojisession = self.mock_anon_kojisession.return_value + self.kojisession.getBuildTarget.return_value = None + + cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', + 'mockbuild'] + + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + six.assertRaisesRegex( + self, rpkgError, + 'Consider using/change the "x-pkg --release" option', cli.mockbuild) + class TestCoprBuild(CliTestCase): """Test copr command""" @@ -2202,6 +2219,18 @@ class TestMockConfig(CliTestCase): cli = self.new_cli() self.assertRaises(rpkgError, cli.mock_config) + def test_fail_if_specified_target_not_exists_message(self): + self.kojisession.getBuildTarget.return_value = None + + cli_cmd = ['rpkg', '--path', self.cloned_repo_path, + 'mock-config', '--target', 'some-target'] + + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + six.assertRaisesRegex( + self, rpkgError, + 'Consider using/change the --target option', cli.mock_config) + def test_fail_if_cannot_find_a_valid_repo(self): self.kojisession.getRepo.side_effect = Exception