From 3478c007c7c012c757b126a6f64f166c8b2d0935 Mon Sep 17 00:00:00 2001 From: "Evgeny V. Generalov" Date: Fri, 4 Dec 2015 09:59:14 +0500 Subject: [PATCH 1/2] added test for --no-config option --- test_pyformat.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test_pyformat.py b/test_pyformat.py index e264294..9b0526d 100755 --- a/test_pyformat.py +++ b/test_pyformat.py @@ -315,6 +315,52 @@ 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), \ + change_cwd(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 +375,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.""" @@ -339,5 +399,32 @@ def temporary_directory(directory='.', prefix=''): shutil.rmtree(temp_directory) +@contextlib.contextmanager +def change_cwd(path, quiet=False): + """Return a context manager that changes the current working directory. + + Arguments: + + path: the directory to use as the temporary current working directory. + + quiet: if False (the default), the context manager raises an exception + on error. Otherwise, it issues only a warning and keeps the current + working directory the same. + + """ + saved_dir = os.getcwd() + try: + os.chdir(path) + except OSError: + if not quiet: + raise + warnings.warn('tests may fail, unable to change CWD to: ' + path, + RuntimeWarning, stacklevel=3) + try: + yield os.getcwd() + finally: + os.chdir(saved_dir) + + if __name__ == '__main__': unittest.main() From 8d99648ba53981d5c74db3905413b564c9ff3f40 Mon Sep 17 00:00:00 2001 From: "Evgeny V. Generalov" Date: Sat, 5 Dec 2015 19:01:51 +0500 Subject: [PATCH 2/2] Pass filename to autopep8 formatter autopep8 search the local configuration files in the filesystem by file path. --- pyformat.py | 14 ++++++++------ test_pyformat.py | 30 +----------------------------- 2 files changed, 9 insertions(+), 35 deletions(-) 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 9b0526d..41b4980 100755 --- a/test_pyformat.py +++ b/test_pyformat.py @@ -339,8 +339,7 @@ def test_no_config(self): """ 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), \ - change_cwd(directory): + temporary_named_file(setup_cfg, name='setup.cfg', directory=directory): output_file = io.StringIO() pyformat._main(argv=['my_fake_program', @@ -399,32 +398,5 @@ def temporary_directory(directory='.', prefix=''): shutil.rmtree(temp_directory) -@contextlib.contextmanager -def change_cwd(path, quiet=False): - """Return a context manager that changes the current working directory. - - Arguments: - - path: the directory to use as the temporary current working directory. - - quiet: if False (the default), the context manager raises an exception - on error. Otherwise, it issues only a warning and keeps the current - working directory the same. - - """ - saved_dir = os.getcwd() - try: - os.chdir(path) - except OSError: - if not quiet: - raise - warnings.warn('tests may fail, unable to change CWD to: ' + path, - RuntimeWarning, stacklevel=3) - try: - yield os.getcwd() - finally: - os.chdir(saved_dir) - - if __name__ == '__main__': unittest.main()