From e391e56a549ce50355688d83b099d03c7dfaabd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B5=D0=BA=D1=87=D1=83=D1=80=D0=B8=D0=BD=20=D0=92?= =?UTF-8?q?=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2?= Date: Tue, 27 Oct 2020 10:05:33 +0500 Subject: [PATCH] style fixes. --- test_unify.py | 132 ++++++++++++++++++++++++++------------------------ unify.py | 17 ++++--- 2 files changed, 79 insertions(+), 70 deletions(-) diff --git a/test_unify.py b/test_unify.py index e1d7650..889df60 100755 --- a/test_unify.py +++ b/test_unify.py @@ -7,6 +7,7 @@ import contextlib import io import tempfile +from textwrap import dedent try: # Python 2.6 @@ -37,7 +38,6 @@ def test_preferred_single(self): result = unify.format_code('b"foo"') self.assertEqual(result, "b'foo'") - def test_preferred_double(self): unify.rules['preferred_quote'] = '"' @@ -145,12 +145,12 @@ def test_no_change(self): result = unify.format_code('''b"""foo"""''') self.assertEqual(result, '''b"""foo"""''') + class TestUnitsCode(unittest.TestCase): def test_detect_encoding_with_bad_encoding(self): with temporary_file('# -*- coding: blah -*-\n') as filename: - self.assertEqual('latin-1', - unify.detect_encoding(filename)) + self.assertEqual('latin-1', unify.detect_encoding(filename)) def test_format_code(self): unify.rules['preferred_quote'] = "'" @@ -210,42 +210,46 @@ def test_format_code_with_syntax_error(self): class TestSystem(unittest.TestCase): def test_diff(self): - with temporary_file('''\ -if True: - x = "abc" -''') as filename: + with temporary_file(dedent('''\ + if True: + x = "abc" + ''')) as filename: output_file = io.StringIO() self.assertEqual( unify._main(argv=['my_fake_program', filename], standard_out=output_file, standard_error=None), - None, - ) - self.assertEqual('''\ -@@ -1,2 +1,2 @@ - if True: -- x = "abc" -+ x = 'abc' -''', '\n'.join(output_file.getvalue().split('\n')[2:])) + None) + + self.assertEqual( + dedent('''\ + @@ -1,2 +1,2 @@ + if True: + - x = "abc" + + x = 'abc' + '''), + '\n'.join(output_file.getvalue().split('\n')[2:])) def test_check_only(self): - with temporary_file('''\ -if True: - x = "abc" -''') as filename: + with temporary_file(dedent('''\ + if True: + x = "abc" + ''')) as filename: output_file = io.StringIO() self.assertEqual( unify._main(argv=['my_fake_program', '--check-only', filename], standard_out=output_file, standard_error=None), - 1, - ) - self.assertEqual('''\ -@@ -1,2 +1,2 @@ - if True: -- x = "abc" -+ x = 'abc' -''', '\n'.join(output_file.getvalue().split('\n')[2:])) + 1) + + self.assertEqual( + dedent('''\ + @@ -1,2 +1,2 @@ + if True: + - x = "abc" + + x = 'abc' + '''), + '\n'.join(output_file.getvalue().split('\n')[2:])) def test_diff_with_empty_file(self): with temporary_file('') as filename: @@ -253,9 +257,7 @@ def test_diff_with_empty_file(self): unify._main(argv=['my_fake_program', filename], standard_out=output_file, standard_error=None) - self.assertEqual( - '', - output_file.getvalue()) + self.assertEqual('', output_file.getvalue()) def test_diff_with_missing_file(self): output_file = io.StringIO() @@ -263,36 +265,38 @@ def test_diff_with_missing_file(self): self.assertEqual( 1, - unify._main(argv=['my_fake_program', - '/non_existent_file_92394492929'], - standard_out=None, - standard_error=output_file)) + unify._main( + argv=['my_fake_program', '/non_existent_file_92394492929'], + standard_out=None, + standard_error=output_file)) self.assertIn(non_existent_filename, output_file.getvalue()) def test_in_place(self): - with temporary_file('''\ -if True: - x = "abc" -''') as filename: + with temporary_file(dedent('''\ + if True: + x = "abc" + ''')) as filename: output_file = io.StringIO() self.assertEqual( unify._main(argv=['my_fake_program', '--in-place', filename], standard_out=output_file, standard_error=None), - None, - ) + None) + with open(filename) as f: - self.assertEqual('''\ -if True: - x = 'abc' -''', f.read()) + self.assertEqual( + dedent('''\ + if True: + x = 'abc' + '''), + f.read()) def test_in_place_precedence_over_check_only(self): - with temporary_file('''\ -if True: - x = "abc" -''') as filename: + with temporary_file(dedent('''\ + if True: + x = "abc" + ''')) as filename: output_file = io.StringIO() self.assertEqual( unify._main(argv=['my_fake_program', @@ -301,23 +305,27 @@ def test_in_place_precedence_over_check_only(self): filename], standard_out=output_file, standard_error=None), - None, - ) + None) + with open(filename) as f: - self.assertEqual('''\ -if True: - x = 'abc' -''', f.read()) + self.assertEqual( + dedent('''\ + if True: + x = 'abc' + '''), + f.read()) def test_ignore_hidden_directories(self): with temporary_directory() as directory: with temporary_directory(prefix='.', directory=directory) as inner_directory: - with temporary_file("""\ -if True: - x = "abc" -""", directory=inner_directory): + with temporary_file( + dedent("""\ + if True: + x = "abc" + """), + directory=inner_directory): output_file = io.StringIO() self.assertEqual( @@ -326,11 +334,9 @@ def test_ignore_hidden_directories(self): directory], standard_out=output_file, standard_error=None), - None, - ) - self.assertEqual( - '', - output_file.getvalue().strip()) + None) + + self.assertEqual('', output_file.getvalue().strip()) @contextlib.contextmanager diff --git a/unify.py b/unify.py index 24734d7..89e297f 100755 --- a/unify.py +++ b/unify.py @@ -175,6 +175,7 @@ def _drop_escape_bs(string): body = ''.join(splitted_body) return body + class SimpleEscapeFstring(SimpleEscapeString): """ F-string with one type of quote in body. @@ -219,8 +220,7 @@ def _format_code(source): editable_string.reformat() token_string = editable_string.token - modified_tokens.append( - (token_type, token_string, start, end, line)) + modified_tokens.append((token_type, token_string, start, end, line)) return untokenize.untokenize(modified_tokens) @@ -348,11 +348,14 @@ def _main(argv, standard_out, standard_error): name = filenames.pop(0) if args.recursive and os.path.isdir(name): for root, directories, children in os.walk(unicode(name)): - filenames += [os.path.join(root, f) for f in children - if f.endswith('.py') and - not f.startswith('.')] - directories[:] = [d for d in directories - if not d.startswith('.')] + filenames += [ + os.path.join(root, f) for f in children + if f.endswith('.py') and not f.startswith('.') + ] + + directories[:] = [ + d for d in directories if not d.startswith('.') + ] else: try: if format_file(name, args=args, standard_out=standard_out):