From e1b117ef5005bbbffcfc848d5dda341b3b9ee406 Mon Sep 17 00:00:00 2001
From: Martin Curlej <mcurlej@redhat.com>
Date: Jan 05 2018 11:30:54 +0000
Subject: Updated module cli API


Signed-off-by: Martin Curlej <mcurlej@redhat.com>

added --skiptests option.

Signed-off-by: Martin Curlej <mcurlej@redhat.com>

used self.cmd.module_name, added file check

Signed-off-by: Martin Curlej <mcurlej@redhat.com>

Fixed typos, help desc, using repo path

---

diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py
index 3757fcf..be6689b 100644
--- a/pyrpkg/__init__.py
+++ b/pyrpkg/__init__.py
@@ -2834,20 +2834,19 @@ class Commands(object):
             actual_scm_url = '{0}?#{1}'.format(actual_scm_url, self.commithash)
         return actual_scm_url, actual_branch
 
-    def module_local_build(self, scm_url, branch, local_builds_nsvs=None,
-                           skip_tests=False, verbose=False, debug=False):
+    def module_local_build(self, file_path, stream, local_builds_nsvs=None, verbose=False,
+                           debug=False, skip_tests=False):
         """
         A wrapper for `mbs-manager build_module_locally`.
-        :param scm_url: a string of the module's SCM URL.
-        :param branch: a string of the module's branch.
-        :kwarg local_builds_nsvs: a list of localbuilds to import into MBS
+        :param file_path: a string, path of the module's modulemd yaml file.
+        :param stream: a string, stream of the module.
+        :kwarg local_builds_nsvs: a list of localbuild ids to import into MBS
         before running this local build.
-        :kwarg skip_tests: a boolean determining if the check sections should
-        be skipped.
         :kwarg verbose: a boolean specifying if mbs-manager should be verbose.
         This is overridden by self.quiet.
         :kwarg debug: a boolean specifying if mbs-manager should be debug.
         This is overridden by self.quiet and verbose.
+        :kwarg skip_tests: a boolean determining if the check sections should be skipped
         :return: None
         """
         command = ['mbs-manager']
@@ -2858,14 +2857,17 @@ class Commands(object):
         elif debug:
             command.append('-d')
         command.append('build_module_locally')
-        if skip_tests:
-            command.append('--skiptests')
 
         if local_builds_nsvs:
             for build_id in local_builds_nsvs:
                 command += ['--add-local-build', build_id]
 
-        command.extend([scm_url, branch])
+        if skip_tests:
+            command.append('--skiptests')
+
+        command.extend(['--file', file_path])
+        command.extend(['--stream', stream])
+
         self._run_command(command)
 
     def module_overview(self, api_url, limit=10, finished=True):
diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py
index c75827d..279a7c7 100644
--- a/pyrpkg/cli.py
+++ b/pyrpkg/cli.py
@@ -755,15 +755,16 @@ defined, packages will be built sequentially.""" % {'name': self.name})
         self.module_build_local_parser = self.subparsers.add_parser(
             'module-build-local', help=sub_help, description=sub_help)
         self.module_build_local_parser.add_argument(
-            'scm_url', nargs='?',
-            help='The module\'s SCM URL. This defaults to the current repo.')
+            '--file', nargs='?', dest='file_path',
+            help=('The module\'s modulemd yaml file. If not specified, a yaml file'
+                  ' with the same basename as the name of the repository will be used.'))
         self.module_build_local_parser.add_argument(
-            'branch', nargs='?',
-            help=('The module\'s SCM branch. This defaults to the current '
+            '--stream', nargs='?', dest='stream',
+            help=('The module\'s stream/SCM branch. This defaults to the current '
                   'checked-out branch.'))
         self.module_build_local_parser.add_argument(
             '--skip-tests', help='Adds a macro for skipping the check section',
-            action='store_true')
+            action='store_true', dest='skiptests')
         self.module_build_local_parser.add_argument(
             '--add-local-build', action='append', dest='local_builds_nsvs',
             metavar='BUILD_ID', type=int,
@@ -1481,11 +1482,23 @@ see API KEY section of copr-cli(1) man page.
         :return: None
         """
         self.module_validate_config()
