Skip to content

Commit

Permalink
Return 1 if --diff changed the code.
Browse files Browse the repository at this point in the history
This is how GNU diff acts.
  • Loading branch information
bwendling committed Aug 1, 2017
1 parent 010a201 commit 71d9b2e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -5,6 +5,7 @@
## [0.16.4] Unreleased
### Changed
- Adjust blank lines on formatting boundaries when using the `--lines` option.
- Return 1 if a diff changed the code. This is in line with how GNU diff acts.

## [0.16.3] 2017-07-13
### Changed
Expand Down
4 changes: 2 additions & 2 deletions yapf/__init__.py
Expand Up @@ -177,7 +177,7 @@ def main(argv):
if not files:
raise errors.YapfError('Input filenames did not match any python files')

FormatFiles(
changed = FormatFiles(
files,
lines,
style_config=args.style,
Expand All @@ -186,7 +186,7 @@ def main(argv):
print_diff=args.diff,
verify=args.verify,
parallel=args.parallel)
return 0
return 1 if changed and args.diff else 0


def FormatFiles(filenames,
Expand Down
6 changes: 3 additions & 3 deletions yapf/yapflib/reformatter.py
Expand Up @@ -68,8 +68,8 @@ def Reformat(uwlines, verify=False, lines=None):
if prev_uwline and prev_uwline.disable:
# Keep the vertical spacing between a disabled and enabled formatting
# region.
_RetainRequiredVerticalSpacingBetweenTokens(
uwline.first, prev_uwline.last, lines)
_RetainRequiredVerticalSpacingBetweenTokens(uwline.first,
prev_uwline.last, lines)
if any(tok.is_comment for tok in uwline.tokens):
_RetainVerticalSpacingBeforeComments(uwline)

Expand Down Expand Up @@ -134,7 +134,7 @@ def _RetainRequiredVerticalSpacingBetweenTokens(cur_tok, prev_tok, lines):
cur_lineno = cur_tok.lineno

if prev_tok.value.endswith('\\'):
prev_lineno = prev_lineno + prev_tok.value.count('\n')
prev_lineno += prev_tok.value.count('\n')

required_newlines = cur_lineno - prev_lineno

Expand Down
6 changes: 3 additions & 3 deletions yapf/yapflib/unwrapped_line.py
Expand Up @@ -331,9 +331,9 @@ def _SpaceRequiredBetween(left, right):

def _MustBreakBefore(prev_token, cur_token):
"""Return True if a line break is required before the current token."""
if prev_token.is_comment or (
prev_token.previous_token and prev_token.is_pseudo_paren and
prev_token.previous_token.is_comment):
if prev_token.is_comment or (prev_token.previous_token and
prev_token.is_pseudo_paren and
prev_token.previous_token.is_comment):
# Must break if the previous token was a comment.
return True
if (cur_token.is_string and prev_token.is_string and
Expand Down
2 changes: 1 addition & 1 deletion yapf/yapflib/yapf_api.py
Expand Up @@ -152,7 +152,7 @@ def FormatCode(unformatted_source,
unformatted_source, reformatted_source, filename=filename)

if print_diff:
return code_diff, code_diff != ''
return code_diff, code_diff

return reformatted_source, True

Expand Down
5 changes: 4 additions & 1 deletion yapftests/yapf_test.py
Expand Up @@ -492,7 +492,10 @@ def f():
suffix='.py', dirname=self.test_tmpdir) as (out, _):
with utils.TempFileContents(
self.test_tmpdir, unformatted_code, suffix='.py') as filepath:
subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
try:
subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
except subprocess.CalledProcessError as e:
self.assertEqual(e.returncode, 1) # Indicates the text changed.

def testReformattingSpecificLines(self):
unformatted_code = textwrap.dedent("""\
Expand Down

0 comments on commit 71d9b2e

Please sign in to comment.