-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc97005
commit 7753dc8
Showing
5 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.egg-info | ||
*.pyc | ||
dist | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Flake8 Mock plugin | ||
================== | ||
|
||
Remember that a mock’s job is to say, “You got it, boss” whenever anyone calls | ||
it. It will do real work, like raising an exception, when one of its convenience | ||
methods is called, like assert_called_once_with. But it won’t do real work when | ||
you call a method that only resembles a convenience method, such as | ||
assert_called_once (no _with!). Sometimes the developer may not notice that | ||
is using an non-existent method because is not getting an output error telling | ||
him that. And for some reason can also forget to try the test cases to fail | ||
making sure that also fails!. | ||
|
||
This plugin provides checking possible non-existent methods for ``flake8``, | ||
the Python code checker. | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
You can install or upgrade ``flake8-debugger`` with these commands:: | ||
|
||
$ pip install flake8-debugger | ||
$ pip install --upgrade flake8-debugger | ||
|
||
|
||
List of non-existent methods checked | ||
------------------------------------ | ||
* ``assert_calls`` | ||
* ``assert_not_called`` | ||
* ``assert_called`` | ||
* ``assert_called_once`` | ||
* ``not_called`` | ||
* ``called_once`` | ||
* ``called_once_with`` | ||
|
||
|
||
Plugin for Flake8 | ||
----------------- | ||
|
||
When both ``flake8 2.2`` and ``flake8-mock`` are installed, the plugin is | ||
available in ``flake8``:: | ||
|
||
$ flake8 --version | ||
2.0 (pep8: 1.5.7, flake8-mock: 0.1dev, pyflakes: 0.8.1) | ||
|
||
|
||
Example output | ||
-------------- | ||
|
||
Once you run flake8, you can have something like:: | ||
|
||
$ flake8 test_file.py | ||
test_file.py:27:1: T002 assert_calls is a non-existen method. | ||
test_file.py:28:1: T002 called_once_with is a non-existen method. | ||
test_file.py:39:1: T002 not_called is a non-existen method. | ||
test_file.py:40:1: T002 assert_called is a non-existen method. | ||
|
||
|
||
Changes | ||
------- | ||
|
||
0.1dev (unreleased) | ||
```````````````` | ||
* First release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# coding: utf-8 | ||
|
||
from sys import stdin | ||
|
||
import compiler | ||
import tokenize | ||
|
||
|
||
__version__ = '0.1' | ||
|
||
NON_EXISTENT_METHODS = [ | ||
'assert_calls', | ||
'assert_not_called', | ||
'assert_called', | ||
'assert_called_once', | ||
'not_called', | ||
'called_once', | ||
'called_once_with', | ||
] | ||
_code = 'T002' # Not Defined yet | ||
ERROR_MESSAGE = "T002 %s is a non-existen method." | ||
|
||
|
||
def get_noqa_lines(code): | ||
tokens = tokenize.generate_tokens(lambda L=iter(code): next(L)) | ||
noqa = [token[2][0] for token in tokens if token[0] == tokenize.COMMENT | ||
and (token[1].endswith('noqa') or (isinstance(token[0], str) and | ||
token[0].endswith('noqa')))] | ||
return noqa | ||
|
||
|
||
class MockChecker(object): | ||
name = 'flake8-mock' | ||
version = __version__ | ||
|
||
def __init__(self, tree, filename='(none)', builtins=None): | ||
self.tree = tree | ||
self.filename = (filename == 'stdin' and stdin) or filename | ||
self.errors = [] | ||
|
||
def visitGetattr(self, node): | ||
if (node.lineno not in self.noqa_lines) \ | ||
and node.attrname in NON_EXISTENT_METHODS: | ||
self.errors.append({ | ||
"message": ERROR_MESSAGE % node.attrname, | ||
"line": node.lineno, | ||
}) | ||
|
||
def run(self): | ||
with open(self.filename, 'r') as file_to_check: | ||
self.noqa_lines = get_noqa_lines(file_to_check.readlines()) | ||
ast = compiler.parseFile(self.filename) | ||
compiler.walk(ast, self) | ||
for error in self.errors: | ||
yield (error.get("line"), 0, error.get("message"), type(self)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# coding: utf-8 | ||
|
||
from __future__ import with_statement | ||
from setuptools import setup | ||
|
||
|
||
def get_version(fname='flake8_mock.py'): | ||
with open(fname) as f: | ||
for line in f: | ||
if line.startswith('__version__'): | ||
return eval(line.split('=')[-1]) | ||
|
||
|
||
def get_long_description(): | ||
descr = [] | ||
for fname in ('README.rst',): | ||
with open(fname) as f: | ||
descr.append(f.read()) | ||
return '\n\n'.join(descr) | ||
|
||
|
||
setup( | ||
name='flake8-mock', | ||
version=get_version(), | ||
description="Provides checking mock non-existent methods", | ||
long_description=get_long_description(), | ||
keywords='flake8 mock', | ||
author='Alejandro Pereira', | ||
author_email='[email protected]', | ||
url='https://github.com/aleGpereira/flake8-mock', | ||
license='GNU', | ||
py_modules=['flake8_mock'], | ||
zip_safe=False, | ||
entry_points={ | ||
'flake8.extension': [ | ||
'flake8_mock = flake8_mock:MockChecker', | ||
], | ||
}, | ||
classifiers=[ | ||
'Development Status :: 1 - Alpha', | ||
'Environment :: Console', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: GNU License', | ||
'Operating System :: OS Independent', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 3', | ||
'Topic :: Software Development :: Libraries :: Python Modules', | ||
'Topic :: Software Development :: Quality Assurance', | ||
], | ||
) |