Skip to content

Commit

Permalink
style fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Бекчурин Владислав committed Oct 27, 2020
1 parent 555506a commit e391e56
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 70 deletions.
132 changes: 69 additions & 63 deletions test_unify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import contextlib
import io
import tempfile
from textwrap import dedent

try:
# Python 2.6
Expand Down Expand Up @@ -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'] = '"'

Expand Down Expand Up @@ -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'] = "'"
Expand Down Expand Up @@ -210,89 +210,93 @@ 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:
output_file = io.StringIO()
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()
non_existent_filename = '/non_existent_file_92394492929'

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',
Expand All @@ -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(
Expand All @@ -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
Expand Down
17 changes: 10 additions & 7 deletions unify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e391e56

Please sign in to comment.