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())
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())
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())
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:]))
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:]))
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())
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())
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())
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())
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())
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())
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:]))
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:]))
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())
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:]))
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:]))
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:]))
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())
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())