Skip to content

Commit

Permalink
Merge pull request #592 from hhatto/fix-exitcode-when-occur-io-error
Browse files Browse the repository at this point in the history
fix: exit code is non-zero value when occur io error
  • Loading branch information
hhatto committed Mar 23, 2021
2 parents eb484df + eab8d9c commit da73f1c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
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

0 comments on commit da73f1c

Please sign in to comment.