def test_categories(self): """Tests that all default fixers have a category assigned. This makes them more useful in tricorder, where they are all enabled. """ for fx in find_fixer.from_pattern('*').fixers: with self.subTest(fixer=fx): self.assertIsNotNone(fx._category)
def _fixer_from_pattern(pattern, templates): # templates is empty unless you pass in a --sub argument, # which doesn't make sense for this search mode. if templates: raise ValueError( 'Cannot override substitution (--sub, --named-sub) with --mode=fix' ) return find_fixer.from_pattern(pattern)
class PythonFixerTest(parameterized.TestCase): FIXER = find_fixer.from_pattern('*') @parameterized.parameters('bare_foo.py', 'foo/bar.py', 'foo/bar_test.py') def test_includes_paths(self, path): self.assertRegex(path, self.FIXER.include_regex) @parameterized.parameters( 'foo/bar', 'foo/bar_py', # "." trips people up :) 'foo/bar.pyx', # don't forget $ ) def test_excludes_paths(self, path): self.assertNotRegex(path, self.FIXER.include_regex)
def test_smoke_equivalent(self): """Tests that the test fixer conversions seem equivalent.""" for fx in find_fixer.from_pattern('*').fixers: with self.subTest(fixer=type(fx).__name__, fragment=fx.example_fragment()): # Use compile() here to test as much as possible before breaking early. # (e.g. if we can't execute the test, at least verify it parses.) # We either parse both as expressions or both as statements, so that we # can compare the return value of eval(). try: replacement = compile(fx.example_replacement(), '<replacement>', 'eval') exemplar = compile(fx.example_fragment(), '<exemplar>', 'eval') except SyntaxError: replacement = compile(fx.example_replacement(), '<replacement>', 'exec') exemplar = compile(fx.example_fragment(), '<exemplar>', 'exec') # TODO(b/130657662): Allow the fix itself to disable smoke checks, # rather than keeping this INCREDIBLY GROSS central list of opt-outs. if 'IsInstance' in fx.example_replacement(): # isinstance requires it be a type, not a mock, so we'll skip this. continue if fx._category == 'pylint.literal-comparison': # literal equality isn't equivalent to literal is, so don't check # for equivalent semantics. continue if 'has_key' in (fx._category or ''): # The has_key fix intentionally rewrites expressions. continue if 'attrib-default' in (fx._category or ''): # default= is not exactly equivalent to factory=. continue if 'mutable' in fx._message: # By definition, an immutable constant is not equivalent to a mutable # one continue if 'logging.fatal' in fx.example_fragment(): # absl.logging.fatal is dangerous, yo! continue if 'idioms.uncessary-comprehension' in (fx._category or ''): # list(x) is not completely equivalent to [a for a in x] continue m = mock.MagicMock() m.self = self self.assert_equivalent_under_mock(exemplar, replacement, m)
def test_examples_real(self): """Tests that the fixers do actually give the example replacement.""" for fx in find_fixer.from_pattern('*').fixers: example = fx.example_fragment() example_replacement = fx.example_replacement() with self.subTest(fixer=type(fx).__name__, example=example, example_replacement=example_replacement): self.assertIsNotNone(example) substitutions = list( search.find_iter(fixer.CombiningPythonFixer([fx]), example, 'a.py')) self.assertLen(substitutions, 1) replaced = formatting.apply_substitutions( example, substitutions) self.assertEqual(replaced, example_replacement)