-        scm_url, branch = self.cmd.module_get_scm_info(
-            self.args.scm_url, self.args.branch)
+
+        if not self.args.stream:
+            _, stream = self.cmd.module_get_scm_info()
+        else:
+            stream = self.args.stream
+
+        if not self.args.file_path:
+            file_path = os.path.join(self.cmd.path, self.cmd.module_name + ".yaml")
+        else:
+            file_path = self.args.file_path
+
+        if not os.path.isfile(file_path):
+            raise IOError("Module metadata yaml file %s not found!" % file_path)
+
         self.cmd.module_local_build(
-            scm_url, branch, self.args.local_builds_nsvs,
-            self.args.skip_tests, verbose=self.args.v, debug=self.args.debug)
+            file_path, stream, self.args.local_builds_nsvs,
+            verbose=self.args.v, debug=self.args.debug, skip_tests=self.args.skiptests)
 
     def module_get_auth_config(self):
         """
diff --git a/tests/test_cli.py b/tests/test_cli.py
index b6d1bf2..07b5559 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -2132,18 +2132,97 @@ State:    failed
             'rpkg',
             '--path',
             self.cloned_repo_path,
+            'module-build-local'
+        ]
+        mock_proc = Mock()
+        mock_proc.returncode = 0
+        mock_run.return_value = mock_proc
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            file_path = os.path.join(self.cloned_repo_path, cli.cmd.module_name + '.yaml')
+            # we create an empty file for the purpose of this test so we don't raise an exception
+            open(file_path, 'a').close()
+            cli.module_build_local()
+
+        mock_run.assert_called_once_with(['mbs-manager', 'build_module_locally', '--file',
+                                          file_path, '--stream', 'master'])
+
+    @patch.object(Commands, '_run_command')
+    def test_module_build_local_file_not_found(self, mock_run):
+        """
+        Test submitting a local module build and raising an IOError exception
+        """
+        cli_cmd = [
+            'rpkg',
+            '--path',
+            self.cloned_repo_path,
+            'module-build-local'
+        ]
+        mock_proc = Mock()
+        mock_proc.returncode = 0
+        mock_run.return_value = mock_proc
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            with self.assertRaises(IOError):
+                cli.module_build_local()
+
+    @patch.object(Commands, '_run_command')
+    def test_module_build_local_with_params(self, mock_run):
+        """
+        Test submitting a local module build with parameters
+        """
+
+        file_path = os.path.join(self.cloned_repo_path, 'modulemd.yaml')
+
+        cli_cmd = [
+            'rpkg',
+            '--path',
+            self.cloned_repo_path,
             'module-build-local',
-            'git://pkgs.fedoraproject.org/modules/testmodule?#79d87a5a',
-            'master'
+            '--file',
+            file_path,
+            '--stream',
+            'test'
         ]
         mock_proc = Mock()
         mock_proc.returncode = 0
         mock_run.return_value = mock_proc
         with patch('sys.argv', new=cli_cmd):
             cli = self.new_cli()
+            # we create an empty file for the purpose of this test so we don't raise an exception
+            open(file_path, 'a').close()
             cli.module_build_local()
-        mock_run.assert_called_once_with([
-            'mbs-manager',
-            'build_module_locally',
-            'git://pkgs.fedoraproject.org/modules/testmodule?#79d87a5a',
-            'master'])
+
+        mock_run.assert_called_once_with(['mbs-manager', 'build_module_locally', '--file',
+                                          file_path, '--stream', 'test'])
+
+    @patch.object(Commands, '_run_command')
+    def test_module_build_local_with_skiptests(self, mock_run):
+        """
+        Test submitting a local module build with parameters
+        """
+
+        file_path = os.path.join(self.cloned_repo_path, "modulemd.yaml")
+
+        cli_cmd = [
+            'rpkg',
+            '--path',
+            self.cloned_repo_path,
+            'module-build-local',
+            '--file',
+            file_path,
+            '--stream',
+            'test',
+            '--skip-tests'
+        ]
+        mock_proc = Mock()
+        mock_proc.returncode = 0
+        mock_run.return_value = mock_proc
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            # we create an empty file for the purpose of this test so we don't raise an exception
+            open(file_path, 'a').close()
+            cli.module_build_local()
+
+        mock_run.assert_called_once_with(['mbs-manager', 'build_module_locally', '--skiptests',
+                                          '--file', file_path, '--stream', 'test'])

