diff --git a/pyformat.py b/pyformat.py index 4c3f7cd..c7c9b7c 100755 --- a/pyformat.py +++ b/pyformat.py @@ -41,26 +41,27 @@ __version__ = '0.6' -def formatters(aggressive, apply_config): +def formatters(aggressive, apply_config, filename=''): """Return list of code formatters.""" if aggressive: yield autoflake.fix_code autopep8_options = autopep8.parse_args( - [''] + int(aggressive) * ['--aggressive'], + [filename] + int(aggressive) * ['--aggressive'], apply_config=apply_config) else: - autopep8_options = autopep8.parse_args([''], apply_config=apply_config) + autopep8_options = autopep8.parse_args( + [filename], apply_config=apply_config) yield lambda code: autopep8.fix_code(code, options=autopep8_options) yield docformatter.format_code yield unify.format_code -def format_code(source, aggressive=False, apply_config=False): +def format_code(source, aggressive=False, apply_config=False, filename=''): """Return formatted source code.""" formatted_source = source - for fix in formatters(aggressive, apply_config): + for fix in formatters(aggressive, apply_config, filename): formatted_source = fix(formatted_source) return formatted_source @@ -82,7 +83,8 @@ def format_file(filename, args, standard_out): formatted_source = format_code(source, aggressive=args.aggressive, - apply_config=args.config) + apply_config=args.config, + filename=filename) if source != formatted_source: if args.in_place: diff --git a/test_pyformat.py b/test_pyformat.py index e264294..41b4980 100755 --- a/test_pyformat.py +++ b/test_pyformat.py @@ -315,6 +315,51 @@ def test_end_to_end(self): +x = 'abc' """, '\n'.join(output.decode().split('\n')[3:])) + def test_no_config(self): + self.maxDiff = None + source = """\ +x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.'] +""" + expected = """\ +@@ -1 +1,2 @@ +-x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.'] ++x = ['The limits are chosen to avoid wrapping in editors with the window', ++ 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.'] +""" + expected_no_conifg = """\ +@@ -1 +1,3 @@ +-x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.'] ++x = ['The limits are chosen to avoid wrapping in editors with the window', ++ 'width set to 80, even if the ', ++ 'tool places a marker glyph in the final column when wrapping lines.'] +""" + setup_cfg = """\ +[pep8] +max-line-length = 120 +""" + with temporary_directory() as directory: + with temporary_file(source, prefix='food', directory=directory) as filename, \ + temporary_named_file(setup_cfg, name='setup.cfg', directory=directory): + + output_file = io.StringIO() + pyformat._main(argv=['my_fake_program', + '--aggressive', + filename], + standard_out=output_file, + standard_error=None) + self.assertEqual(expected, '\n'.join( + output_file.getvalue().split('\n')[2:])) + + output_file = io.StringIO() + pyformat._main(argv=['my_fake_program', + '--aggressive', + '--no-config', + filename], + standard_out=output_file, + standard_error=None) + self.assertEqual(expected_no_conifg, '\n'.join( + output_file.getvalue().split('\n')[2:])) + @contextlib.contextmanager def temporary_file(contents, directory='.', prefix=''): @@ -329,6 +374,20 @@ def temporary_file(contents, directory='.', prefix=''): os.remove(f.name) +@contextlib.contextmanager +def temporary_named_file(contents, name, directory='.'): + """Write contents to temporary file and yield it.""" + path = os.path.join(directory, name) + try: + with open(path, 'wb') as f: + f.write(contents.encode()) + f.close() + yield path + finally: + if os.path.exists(path): + os.remove(path) + + @contextlib.contextmanager def temporary_directory(directory='.', prefix=''): """Create temporary directory and yield its path."""