Skip to content

Commit

Permalink
fixed #228: make exit code customizable to indicated whether files we…
Browse files Browse the repository at this point in the history
…re changed
  • Loading branch information
reece committed Jul 28, 2016
1 parent f4de4c6 commit 6ac26c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 8 additions & 2 deletions yapf/__init__.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
24 changes: 23 additions & 1 deletion yapftests/main_test.py
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit 6ac26c9

Please sign in to comment.