示例#1
0
 def test_diff_with_empty_file(self):
     with temporary_file('') as filename:
         output_file = io.StringIO()
         pyformat._main(argv=['my_fake_program', filename],
                        standard_out=output_file,
                        standard_error=None)
         self.assertEqual('', output_file.getvalue())
示例#2
0
    def test_multiple_jobs_should_require_in_place(self):
        output_file = io.StringIO()
        pyformat._main(argv=['my_fake_program',
                             '--jobs=2', __file__],
                       standard_out=output_file,
                       standard_error=output_file)

        self.assertIn('requires --in-place', output_file.getvalue())
示例#3
0
    def test_in_place(self):
        with temporary_file('''\
if True:
    x = "abc"
''') as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--in-place', filename],
                           standard_out=output_file,
                           standard_error=None)
            with open(filename) as f:
                self.assertEqual('''\
if True:
    x = 'abc'
''', f.read())
示例#4
0
    def test_in_place(self):
        with temporary_file('''\
if True:
    x = "abc"
''') as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--in-place', filename],
                           standard_out=output_file,
                           standard_error=None)
            with open(filename) as f:
                self.assertEqual('''\
if True:
    x = 'abc'
''', f.read())
示例#5
0
    def test_diff_with_aggressive(self):
        with temporary_file('''\
import os
x = "abc"
''') as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--aggressive', filename],
                           standard_out=output_file,
                           standard_error=None)
            self.assertEqual('''\
@@ -1,2 +1 @@
-import os
-x = "abc"
+x = 'abc'
''', '\n'.join(output_file.getvalue().split('\n')[2:]))
示例#6
0
    def test_diff(self):
        with temporary_file('''\
import os
x = "abc"
''') as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--diff', filename],
                           standard_out=output_file,
                           standard_error=None)
            self.assertEqual(
                '''\
@@ -1,2 +1,2 @@
 import os
-x = "abc"
+x = 'abc'
''', '\n'.join(output_file.getvalue().split('\n')[2:]))
示例#7
0
    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):

                    output_file = io.StringIO()
                    pyformat._main(
                        argv=['my_fake_program', '--recursive', directory],
                        standard_out=output_file,
                        standard_error=None)
                    self.assertEqual('', output_file.getvalue().strip())
示例#8
0
    def test_exclude(self):
        with temporary_directory() as directory:
            with temporary_file("""\
if True:
    x = "abc"
""",
                                prefix='food',
                                directory=directory):

                output_file = io.StringIO()
                pyformat._main(argv=[
                    'my_fake_program', '--recursive', '--exclude=zap',
                    '--exclude=*oo*', directory
                ],
                               standard_out=output_file,
                               standard_error=None)
                self.assertEqual('', output_file.getvalue().strip())
示例#9
0
    def test_multiple_jobs_should_require_in_place(self):
        output_file = io.StringIO()
        self.assertEqual(
            2,
            pyformat._main(argv=['my_fake_program', '--jobs=2', __file__],
                           standard_out=output_file,
                           standard_error=output_file))

        self.assertIn('requires --in-place', output_file.getvalue())
示例#10
0
    def test_exclude(self):
        with temporary_directory() as directory:
            with temporary_file("""\
if True:
    x = "abc"
""", prefix='food', directory=directory):

                output_file = io.StringIO()
                pyformat._main(argv=['my_fake_program',
                                     '--recursive',
                                     '--exclude=zap',
                                     '--exclude=*oo*',
                                     directory],
                               standard_out=output_file,
                               standard_error=None)
                self.assertEqual(
                    '',
                    output_file.getvalue().strip())
示例#11
0
    def test_remove_unused_variables(self):
        with temporary_file("""\
def test():
    x = 43
    return 42
""") as filename:
            output_file = io.StringIO()
            pyformat._main(argv=[
                'my_fake_program', '--in-place', '--aggressive',
                '--remove-unused-variables', filename
            ],
                           standard_out=output_file,
                           standard_error=None)
            with open(filename) as f:
                self.assertEqual('''\
def test():
    return 42
''', f.read())
示例#12
0
    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):

                    output_file = io.StringIO()
                    pyformat._main(argv=['my_fake_program',
                                         '--recursive',
                                         directory],
                                   standard_out=output_file,
                                   standard_error=None)
                    self.assertEqual(
                        '',
                        output_file.getvalue().strip())
