Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import compiler
import compiler.ast
import optparse
import sys
class MockChecker(object):
def __init__(self):
self.errors = 0
self.current_filename = ""
self.non_existent_methods = [
def check_files(self, files):
for file in files:
def check_file(self, filename):
self.current_filename = filename
ast = compiler.parseFile(filename)
except SyntaxError, error:
print >>sys.stderr, "SyntaxError on file %s:%d" % (filename, error.lineno)
compiler.walk(ast, self)
def visitGetattr(self, node):
if node.attrname in self.non_existent_methods:
print >>sys.stderr, "%s:%d: you may have called a nonexistent method on mock" % (self.current_filename, node.lineno)
self.errors += 1
def main():
parser = optparse.OptionParser(usage="%prog [options] file [files]", description="Checks that the test file does not contain non-existent mock methods")
(opts, files) = parser.parse_args()
if len(files) == 0:
parser.error("No filenames provided")
checker = MockChecker()
return 1 if checker.errors else 0
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.