def test_with_missing_file(self): output_file = io.StringIO() ignore = StubFile() autoflake._main(argv=['my_fake_program', '--in-place', '.fake'], standard_out=output_file, standard_error=ignore) self.assertFalse(output_file.getvalue())
def test_in_place_with_with_useless_pass(self): with temporary_file("""\ import foo x = foo import subprocess x() try: pass import os except ImportError: pass import os import sys """) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--in-place', filename], standard_out=output_file, standard_error=None) with open(filename) as f: self.assertEqual( """\ import foo x = foo x() try: pass except ImportError: pass """, f.read())
def test_in_place_with_with_useless_pass(self): with temporary_file("""\ import foo x = foo import subprocess x() try: pass import os except ImportError: pass import os import sys """) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--in-place', filename], standard_out=output_file, standard_error=None) with open(filename) as f: self.assertEqual("""\ import foo x = foo x() try: pass except ImportError: pass """, f.read())
def test_redundant_options(self): output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--remove-all', '--imports=blah', __file__], standard_out=output_file, standard_error=output_file) self.assertIn('redundant', output_file.getvalue())
def test_check_with_empty_file(self): line = '' with temporary_file(line) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--check', filename], standard_out=output_file, standard_error=None) self.assertEqual('No issues detected!', output_file.getvalue())
def test_in_place_with_empty_file(self): line = '' with temporary_file(line) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--in-place', filename], standard_out=output_file, standard_error=None) with open(filename) as f: self.assertEqual(line, f.read())
def test_check_correct_file(self): with temporary_file("""\ import foo x = foo.bar print(x) """) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--check', filename], standard_out=output_file, standard_error=None) self.assertEqual('No issues detected!', output_file.getvalue())
def test_diff(self): with temporary_file("""\ import re import os import my_own_module x = 1 """) as filename: output_file = io.StringIO() autoflake._main(argv=['my_fake_program', filename], standard_out=output_file, standard_error=None) self.assertEqual("""\ -import re -import os import my_own_module x = 1 """, '\n'.join(output_file.getvalue().split('\n')[3:]))
def test_ignore_hidden_directories(self): with temporary_directory() as directory: with temporary_directory(prefix='.', directory=directory) as inner_directory: with temporary_file("""\ import re import os """, directory=inner_directory): output_file = io.StringIO() autoflake._main( argv=['my_fake_program', '--recursive', directory], standard_out=output_file, standard_error=None) self.assertEqual('', output_file.getvalue().strip())
def test_ignore_hidden_directories(self): with temporary_directory() as directory: with temporary_directory(prefix='.', directory=directory) as inner_directory: with temporary_file("""\ import re import os """, directory=inner_directory): output_file = io.StringIO() autoflake._main(argv=['my_fake_program', '--recursive', directory], standard_out=output_file, standard_error=None) self.assertEqual( '', output_file.getvalue().strip())
def test_check_useless_pass(self): with temporary_file("""\ import foo x = foo import subprocess x() try: pass import os except ImportError: pass import os import sys """) as filename: output_file = io.StringIO() with self.assertRaises(SystemExit) as cm: autoflake._main(argv=['my_fake_program', '--check', filename], standard_out=output_file, standard_error=None) self.assertEqual(cm.exception.code, 1) self.assertEqual('Unused imports/variables detected.', output_file.getvalue())
def test_diff_with_nonexistent_file(self): output_file = io.StringIO() autoflake._main(argv=['my_fake_program', 'nonexistent_file'], standard_out=output_file, standard_error=output_file) self.assertIn('no such file', output_file.getvalue().lower())