diff --git a/git_cl.py b/git_cl.py index 13d64035a..4067e8b5a 100755 --- a/git_cl.py +++ b/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 diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 87d8e1e4d..d0c0aeba6 100755 --- a/tests/git_cl_test.py +++ b/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')