Ejemplo n.º 1
0
 def test_renaming_constant(self):
     self.write_file('foo.py', 'CACHE = {}\n')
     slicker.make_fixes(['foo.CACHE'],
                        'foo._SECRET_CACHE',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py', '_SECRET_CACHE = {}\n')
     self.assertFalse(self.error_output)
Ejemplo n.º 2
0
    def assert_(self,
                old_module,
                new_module,
                old_string,
                new_string,
                alias=None):
        """Assert that a file that imports old_module rewrites its strings too.

        We create a temp file that imports old_module as alias, and then
        defines a docstring with the contents old_string.  We then rename
        old_module to new_module, and make sure that our temp file not
        only has the import renamed, it has the string renamed as well.
        """
        self.write_file(old_module.replace('.', os.sep) + '.py', '# A file')
        self.write_file(
            'in.py', '"""%s"""\n%s\n\n_ = %s.myfunc()\n' %
            (old_string,
             model.Import(old_module, alias or old_module, 'absolute', None,
                          None).import_stmt(), alias or old_module))

        slicker.make_fixes([old_module],
                           new_module,
                           project_root=self.tmpdir,
                           automove=False)
        self.assertFalse(self.error_output)

        expected = ('"""%s"""\nimport %s\n\n_ = %s.myfunc()\n' %
                    (new_string, new_module, new_module))
        with open(self.join('in.py')) as f:
            actual = f.read()
        self.assertMultiLineEqual(expected, actual)
Ejemplo n.º 3
0
 def test_renaming_old_style_class(self):
     self.write_file('foo.py', ('class Classy:\n' '    return 17\n'))
     slicker.make_fixes(['foo.Classy'],
                        'foo.Classier',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py', ('class Classier:\n' '    return 17\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 4
0
 def test_move_function(self):
     self.write_file('foo.py', 'def myfunc(): return 17\n')
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py', 'def myfunc(): return 17\n')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 5
0
 def test_move_module_within_directory(self):
     self.write_file('foo.py', 'def myfunc(): return 4\n')
     self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
     slicker.make_fixes(['foo'], 'baz', project_root=self.tmpdir)
     self.assertFileIs('baz.py', 'def myfunc(): return 4\n')
     self.assertFileIs('bar.py', 'import baz\n\nbaz.myfunc()\n')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 6
0
 def test_renaming_function(self):
     self.write_file('foo.py', ('def myfunc():\n' '    return 17\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'foo.mybetterfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py', ('def mybetterfunc():\n'
                                  '    return 17\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 7
0
 def test_move_constant(self):
     self.write_file('foo.py', 'CACHE = {}\n')
     slicker.make_fixes(['foo.CACHE'],
                        'newfoo.CACHE',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py', 'CACHE = {}\n')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 8
0
 def test_move_class(self):
     self.write_file('foo.py', 'class Classy(object): return 17\n')
     slicker.make_fixes(['foo.Classy'],
                        'newfoo.Classy',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py', 'class Classy(object): return 17\n')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 9
0
 def test_move_to_a_new_dir(self):
     self.write_file('foo.py', 'CACHE = {}\n')
     slicker.make_fixes(['foo.CACHE'],
                        'newfoo.bar.CACHE',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo/bar.py', 'CACHE = {}\n')
     self.assertFileIs('newfoo/__init__.py', '')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 10
0
 def test_move_module_out_of_a_directory(self):
     self.write_file('foo/__init__.py', '')
     self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
     self.write_file('baz.py', 'import foo.bar\n\nfoo.bar.myfunc()\n')
     slicker.make_fixes(['foo.bar'], 'bang', project_root=self.tmpdir)
     self.assertFileIs('bang.py', 'def myfunc(): return 4\n')
     self.assertFileIs('baz.py', 'import bang\n\nbang.myfunc()\n')
     self.assertFileIsNot('foo/bar.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 11
0
 def test_move_module_to_directory_with_a_nonempty_init_py(self):
     self.write_file('foo.py', 'def myfunc(): return 4\n')
     self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
     self.write_file('baz/__init__.py', '# Non-empty init.py\n')
     slicker.make_fixes(['foo'], 'baz.bang', project_root=self.tmpdir)
     self.assertFileIs('baz/bang.py', 'def myfunc(): return 4\n')
     self.assertFileIs('baz/__init__.py', '# Non-empty init.py\n')
     self.assertFileIs('bar.py', 'import baz.bang\n\nbaz.bang.myfunc()\n')
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 12
0
 def test_renaming_decorated_class(self):
     self.write_file('foo.py', ('@decorator\n'
                                'class Classy(object):\n'
                                '    return 17\n'))
     slicker.make_fixes(['foo.Classy'],
                        'foo.Classier',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py', ('@decorator\n'
                                  'class Classier(object):\n'
                                  '    return 17\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 13
0
 def test_move_package_to_existing_name(self):
     self.write_file('foo/__init__.py', '')
     self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
     self.write_file('foo/baz.py', 'def myfunc(): return 5\n')
     self.write_file('qux/__init__.py', '')
     slicker.make_fixes(['foo'], 'qux', project_root=self.tmpdir)
     self.assertFileIs('qux/__init__.py', '')
     self.assertFileIs('qux/foo/__init__.py', '')
     self.assertFileIs('qux/foo/bar.py', 'def myfunc(): return 4\n')
     self.assertFileIs('qux/foo/baz.py', 'def myfunc(): return 5\n')
     self.assertFalse(self.error_output)
Ejemplo n.º 14
0
 def test_rename_and_move(self):
     self.write_file('foo.py', ('# a class.\n'
                                'class Classy(object):\n'
                                '    return 17\n'))
     slicker.make_fixes(['foo.Classy'],
                        'newfoo.Classier',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py', ('# a class.\n'
                                     'class Classier(object):\n'
                                     '    return 17\n'))
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 15
0
 def test_removes_remaining_future_import(self):
     self.write_file('foo.py', ('from __future__ import absolute_import\n\n'
                                'import bar\n\n\n'
                                'def myfunc():\n'
                                '    return bar.unrelated_function()\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIsNot('foo.py')
     self.assertFileIs('newfoo.py',
                       ('from __future__ import absolute_import\n\n'
                        'import bar\n\n\n'
                        'def myfunc():\n'
                        '    return bar.unrelated_function()\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 16
0
 def test_dash_f_flag(self):
     self.write_file('foo.py', 'def myfunc(): return 4\n')
     self.write_file('faa.py', 'def myfaa(): return 4\n')
     self.write_file('bar.py', ('import foo\nimport faa\n\n'
                                'foo.myfunc()\nfaa.myfaa()\n'))
     slicker.make_fixes(['foo', 'faa'],
                        self.join('baz/'),
                        import_alias='FROM',
                        project_root=self.tmpdir)
     self.assertFileIs('baz/foo.py', 'def myfunc(): return 4\n')
     self.assertFileIs('baz/faa.py', 'def myfaa(): return 4\n')
     self.assertFileIs('baz/__init__.py', '')
     self.assertFileIs('bar.py',
                       ('from baz import faa\nfrom baz import foo\n\n'
                        'foo.myfunc()\nfaa.myfaa()\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 17
0
    def test_third_party_sorting(self):
        self.copy_file('third_party_sorting_in.py')

        os.mkdir(self.join('third_party'))
        for f in ('mycode1.py', 'mycode2.py', 'third_party/__init__.py',
                  'third_party/slicker.py'):
            with open(self.join(f), 'w') as f:
                print >> f, '# A file'

        slicker.make_fixes(['third_party_sorting_in'],
                           'out',
                           project_root=self.tmpdir)

        with open(self.join('out.py')) as f:
            actual = f.read()
        with open('testdata/third_party_sorting_out.py') as f:
            expected = f.read()
        self.assertMultiLineEqual(expected, actual)
        self.assertFalse(self.error_output)
Ejemplo n.º 18
0
 def test_moving_with_context(self):
     self.write_file('foo.py',
                     ('"""A file with the old version of foo."""\n'
                      'import quux\n\n'
                      'def _secretfunc():\n'
                      '    return quux.secretmonkeys\n\n\n'
                      '# Does some stuff\n'
                      '# Be careful calling it!\n\n'
                      'def myfunc():\n'
                      '    """Returns a number."""\n'
                      '    return 289\n\n\n'
                      '# Here is another function.\n'
                      'def otherfunc():\n'
                      '    return 1 + 1\n'))
     self.write_file('newfoo.py',
                     ('"""A file with the new version of foo."""\n'
                      'import quux\n\n'
                      'def otherfunc():\n'
                      '    return quux.value\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py',
                       ('"""A file with the new version of foo."""\n'
                        'import quux\n\n'
                        'def otherfunc():\n'
                        '    return quux.value\n\n\n'
                        '# Does some stuff\n'
                        '# Be careful calling it!\n\n'
                        'def myfunc():\n'
                        '    """Returns a number."""\n'
                        '    return 289\n'))
     self.assertFileIs('foo.py',
                       ('"""A file with the old version of foo."""\n'
                        'import quux\n\n'
                        'def _secretfunc():\n'
                        '    return quux.secretmonkeys\n\n\n'
                        '# Here is another function.\n'
                        'def otherfunc():\n'
                        '    return 1 + 1\n'))
     self.assertFalse(self.error_output)
Ejemplo n.º 19
0
 def test_warns_remaining_docstring(self):
     self.write_file('foo.py', ('"""This file frobnicates the doodad."""\n'
                                'from __future__ import absolute_import\n\n'
                                'import bar\n\n\n'
                                'def myfunc():\n'
                                '    return bar.unrelated_function()\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py',
                       ('"""This file frobnicates the doodad."""\n'
                        'from __future__ import absolute_import\n\n'))
     self.assertFileIs('newfoo.py',
                       ('from __future__ import absolute_import\n\n'
                        'import bar\n\n\n'
                        'def myfunc():\n'
                        '    return bar.unrelated_function()\n'))
     self.assertEqual(self.error_output, [
         'WARNING:This file looks mostly empty; consider removing it.'
         '\n    on foo.py:1 --> """This file frobnicates the doodad."""'
     ])
Ejemplo n.º 20
0
 def test_warns_remaining_comment(self):
     self.write_file('foo.py', ('# this comment is very important!!!!!111\n'
                                'from __future__ import absolute_import\n\n'
                                'import bar\n\n\n'
                                'def myfunc():\n'
                                '    return bar.unrelated_function()\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py',
                       ('# this comment is very important!!!!!111\n'
                        'from __future__ import absolute_import\n\n'))
     self.assertFileIs('newfoo.py',
                       ('from __future__ import absolute_import\n\n'
                        'import bar\n\n\n'
                        'def myfunc():\n'
                        '    return bar.unrelated_function()\n'))
     self.assertEqual(self.error_output, [
         'WARNING:This file looks mostly empty; consider removing it.'
         '\n    on foo.py:1 --> # this comment is very important!!!!!111'
     ])
Ejemplo n.º 21
0
 def test_warns_remaining_import(self):
     self.write_file('foo.py', ('from __future__ import absolute_import\n\n'
                                'import asdf  # @UnusedImport\n'
                                'import bar\n\n\n'
                                'def myfunc():\n'
                                '    return bar.unrelated_function()\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('foo.py',
                       ('from __future__ import absolute_import\n\n'
                        'import asdf  # @UnusedImport\n'))
     self.assertFileIs('newfoo.py',
                       ('from __future__ import absolute_import\n\n'
                        'import bar\n\n\n'
                        'def myfunc():\n'
                        '    return bar.unrelated_function()\n'))
     self.assertEqual(
         self.error_output,
         [('WARNING:Not removing import with @Nolint.'
           '\n    on foo.py:3 --> import asdf  # @UnusedImport'),
          ('WARNING:This file looks mostly empty; consider removing it.'
           '\n    on foo.py:1 --> from __future__ import absolute_import')])
Ejemplo n.º 22
0
 def test_move_package(self):
     self.write_file('foo/__init__.py', '')
     self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
     self.write_file('foo/baz.py', 'def myfunc(): return 5\n')
     self.write_file('foo/bang/__init__.py', '')
     self.write_file('foo/bang/qux.py', 'qux = True\n')
     self.write_file('toplevel.py', ('import foo.bar\nimport foo.baz\n'
                                     'import foo.bang.qux\n\n'
                                     'return foo.bar.val + foo.baz.val +'
                                     ' foo.bang.qux.qux\n'))
     slicker.make_fixes(['foo'], 'newfoo', project_root=self.tmpdir)
     self.assertFileIs('newfoo/__init__.py', '')
     self.assertFileIs('newfoo/bar.py', 'def myfunc(): return 4\n')
     self.assertFileIs('newfoo/baz.py', 'def myfunc(): return 5\n')
     self.assertFileIs('newfoo/bang/__init__.py', '')
     self.assertFileIs('newfoo/bang/qux.py', 'qux = True\n')
     self.assertFileIs('toplevel.py',
                       ('import newfoo.bang.qux\nimport newfoo.bar\n'
                        'import newfoo.baz\n\n'
                        'return newfoo.bar.val + newfoo.baz.val +'
                        ' newfoo.bang.qux.qux\n'))
     self.assertFileIsNot('foo/bar.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 23
0
 def test_appending_to_existing_file(self):
     # Note since myfunc is at the top of foo.py, and there's only one
     # newline at the bottom of newfoo.py, this tests the case where we add
     # newlines.
     self.write_file('foo.py', 'def myfunc(): return 17\n')
     self.write_file(
         'newfoo.py',
         (
             '"""A file with the new version of foo."""\n'
             'import quux\n\n'
             'def otherfunc():\n'
             # Make sure that extra newline won't mess us up:
             '    return 71\n\n'))
     slicker.make_fixes(['foo.myfunc'],
                        'newfoo.myfunc',
                        project_root=self.tmpdir)
     self.assertFileIs('newfoo.py',
                       ('"""A file with the new version of foo."""\n'
                        'import quux\n\n'
                        'def otherfunc():\n'
                        '    return 71\n\n\n'
                        'def myfunc(): return 17\n'))
     self.assertFileIsNot('foo.py')
     self.assertFalse(self.error_output)
Ejemplo n.º 24
0
 def test_keep_permissions(self):
     self.write_file('foo.py', 'def myfunc(): return 4\n')
     os.chmod(self.join('foo.py'), 0o755)
     slicker.make_fixes(['foo'], 'baz', project_root=self.tmpdir)
     self.assertEqual(0o755,
                      stat.S_IMODE(os.stat(self.join('baz.py')).st_mode))
Ejemplo n.º 25
0
 def test_move_module_to_existing_name(self):
     self.write_file('foo.py', 'def myfunc(): return 4\n')
     self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
     with self.assertRaises(ValueError):
         slicker.make_fixes(['foo'], 'bar', project_root=self.tmpdir)