From 9c9b160e81ec5a9838cb9b13e5c13d528065d15f Mon Sep 17 00:00:00 2001
From: Chenxiong Qi <cqi@redhat.com>
Date: Aug 16 2018 03:56:56 +0000
Subject: Add --shell option to mockbuild


`rpkg mockbuild --shell` is equivalent to `mock -r ... --shell`

Resolves: rhbz#1438685

Signed-off-by: Chenxiong Qi <cqi@redhat.com>

---

diff --git a/etc/bash_completion.d/rpkg.bash b/etc/bash_completion.d/rpkg.bash
index 056fd3a..9dd8de3 100644
--- a/etc/bash_completion.d/rpkg.bash
+++ b/etc/bash_completion.d/rpkg.bash
@@ -171,7 +171,7 @@ _rpkg()
             options_arch="--arch"
             ;;
         mockbuild)
-            options="--md5 --no-clean --no-cleanup-after --no-clean-all"
+            options="--md5 --no-clean --no-cleanup-after --no-clean-all --shell"
             options_mroot="--root"
             ;;
         patch)
diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py
index 9adc6d1..8692635 100644
--- a/pyrpkg/__init__.py
+++ b/pyrpkg/__init__.py
@@ -2545,10 +2545,21 @@ class Commands(object):
                 raise rpkgError('Failed to remove temporary directory'
                                 ' %s. Reason: %s.' % (tmp_dir, error))
 
-    def mockbuild(self, mockargs=[], root=None, hashtype=None):
+    def mockbuild(self, mockargs=[], root=None, hashtype=None, shell=None):
         """Build the package in mock, using mockargs
 
         Log the output and returns nothing
+
+        :param list mockargs: list of command line arguments which are passed
+            mock.
+        :param str root: chroot config name which is passed to mock ``-r``
+            option.
+        :param str hashtype: used to generate SRPM only if there is no SRPM
+            generated before.
+        :param bool shell: indicate whether to go into chroot.
+
+        .. versionadded:: 1.56
+           Parameter shell.
         """
 
         # Make sure we have an srpm to run on
@@ -2581,8 +2592,13 @@ class Commands(object):
                                     ' %s' % error)
                 cmd.extend(['--configdir', config_dir])
 
-        cmd.extend(['-r', root, '--resultdir', self.mock_results_dir,
-                    '--rebuild', self.srpmname])
+        cmd += ['-r', root, '--resultdir', self.mock_results_dir]
+
+        if shell:
+            cmd.append('--shell')
+        else:
+            cmd += ['--rebuild', self.srpmname]
+
         # Run the command
         try:
             self._run_command(cmd)
diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py
index f414961..28fd6da 100644
--- a/pyrpkg/cli.py
+++ b/pyrpkg/cli.py
@@ -1042,6 +1042,11 @@ defined, packages will be built sequentially.""" % {'name': self.name})
         mockbuild_parser.add_argument(
             '--without', help='Disable configure option (bcond) for the build',
             dest='bcond_without', action='append')
+        mockbuild_parser.add_argument(
+            '--shell', action='store_true',
+            help='Run commands interactively within chroot. Before going into'
+                 ' chroot, mockbuild needs to run with --no-cleanup-after '
+                 'in advanced.')
         mockbuild_parser.set_defaults(command=self.mockbuild)
 
     def register_mock_config(self):
@@ -1895,7 +1900,8 @@ see API KEY section of copr-cli(1) man page.
             pass
         try:
             self.cmd.mockbuild(mockargs, self.args.root,
-                               hashtype=self.args.hash)
+                               hashtype=self.args.hash,
+                               shell=self.args.shell)
         except Exception as e:
             raise rpkgError(e)
 
diff --git a/tests/test_cli.py b/tests/test_cli.py
index b6099b5..8a25a77 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1419,6 +1419,18 @@ class TestMockbuild(CliTestCase):
                    '--release', 'rhel-7', 'mockbuild']
         self.assertRaises(rpkgError, self.mockbuild, cli_cmd)
 
+    def test_shell_option(self):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path,
+                   '--release', 'rhel-6', 'mockbuild',
+                   '--root', '/etc/mock/some-root', '--shell']
+        cli = self.mockbuild(cli_cmd)
+
+        expected_cmd = [
+            'mock', '-r', '/etc/mock/some-root',
+            '--resultdir', cli.cmd.mock_results_dir, '--shell'
+        ]
+        self.mock_run_command.assert_called_with(expected_cmd)
+
 
 class TestCoprBuild(CliTestCase):
     """Test copr command"""

