Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exit code is non-zero value when occur io error #592

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -4324,6 +4324,7 @@ def _fix_file(parameters):
return fix_file(*parameters)
except IOError as error:
print(unicode(error), file=sys.stderr)
raise error


def fix_multiple_files(filenames, options, output=None):
Expand All @@ -4337,12 +4338,17 @@ def fix_multiple_files(filenames, options, output=None):
if options.jobs > 1:
import multiprocessing
pool = multiprocessing.Pool(options.jobs)
ret = pool.map(_fix_file, [(name, options) for name in filenames])
rets = []
for name in filenames:
ret = pool.apply_async(_fix_file, ((name, options),))
rets.append(ret)
pool.close()
pool.join()
if options.diff:
for r in ret:
sys.stdout.write(r.decode())
for r in rets:
sys.stdout.write(r.get().decode())
sys.stdout.flush()
results.extend([x for x in ret if x is not None])
results.extend([x.get() for x in rets if x is not None])
else:
for name in filenames:
ret = _fix_file((name, options, output))
Expand Down Expand Up @@ -4458,6 +4464,8 @@ def main(argv=None, apply_config=True):
ret = any([ret is not None for ret in results])
if args.exit_code and ret:
return EXIT_CODE_EXISTS_DIFF
except IOError:
return EXIT_CODE_ERROR
except KeyboardInterrupt:
return EXIT_CODE_ERROR # pragma: no cover

Expand Down
45 changes: 45 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import contextlib
import io
import shutil
import stat
from subprocess import Popen, PIPE
from tempfile import mkstemp, mkdtemp
import tokenize
Expand Down Expand Up @@ -5503,6 +5504,15 @@ def test_diff_with_standard_in(self):
error = p.communicate()[1].decode('utf-8')
self.assertIn('cannot', error)

def test_exit_code_with_io_error(self):
line = "import sys\ndef a():\n print(1)\n"
with readonly_temporary_file_context(line) as filename:
print(filename)
p = Popen(list(AUTOPEP8_CMD_TUPLE) + ['--in-place', filename],
stdout=PIPE, stderr=PIPE)
result = p.communicate()
self.assertEqual(p.returncode, 1)

def test_pep8_passes(self):
line = "'abc' \n"
fixed = "'abc'\n"
Expand Down Expand Up @@ -5671,6 +5681,28 @@ def test_parallel_jobs_with_diff_option(self):
for actual_diff in actual_diffs:
self.assertIn(actual_diff, output)

def test_parallel_jobs_with_inplace_option_and_io_error(self):
temp_directory = mkdtemp(dir='.')
try:
file_a = os.path.join(temp_directory, 'a.py')
with open(file_a, 'w') as output:
output.write("'abc' \n")
os.chmod(file_a, stat.S_IRUSR) # readonly

os.mkdir(os.path.join(temp_directory, 'd'))
file_b = os.path.join(temp_directory, 'd', 'b.py')
with open(file_b, 'w') as output:
output.write('123 \n')
os.chmod(file_b, stat.S_IRUSR)

p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[temp_directory, '--recursive', '--in-place'],
stdout=PIPE, stderr=PIPE)
result = p.communicate()[0].decode('utf-8')
self.assertEqual(p.returncode, 1)
finally:
shutil.rmtree(temp_directory)

def test_parallel_jobs_with_automatic_cpu_count(self):
line = "'abc' \n"
fixed = "'abc'\n"
Expand Down Expand Up @@ -7363,6 +7395,19 @@ def temporary_file_context(text, suffix='', prefix=''):
os.remove(temporary[1])


@contextlib.contextmanager
def readonly_temporary_file_context(text, suffix='', prefix=''):
temporary = mkstemp(suffix=suffix, prefix=prefix)
os.close(temporary[0])
with autopep8.open_with_encoding(temporary[1],
encoding='utf-8',
mode='w') as temp_file:
temp_file.write(text)
os.chmod(temporary[1], stat.S_IRUSR)
yield temporary[1]
os.remove(temporary[1])


@contextlib.contextmanager
def temporary_project_directory(prefix="autopep8test"):
temporary = mkdtemp(prefix=prefix)
Expand Down