Browse Source

lucicfg: format star files if --lucicfg

Bug: 390409288
Change-Id: Ida238f3a903c4f1dd31c8fa007e0a8823aeaf1aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6219826
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Scott Lee <ddoman@chromium.org>
Scott Lee 6 months ago
parent
commit
c60f8194a5
2 changed files with 67 additions and 0 deletions
  1. 24 0
      git_cl.py
  2. 43 0
      tests/git_cl_test.py

+ 24 - 0
git_cl.py

@@ -6843,6 +6843,24 @@ def _RunMetricsXMLFormat(opts, paths, top_dir, upstream_commit):
     return return_value
 
 
+def _RunLUCICfgFormat(opts, paths, top_dir, upstream_commit):
+    depot_tools_path = os.path.dirname(os.path.abspath(__file__))
+    lucicfg = os.path.join(depot_tools_path, 'lucicfg')
+    if sys.platform == 'win32':
+        lucicfg += '.bat'
+
+    cmd = [lucicfg, 'fmt']
+    if opts.dry_run:
+        cmd.append('--dry-run')
+    cmd.extend(paths)
+
+    ret = subprocess2.call(cmd)
+    if opts.dry_run and ret != 0:
+        return 2
+
+    return ret
+
+
 def MatchingFileType(file_name, extensions):
     """Returns True if the file name ends with one of the given extensions."""
     return bool([ext for ext in extensions if file_name.lower().endswith(ext)])
@@ -6929,6 +6947,10 @@ def CMDformat(parser, args):
                       action='store_true',
                       help='Disable auto-formatting of .java')
 
+    parser.add_option('--lucicfg',
+                      action='store_true',
+                      help='Enables formatting of .star files.')
+
     opts, files = parser.parse_args(args)
 
     # Normalize files against the current path, so paths relative to the
@@ -6982,6 +7004,8 @@ def CMDformat(parser, args):
         formatters += [(['.py'], _RunYapf)]
     if opts.mojom:
         formatters += [(['.mojom'], _RunMojomFormat)]
+    if opts.lucicfg:
+        formatters += [(['.star'], _RunLUCICfgFormat)]
 
     top_dir = settings.GetRoot()
     return_value = 0

+ 43 - 0
tests/git_cl_test.py

@@ -5295,6 +5295,49 @@ class CMDFormatTestCase(unittest.TestCase):
                 cwd=self._top_dir),
         ])
 
+    @mock.patch('subprocess2.call')
+    def testLUCICfgFormatWorks(self, mock_call):
+        """Checks if lucicfg is given then input file path."""
+        mock_opts = mock.Mock(dry_run=False)
+        files = ['test/main.star']
+        mock_call.return_value = 0
+        ret = git_cl._RunLUCICfgFormat(mock_opts, files, self._top_dir, 'HEAD')
+        mock_call.assert_called_with([
+            mock.ANY,
+            'fmt',
+            'test/main.star',
+        ])
+        self.assertEqual(ret, 0)
+
+    @mock.patch('subprocess2.call')
+    def testLUCICfgFormatWithDryRun(self, mock_call):
+        """Tests the command with --dry-run."""
+        mock_opts = mock.Mock(dry_run=True)
+        files = ['test/main.star']
+        git_cl._RunLUCICfgFormat(mock_opts, files, self._top_dir, 'HEAD')
+        mock_call.assert_called_with([
+            mock.ANY,
+            'fmt',
+            '--dry-run',
+            'test/main.star',
+        ])
+
+    @mock.patch('subprocess2.call')
+    def testLUCICfgFormatWithDryRunReturnCode(self, mock_call):
+        """Tests that it returns 2 for non-zero exit codes."""
+        mock_opts = mock.Mock(dry_run=True)
+        files = ['test/main.star']
+        run = git_cl._RunLUCICfgFormat
+
+        mock_call.return_value = 0
+        self.assertEqual(run(mock_opts, files, self._top_dir, 'HEAD'), 0)
+        mock_call.return_value = 1
+        self.assertEqual(run(mock_opts, files, self._top_dir, 'HEAD'), 2)
+        mock_call.return_value = 2
+        self.assertEqual(run(mock_opts, files, self._top_dir, 'HEAD'), 2)
+        mock_call.return_value = 255
+        self.assertEqual(run(mock_opts, files, self._top_dir, 'HEAD'), 2)
+
 
 @unittest.skipIf(gclient_utils.IsEnvCog(),
                 'not supported in non-git environment')