Skip to content

Commit

Permalink
Merge pull request #553 from hhatto/issue-551
Browse files Browse the repository at this point in the history
fix: ignoring w292 with in-place option
  • Loading branch information
hhatto committed Jun 22, 2020
2 parents 9d82181 + 1ee2a56 commit 2ab2ea7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion autopep8.py
Expand Up @@ -3534,7 +3534,13 @@ def fix_file(filename, options=None, output=None, apply_config=False):
elif options.in_place:
original = "".join(original_source).splitlines()
fixed = fixed_source.splitlines()
if original != fixed:
original_source_last_line = (
original_source[-1].split("\n")[-1] if original_source else ""
)
fixed_source_last_line = fixed_source.split("\n")[-1]
if original != fixed or (
original_source_last_line != fixed_source_last_line
):
with open_with_encoding(filename, 'w', encoding=encoding) as fp:
fp.write(fixed_source)
return fixed_source
Expand Down
22 changes: 22 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -5360,6 +5360,28 @@ def test_in_place_no_modifications_no_writes(self):
self.assertEqual(err, b'')
self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK)

def test_in_place_no_modifications_no_writes_with_empty_file(self):
with temporary_file_context('') as filename:
# ensure that noops do not do writes by making writing an error
os.chmod(filename, 0o444)
p = Popen(
list(AUTOPEP8_CMD_TUPLE) + [filename, '--in-place'],
stderr=PIPE,
)
_, err = p.communicate()
self.assertEqual(err, b'')
self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK)

def test_in_place_with_w292(self):
line = "import os"
fixed = "import os\n"

with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename, '--in-place'])
p.wait()
with open(filename) as f:
self.assertEqual(fixed, f.read())

def test_in_place_with_exit_code_option(self):
line = "'abc' \n"
fixed = "'abc'\n"
Expand Down

0 comments on commit 2ab2ea7

Please sign in to comment.