示例#13
0
    def test_diff_with_encoding_declaration(self):
        with temporary_file("""\
# coding: utf-8
import re
import os
import my_own_module
x = 1
""") as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--aggressive', filename],
                           standard_out=output_file,
                           standard_error=None)
            self.assertEqual("""\
 # coding: utf-8
-import re
-import os
 import my_own_module
 x = 1
""", '\n'.join(output_file.getvalue().split('\n')[3:]))
示例#14
0
    def test_diff_with_encoding_declaration(self):
        with temporary_file("""\
# coding: utf-8
import re
import os
import my_own_module
x = 1
""") as filename:
            output_file = io.StringIO()
            pyformat._main(argv=['my_fake_program', '--aggressive', filename],
                           standard_out=output_file,
                           standard_error=None)
            self.assertEqual(
                """\
 # coding: utf-8
-import re
-import os
 import my_own_module
 x = 1
""", '\n'.join(output_file.getvalue().split('\n')[3:]))
示例#15
0
    def test_remove_unused_variables_requires_aggressive(self):
        output_file = io.StringIO()
        self.assertEqual(
            2,
            pyformat._main(argv=[
                'my_fake_program', '--remove-unused-variables', __file__
            ],
                           standard_out=output_file,
                           standard_error=output_file))

        self.assertIn('requires --aggressive', output_file.getvalue())
示例#16
0
    def test_recursive(self):
        with temporary_directory() as directory:
            with temporary_file("""\
if True:
    x = "abc"
""", prefix='food', directory=directory):

                output_file = io.StringIO()
                pyformat._main(argv=['my_fake_program',
                                     '--recursive',
                                     '--exclude=zap',
                                     '--exclude=x*oo*',
                                     directory],
                               standard_out=output_file,
                               standard_error=None)
                self.assertEqual("""\
@@ -1,2 +1,2 @@
 if True:
-    x = "abc"
+    x = 'abc'
""", '\n'.join(output_file.getvalue().split('\n')[2:]))
示例#17
0
    def test_no_config(self):
        source = """\
x =1
"""
        expected = ''
        expected_no_config = """\
@@ -1 +1 @@
-x =1
+x = 1
"""
        setup_cfg = """\
[pep8]
ignore=E
"""
        with temporary_directory() as directory:
            with temporary_file(source, directory=directory) as filename:

                with open(os.path.join(directory, 'setup.cfg'),
                          'w') as setup_file:
                    setup_file.write(setup_cfg)

                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', '--diff', '--aggressive', '--no-config',
                    filename
                ],
                               standard_out=output_file,
                               standard_error=None)
                self.assertEqual(
                    expected_no_config,
                    '\n'.join(output_file.getvalue().split('\n')[2:]))
示例#18
0
    def test_recursive(self):
        with temporary_directory() as directory:
            with temporary_file("""\
if True:
    x = "abc"
""",
                                prefix='food',
                                directory=directory):

                output_file = io.StringIO()
                pyformat._main(argv=[
                    'my_fake_program', '--recursive', '--exclude=zap',
                    '--exclude=x*oo*', '--diff', directory
                ],
                               standard_out=output_file,
                               standard_error=None)
                self.assertEqual(
                    """\
@@ -1,2 +1,2 @@
 if True:
-    x = "abc"
+    x = 'abc'
""", '\n'.join(output_file.getvalue().split('\n')[2:]))
示例#19
0
 def test_diff_with_nonexistent_file(self):
     output_file = io.StringIO()
     pyformat._main(argv=['my_fake_program', 'nonexistent_file'],
                    standard_out=output_file,
                    standard_error=output_file)
     self.assertIn('no such file', output_file.getvalue().lower())
示例#20
0
 def test_verbose(self):
     output_file = io.StringIO()
     pyformat._main(argv=['my_fake_program', '--verbose', __file__],
                    standard_out=output_file,
                    standard_error=output_file)
     self.assertIn('.py', output_file.getvalue())
示例#21
0
 def test_diff_with_nonexistent_file(self):
     output_file = io.StringIO()
     pyformat._main(argv=['my_fake_program', 'nonexistent_file'],
                    standard_out=output_file,
                    standard_error=output_file)
     self.assertIn('no such file', output_file.getvalue().lower())
示例#22
0
 def test_verbose(self):
     output_file = io.StringIO()
     pyformat._main(argv=['my_fake_program', '--verbose', __file__],
                    standard_out=output_file,
                    standard_error=output_file)
     self.assertIn('.py', output_file.getvalue())