From 6ac26c9ad032d532d717ce3eab6d45f585a9792b Mon Sep 17 00:00:00 2001 From: Reece Hart Date: Wed, 27 Jul 2016 21:41:59 -0700 Subject: [PATCH] fixed #228: make exit code customizable to indicated whether files were changed --- yapf/__init__.py | 10 ++++++++-- yapftests/main_test.py | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/yapf/__init__.py b/yapf/__init__.py index dfbb8ee2d..1348066f3 100644 --- a/yapf/__init__.py +++ b/yapf/__init__.py @@ -87,6 +87,12 @@ def main(argv): default=None, help='range of lines to reformat, one-based') + parser.add_argument( + '-c', + '--changed-files-exit-code', + default=0, + type=int, + help='exit code to indicate that files were changed') parser.add_argument( '-e', '--exclude', @@ -161,7 +167,7 @@ def main(argv): lines=lines, verify=args.verify) sys.stdout.write(reformatted_source) - return 2 if changed else 0 + return args.changed_files_exit_code if changed else 0 files = file_resources.GetCommandLineFiles(args.files, args.recursive, args.exclude) @@ -175,7 +181,7 @@ def main(argv): in_place=args.in_place, print_diff=args.diff, verify=args.verify) - return 2 if changed else 0 + return args.changed_files_exit_code if changed else 0 def FormatFiles(filenames, diff --git a/yapftests/main_test.py b/yapftests/main_test.py index e5a5a74ce..163fc8872 100644 --- a/yapftests/main_test.py +++ b/yapftests/main_test.py @@ -91,7 +91,7 @@ def testEchoInputWithStyle(self): with patched_input(code): with captured_output() as (out, err): ret = yapf.main(['-', '--style=chromium']) - self.assertEqual(ret, 2) + self.assertEqual(ret, 0) self.assertEqual(out.getvalue(), chromium_code) def testEchoBadInput(self): @@ -116,3 +116,25 @@ def testVersion(self): self.assertEqual(ret, 0) version = 'yapf {}\n'.format(yapf.__version__) self.assertEqual(version, out.getvalue()) + + def testUnchangedFileExitCode(self): + code = "a = 1" + with patched_input(code): + with captured_output() as (out, err): + ret = yapf.main([]) + self.assertEqual(ret, 0) + + def testChangedFileExitCode(self): + code = "a=1" + with patched_input(code): + with captured_output() as (out, err): + ret = yapf.main([]) + self.assertEqual(ret, 0) + + def testCustomChangedFileExitCode(self): + code = "a=1" + with patched_input(code): + with captured_output() as (out, err): + ret = yapf.main(['-', '-c', '2']) + self.assertEqual(ret, 2) +