def test_should_add_imports_with_no_prior_imports(self): filename = self.create_file("foo.py", "obj = json.dumps(23)") (change, ) = autoimport.get_changes(filename) self.assertEqual(change['action'], 'change') self.assertEqual(change['file'], filename) self.assertEqual(change['contents'], "import json\n" "obj = json.dumps(23)")
def test_should_add_imports_with_no_prior_imports(self): filename = self.create_file("foo.py", "conf = ConfigParser.SafeConfigParser()") (change,) = autoimport.get_changes(filename) self.assertEqual(change['action'], 'change') self.assertEqual(change['file'], filename) self.assertEqual(change['contents'], "import ConfigParser\n" "conf = ConfigParser.SafeConfigParser()")
def test_should_add_imports_with_no_prior_imports(self): filename = self.create_file("foo.py", "obj = json.dumps(23)") (change,) = autoimport.get_changes(filename) self.assertEqual(change['action'], 'change') self.assertEqual(change['file'], filename) self.assertEqual(change['contents'], "import json\n" "obj = json.dumps(23)")
def test_should_not_add_duplicates(self): filename = self.create_file( "foo.py", "if sys.argv[1]:\n" " sys.exit('yiakes')\n") (change, ) = autoimport.get_changes(filename) self.assertEqual(change['action'], 'change') self.assertEqual(change['file'], filename) self.assertEqual( change['contents'], "import sys\n" "if sys.argv[1]:\n" " sys.exit('yiakes')\n")
def test_should_not_add_duplicates(self): filename = self.create_file("foo.py", "if sys.argv[1]:\n" " sys.exit('yiakes')\n") (change,) = autoimport.get_changes(filename) self.assertEqual(change['action'], 'change') self.assertEqual(change['file'], filename) self.assertEqual(change['contents'], "import sys\n" "if sys.argv[1]:\n" " sys.exit('yiakes')\n")
def test_should_do_nothing_for_global_names(self): filename = self.create_file("foo.py", "open('foo').read()") self.assertEqual(autoimport.get_changes(filename), [])
def refactor_add_missing_imports(self): return autoimport.get_changes(self.resource.real_path)