def test_no_accept_encodings_sets_encodings_none(self): try: get_style_guide(parse_argv=True) # parse arguments self.assertTrue(CodingChecker.encodings is None) finally: if hasattr(CodingChecker, 'encodings'): del CodingChecker.encodings
def test_change_encoding(self): try: get_style_guide(parse_argv=True) # parse arguments self.assertEqual(CodingChecker.encodings, ['utf-8', 'utf-16']) finally: if hasattr(CodingChecker, 'encodings'): del CodingChecker.encodings
def test_get_style_guide_kwargs(self): m = mock.Mock() with mock.patch('flake8.engine.StyleGuide') as StyleGuide: with mock.patch('flake8.engine.get_parser') as get_parser: get_parser.return_value = (m, []) engine.get_style_guide(foo='bar') get_parser.assert_called_once_with() StyleGuide.assert_called_once_with(**{'parser': m, 'foo': 'bar'})
def test_get_style_guide_kwargs(self): m = mock.Mock() with mock.patch("flake8.engine.StyleGuide") as StyleGuide: with mock.patch("flake8.engine.get_parser") as get_parser: StyleGuide.return_value.options.jobs = "42" get_parser.return_value = (m, []) engine.get_style_guide(foo="bar") get_parser.assert_called_once_with() StyleGuide.assert_called_once_with(**{"parser": m, "foo": "bar"})
def test_config_file(self): """Check that one can force to not look for a config file""" _argv = sys.argv try: sys.argv = ['', '--no-isort-config'] get_style_guide(parse_argv=True) # parse arguments self.assertFalse(Flake8Isort.config_file) finally: sys.argv = _argv
def test_default_option(self): """By default a config file (.isort.cfg) is expected""" _argv = sys.argv try: sys.argv = [] get_style_guide(parse_argv=True) # parse arguments self.assertTrue(Flake8Isort.config_file) finally: sys.argv = _argv
def test_get_style_guide_kwargs(self): m = mock.Mock() with mock.patch('flake8.engine.StyleGuide') as StyleGuide: with mock.patch('flake8.engine.get_parser') as get_parser: m.ignored_extensions = [] StyleGuide.return_value.options.jobs = '42' StyleGuide.return_value.options.diff = False get_parser.return_value = (m, []) engine.get_style_guide(foo='bar') get_parser.assert_called_once_with() StyleGuide.assert_called_once_with(**{'parser': m, 'foo': 'bar'})
def _run_flake8_internal(filename): """Run flake8.""" from flake8.engine import get_style_guide from pep8 import BaseReport from prospector.message import Message, Location return_dict = dict() cwd = os.getcwd() class Flake8MergeReporter(BaseReport): """An implementation of pep8.BaseReport merging results. This implementation merges results from the flake8 report into the prospector report created earlier. """ def __init__(self, options): """Initialize this Flake8MergeReporter.""" super(Flake8MergeReporter, self).__init__(options) self._current_file = "" def init_file(self, filename, lines, expected, line_offset): """Start processing filename.""" relative_path = os.path.join(cwd, filename) self._current_file = os.path.realpath(relative_path) super(Flake8MergeReporter, self).init_file(filename, lines, expected, line_offset) def error(self, line, offset, text, check): """Record error and store in return_dict.""" code = super(Flake8MergeReporter, self).error(line, offset, text, check) key = _Key(self._current_file, line, code) return_dict[key] = Message(code, code, Location(self._current_file, None, None, line, offset), text[5:]) flake8_check_paths = [filename] get_style_guide(reporter=Flake8MergeReporter, jobs="1").check_files(paths=flake8_check_paths) return return_dict
def test_config_file(self): """Check that one can force to not look for a config file""" _argv = sys.argv try: sys.argv = ['', '--no-isort-config'] if IS_FLAKE8_3: from flake8.main import application app = application.Application() app.run(sys.argv) else: get_style_guide(parse_argv=True) # parse arguments self.assertFalse(Flake8Isort.config_file) finally: sys.argv = _argv
def check_file(path, flake8ignore, maxlength): """Run flake8 over a single file, and return the number of failures.""" if maxlength: flake8_style = get_style_guide( parse_argv=False, paths=['--max-line-length', maxlength]) else: flake8_style = get_style_guide(parse_argv=False) options = flake8_style.options if options.install_hook: from flake8.hooks import install_hook install_hook() return flake8_style.input_file(str(path), expected=flake8ignore)
def configure_pep8(): if IS_WINDOWS: # WINDOWS UGLY AND HACKISH PATCH for flake 8 is based on # http://goo.gl/2b53SG sys.argv.append(".") pep8 = engine.get_style_guide(jobs=1) else: pep8 = engine.get_style_guide() pep8.reporter = FileCollectReport report = pep8.init_report(pep8.reporter) report.input_file = pep8.input_file pep8.runner = report.task_queue.put return pep8
def check_syntax(self): """ From flake8 """ packages = settings.PACKAGES_TO_TEST # Prepare config_file = getattr(yak_settings, 'FLAKE8_CONFIG', flake8.main.DEFAULT_CONFIG) flake8_style = get_style_guide(config_file=config_file) options = flake8_style.options if options.install_hook: from flake8.hooks import install_hook install_hook() # Save to file for later printing instead of printing now old_stdout = sys.stdout sys.stdout = out = open('syntax_output', 'w+') # Run the checkers report = flake8_style.check_files(paths=packages) sys.stdout = old_stdout # Print the final report options = flake8_style.options if options.statistics: report.print_statistics() if options.benchmark: report.print_benchmark() if report.total_errors: out.close() with open("syntax_output") as f: self.fail("{0} Syntax warnings!\n\n{1}".format(report.total_errors, f.read()))
def critique(ui, repo, entire=False, node=None, **kwargs): """Perform a critique of a changeset.""" demandimport.disable() from flake8.engine import get_style_guide from pep8 import DiffReport, parse_udiff style = get_style_guide(parse_argv=False, ignore='E128') ctx = repo[node] if not entire: diff = ''.join(ctx.diff()) style.options.selected_lines = {} for k, v in parse_udiff(diff).items(): if k.startswith('./'): k = k[2:] style.options.selected_lines[k] = v style.options.report = DiffReport(style.options) deleted = repo.status(ctx.p1().node(), ctx.node())[2] files = [f for f in ctx.files() if f.endswith('.py') and f not in deleted] style.check_files(files) demandimport.enable()
def main(): # flake8 flake8 = get_style_guide(exclude=['.tox', 'build']) report = flake8.check_files([BASE_DIR]) # TODO integration test creating a directory to be git-ified return print_report(report, flake8)
def git_hook(complexity=-1, strict=False, ignore=None, lazy=False): """This is the function used by the git hook. :param int complexity: (optional), any value > 0 enables complexity checking with mccabe :param bool strict: (optional), if True, this returns the total number of errors which will cause the hook to fail :param str ignore: (optional), a comma-separated list of errors and warnings to ignore :param bool lazy: (optional), allows for the instances where you don't add the files to the index before running a commit, e.g., git commit -a :returns: total number of errors if strict is True, otherwise 0 """ gitcmd = "git diff-index --cached --name-only HEAD" if lazy: gitcmd = gitcmd.replace('--cached ', '') _, files_modified, _ = run(gitcmd) flake8_style = get_style_guide( config_file=DEFAULT_CONFIG, ignore=ignore, max_complexity=complexity) report = flake8_style.check_files(files_modified) if strict: return report.total_errors return 0
def git_hook(complexity=-1, strict=False, ignore=None, lazy=False): """This is the function used by the git hook. :param int complexity: (optional), any value > 0 enables complexity checking with mccabe :param bool strict: (optional), if True, this returns the total number of errors which will cause the hook to fail :param str ignore: (optional), a comma-separated list of errors and warnings to ignore :param bool lazy: (optional), allows for the instances where you don't add the files to the index before running a commit, e.g., git commit -a :returns: total number of errors if strict is True, otherwise 0 """ gitcmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD" if lazy: # Catch all files, including those not added to the index gitcmd = gitcmd.replace('--cached ', '') if hasattr(ignore, 'split'): ignore = ignore.split(',') # Returns the exit code, list of files modified, list of error messages _, files_modified, _ = run(gitcmd) # Run the checks flake8_style = get_style_guide( config_file=DEFAULT_CONFIG, ignore=ignore, max_complexity=complexity) report = flake8_style.check_files([f for f in files_modified if f.endswith('.py')]) if strict: return report.total_errors return 0
def load_tests(loader, tests, pattern): flake8_style = engine.get_style_guide( parse_argv=False, # Ignore H104 otherwise it's # raised on doctests. ignore=("F", "H104"), ) options = flake8_style.options for name, check in checks.__dict__.items(): if not hasattr(check, "name"): continue if check.name != checks.__name__: continue if not check.__doc__: continue for (lineno, (raw, line)) in enumerate(_get_lines(check)): code, __, filename, source = line lines = [part.replace(r"\t", "\t") + "\n" for part in source.split(r"\n")] file_cases.append( ( "%s-line-%s" % (name, lineno), dict(lines=lines, raw=raw, options=options, code=code, filename=filename), ) ) return testscenarios.load_tests_apply_scenarios(loader, tests, pattern)
def test_syntax(self): """ From flake8 """ packages = ["signals"] # Prepare config_file = os.path.join(os.path.dirname(__file__), "..", "setup.cfg") flake8_style = get_style_guide(config_file=config_file) options = flake8_style.options if options.install_hook: from flake8.hooks import install_hook install_hook() # Save to file for later printing instead of printing now old_stdout = sys.stdout sys.stdout = out = open(".syntax_output", "w+") # Run the checkers report = flake8_style.check_files(paths=packages) sys.stdout = old_stdout # Print the final report options = flake8_style.options if options.statistics: report.print_statistics() if options.benchmark: report.print_benchmark() if report.total_errors: out.close() with open(".syntax_output") as f: self.fail("{0} Syntax warnings!\n\n{1}".format(report.total_errors, f.read()))
def load_tests(loader, tests, pattern): flake8_style = engine.get_style_guide(parse_argv=False, # Ignore H104 otherwise it's # raised on doctests. ignore=('F', 'H104')) options = flake8_style.options for entry in pkg_resources.iter_entry_points('flake8.extension'): if not entry.module_name.startswith('spotless.'): continue check = entry.load() name = entry.attrs[0] if check.skip_on_py3 and six.PY3: continue for (lineno, (raw, (code, source))) in enumerate( test_doctest._get_lines(check)): lines = [part.replace(r'\t', '\t') + '\n' for part in source.split(r'\n')] test_doctest.file_cases.append(("%s-%s-line-%s" % (entry.name, name, lineno), dict( lines=lines, raw=raw, options=options, code=code))) return testscenarios.load_tests_apply_scenarios(loader, tests, pattern)
def run_flake8(filename, rcfile=None): """run flake8 check.""" kwargs = dict(format="pylint", ignore=["E501"]) if rcfile: kwargs['config'] = rcfile flake8 = get_style_guide(**kwargs) flake8.input_file(filename)
def load_tests(loader, tests, pattern): default_checks = [e.name for e in pkg_resources.iter_entry_points('flake8.extension')] flake8_style = engine.get_style_guide( parse_argv=False, # We are testing neutron-specific hacking rules, so there is no need # to run the checks registered by hacking or other flake8 extensions. ignore=default_checks) options = flake8_style.options for name, check in checks.__dict__.items(): if not hasattr(check, 'name'): continue if check.name != checks.__name__: continue if not check.__doc__: continue for (lineno, (raw, line)) in enumerate(_get_lines(check)): code, __, filename, source = line lines = [part.replace(r'\t', '\t') + '\n' for part in source.split(r'\n')] file_cases.append(("%s-line-%s" % (name, lineno), dict(lines=lines, raw=raw, options=options, code=code, filename=filename))) return testscenarios.load_tests_apply_scenarios(loader, tests, pattern)
def run(self): flake8_style = get_style_guide(config_file=DEFAULT_CONFIG, **self.options_dict) paths = self.distribution_files() report = flake8_style.check_files(paths) exit_code = print_report(report, flake8_style) raise SystemExit(exit_code > 0)
def run(self, apps_locations, **options): output = open(os.path.join(options['output_dir'], 'flake8.report'), 'w') class JenkinsReport(pep8.BaseReport): def error(instance, line_number, offset, text, check): code = super(JenkinsReport, instance).error(line_number, offset, text, check) if not code: return sourceline = instance.line_offset + line_number output.write('%s:%s:%s: %s\n' % (instance.filename, sourceline, offset + 1, text)) pep8_options = {'exclude': options['pep8-exclude'].split(',')} if options['pep8-select']: pep8_options['select'] = options['pep8-select'].split(',') if options['pep8-ignore']: pep8_options['ignore'] = options['pep8-ignore'].split(',') if options['pep8-max-line-length']: pep8_options['max_line_length'] = options['pep8-max-line-length'] pep8style = get_style_guide( parse_argv=False, config_file=self.get_config_path(options), reporter=JenkinsReport, max_complexity=int(options['max_complexity']), jobs='1', **pep8_options) pep8style.options.report.start() for location in apps_locations: pep8style.input_dir(os.path.relpath(location)) pep8style.options.report.stop() output.close()
def teardown_test_environment(self, **kwargs): class JenkinsReport(pep8.BaseReport): def error(instance, line_number, offset, text, check): code = super(JenkinsReport, instance).error( line_number, offset, text, check, ) if not code: return sourceline = instance.line_offset + line_number self.output.write( '%s:%s:%s: %s\n' % (instance.filename, sourceline, offset + 1, text), ) flake8style = get_style_guide( parse_argv=False, config_file=self.pep8_rcfile, reporter=JenkinsReport, **self.pep8_options) # Jenkins pep8 validator requires relative paths project_root = os.path.abspath(os.path.dirname(__name__)) for location in map(lambda x: os.path.relpath(x, project_root), get_app_locations()): flake8style.input_dir(location) self.output.close()
def hg_hook(ui, repo, **kwargs): """This is the function executed directly by Mercurial as part of the hook. This is never called directly by the user, so the parameters are undocumented. If you would like to learn more about them, please feel free to read the official Mercurial documentation. """ complexity = ui.config('flake8', 'complexity', default=-1) strict = ui.configbool('flake8', 'strict', default=True) ignore = ui.config('flake8', 'ignore', default=None) config = ui.config('flake8', 'config', default=DEFAULT_CONFIG) paths = _get_files(repo, **kwargs) # We only want to pass ignore and max_complexity if they differ from the # defaults so that we don't override a local configuration file options = {} if ignore: options['ignore'] = ignore if complexity > -1: options['max_complexity'] = complexity flake8_style = get_style_guide(config_file=config, paths=['.'], **options) report = flake8_style.check_files(paths) if strict: return report.total_errors return 0
def check_files(self, fake_stdin=None, arglist=None, explicit_stdin=True, filename=None, count=0): """Call check_files and verify error count.""" arglist = arglist or [] ignore_used = any(x.startswith('--putty-ignore') for x in arglist) if not ignore_used: arglist.append('--putty-ignore=') if '--putty-auto-ignore' not in arglist: arglist.append('--putty-no-auto-ignore') if explicit_stdin: arglist.append('-') argv = ['flake8', _IGNORE_OPTION] + arglist with mock.patch("sys.argv", argv): style_guide = engine.get_style_guide(parse_argv=True) with mock.patch(pep8.__name__ + '.stdin_get_value', fake_stdin): if filename: results = style_guide.input_file( filename, lines=pep8.stdin_get_value().splitlines(True), ) total_errors = results else: report = style_guide.check_files() total_errors = report.total_errors self.assertEqual(total_errors, count) return style_guide, total_errors
def critique(ui, repo, entire=False, node=None, **kwargs): """Perform a critique of a changeset.""" # We run into weird import issues when running static analysis if the # demandimporter is enabled. with demandimport.deactivated(): from flake8.engine import get_style_guide from pep8 import DiffReport, parse_udiff style = get_style_guide(parse_argv=False, ignore='E128') ctx = repo[node] # Tell the reporter to ignore lines we didn't touch as part of this change. if not entire: diff = ''.join(ctx.diff()) style.options.selected_lines = {} for k, v in parse_udiff(diff).items(): if k.startswith('./'): k = k[2:] style.options.selected_lines[k] = v style.options.report = DiffReport(style.options) deleted = repo.status(ctx.p1().node(), ctx.node()).deleted files = [f for f in ctx.files() if f.endswith('.py') and f not in deleted] style.check_files(files)
def get_style_guide_with_warnings(engine, *args, **kwargs): """ Return a style guide object (obtained by calling engine.get_style_guide) and a list of the warnings that were raised in the process. Note: not threadsafe """ # Note # https://docs.python.org/2/library/warnings.html # # The catch_warnings manager works by replacing and then later # restoring the module's showwarning() function and internal list of # filter specifications. This means the context manager is modifying # global state and therefore is not thread-safe with warnings.catch_warnings(record=True) as collected_warnings: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Get the style guide style_guide = engine.get_style_guide(*args, **kwargs) # Now that the warnings have been collected, return the style guide and # the warnings. return (style_guide, collected_warnings)
def check_flake8(): if not HAS_FLAKE8: raise Exception('flake8 is required to check code formatting') import flake8.main from flake8.engine import get_style_guide test_dir = os.path.abspath(inspect.getfile(inspect.currentframe())) obspy_dir = os.path.dirname(os.path.dirname(os.path.dirname(test_dir))) untracked_files = get_untracked_files_from_git() or [] files = [] for dirpath, _, filenames in os.walk(obspy_dir): filenames = [_i for _i in filenames if os.path.splitext(_i)[-1] == os.path.extsep + "py"] if not filenames: continue for py_file in filenames: py_file = os.path.join(dirpath, py_file) # ignore untracked files if os.path.abspath(py_file) in untracked_files: continue # Check files that do not match any exclusion pattern for exclude_pattern in FLAKE8_EXCLUDE_FILES: if fnmatch.fnmatch(py_file, exclude_pattern): break else: files.append(py_file) flake8_style = get_style_guide(parse_argv=False, config_file=flake8.main.DEFAULT_CONFIG) sys.stdout = StringIO.StringIO() report = flake8_style.check_files(files) sys.stdout.seek(0) message = sys.stdout.read() sys.stdout = sys.__stdout__ return report, message
def test_local_check(self): flake8_style = engine.get_style_guide(parse_argv=False, ignore='F') report = pep8.BaseReport(flake8_style.options) line = ["#this-is-the-test-phrase"] checker = pep8.Checker(lines=line, options=flake8_style.options, report=report) checker.check_all() self.assertIn("L100", report.counters)
def main(): """Parse options and run checks on Python source.""" # Prepare flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG) options = flake8_style.options if options.install_hook: from flake8.hooks import install_hook install_hook() # Run the checkers report = flake8_style.check_files() # Print the final report options = flake8_style.options if options.statistics: report.print_statistics() if options.benchmark: report.print_benchmark() if report.total_errors: if options.count: sys.stderr.write(str(report.total_errors) + '\n') if not options.exit_zero: raise SystemExit(1)
def run(self): flake8_style = get_style_guide(config_file=DEFAULT_CONFIG) paths = self.distribution_files() report = flake8_style.check_files(paths) raise SystemExit(report.total_errors > 0)
def test_disables_extensions_that_are_not_selected(self): with mock.patch('flake8.engine._register_extensions') as re: re.return_value = ([('fake_ext', '0.1a1')], [], [], ['X']) sg = engine.get_style_guide() assert 'X' in sg.options.ignore
def test_get_style_guide(self): with mock.patch('flake8.engine._register_extensions') as reg_ext: reg_ext.return_value = ([], [], [], []) g = engine.get_style_guide() self.assertTrue(isinstance(g, engine.StyleGuide)) reg_ext.assert_called_once_with()
def process_commit(self, review, landing_repo_url, repo_url, commit): revision = commit['rev'] self.logger.info('reviewing revision: %s (review request: %d)' % (revision[:12], commit['review_request_id'])) repo_path = self.ensure_hg_repo_exists(landing_repo_url, repo_url, revision) self.hg_commit_changes(repo_path, revision, diff_context=0) adds, dels, mods, copies, diff = self.hg_commit_changes(repo_path, revision, diff_context=0) rel_adds = set(f for f in adds if f.endswith('.py')) rel_mods = set(f for f in mods if f.endswith('.py')) relevant = rel_adds | rel_mods if not relevant: self.logger.info('not reviewing revision: %s no relevant ' 'python changes in commit' % revision) return # flake8's multiprocessing default doesn't work synchronously for # some reason. Probably because our saved state isn't being # transferred across process boundaries. Specify jobs=0 to get # results. style = get_style_guide(parse_argv=False, jobs=0) style.options.selected_lines = {} for k, v in parse_udiff(diff).items(): if k.startswith('./'): k = k[2:] style.options.selected_lines[k] = v style.options.report = CapturingDiffReport(style.options) oldcwd = os.getcwd() try: os.chdir(repo_path) results = style.check_files(relevant) finally: os.chdir(oldcwd) error_count = 0 for filename, errors in sorted(results.file_results.items()): if not errors: continue errors = sorted(errors, cmp=_cmp_errors) for line, offset, code, text, doc in errors: error_count += 1 num_lines = 1 comment = '%s: %s' % (code, text) if code in LINE_ADJUSTMENTS: line_offset, num_lines = LINE_ADJUSTMENTS[code] line += line_offset review.comment(filename, line, num_lines, comment) commentlines = [] if error_count: commentlines.extend([ random.choice(ERRORS_QUIPS), '', 'I analyzed your Python changes and found %d errors.' % (error_count), ]) else: commentlines.extend([ random.choice(NO_ERRORS_QUIPS), '', 'Congratulations, there were no Python static analysis ' 'issues with this patch!', ]) commentlines.extend([ '', 'The following files were examined:', '', ]) commentlines.extend(' %s' % f for f in sorted(relevant)) review.publish(body_top='\n'.join(commentlines), ship_it=error_count == 0) self.strip_nonpublic_changesets(repo_path)
def test_stdin_disables_jobs(self): with mock.patch('flake8.util.is_using_stdin') as is_using_stdin: is_using_stdin.return_value = True guide = engine.get_style_guide() assert isinstance(guide, reporter.BaseQReport) is False
def run_flake8(): # flake8 flake8 = get_style_guide(exclude=['.tox', 'build'], max_complexity=10) report = flake8.check_files([BASE_DIR]) return print_report(report, flake8)
def test_flake8_conformance(self): with capture_stdout() as captor: flake8_style = get_style_guide(paths=[settings.BASE_DIR]) report = flake8_style.check_files() if report.total_errors > 0: self.fail('Got some flake8 errors:\n{0}'.format(captor['value']), )
def git_hook(complexity=-1, strict=False, ignore=None, lazy=False): """This is the function used by the git hook. :param int complexity: (optional), any value > 0 enables complexity checking with mccabe :param bool strict: (optional), if True, this returns the total number of errors which will cause the hook to fail :param str ignore: (optional), a comma-separated list of errors and warnings to ignore :param bool lazy: (optional), allows for the instances where you don't add the files to the index before running a commit, e.g., git commit -a :returns: total number of errors if strict is True, otherwise 0 """ gitcmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD" if lazy: # Catch all files, including those not added to the index gitcmd = gitcmd.replace('--cached ', '') if hasattr(ignore, 'split'): ignore = ignore.split(',') # Returns the exit code, list of files modified, list of error messages _, files_modified, _ = run(gitcmd) # We only want to pass ignore and max_complexity if they differ from the # defaults so that we don't override a local configuration file options = {} if ignore: options['ignore'] = ignore if complexity > -1: options['max_complexity'] = complexity tmpdir = tempfile.mkdtemp() flake8_style = get_style_guide(config_file=DEFAULT_CONFIG, paths=['.'], **options) filepatterns = flake8_style.options.filename # Copy staged versions to temporary directory files_to_check = [] try: for file_ in files_modified: # get the staged version of the file gitcmd_getstaged = "git show :%s" % file_ _, out, _ = run(gitcmd_getstaged, raw_output=True, decode=False) # write the staged version to temp dir with its full path to # avoid overwriting files with the same name dirname, filename = os.path.split(os.path.abspath(file_)) prefix = os.path.commonprefix([dirname, tmpdir]) dirname = compat.relpath(dirname, start=prefix) dirname = os.path.join(tmpdir, dirname) if not os.path.isdir(dirname): os.makedirs(dirname) # check_files() only does this check if passed a dir; so we do it if ((pep8.filename_match(file_, filepatterns) and not flake8_style.excluded(file_))): filename = os.path.join(dirname, filename) files_to_check.append(filename) # write staged version of file to temporary directory with open(filename, "wb") as fh: fh.write(out) # Run the checks report = flake8_style.check_files(files_to_check) # remove temporary directory finally: shutil.rmtree(tmpdir, ignore_errors=True) if strict: return report.total_errors return 0
def test_flake8(self): report = get_style_guide(parse_argv=True, paths=".").check_files() self.assertEquals(report.get_state()["total_errors"], 0)
def run(self): from flake8.engine import get_style_guide flake8_style = get_style_guide(config_file='setup.cfg') paths = self.distribution_files() report = flake8_style.check_files(paths) raise SystemExit(report.total_errors > 0)
def run_flake8(): flake8 = get_style_guide(exclude=['.tox', 'build']) report = flake8.check_files([BASE_DIR]) return print_report(report, flake8)
def main(modules, verbosity=2, failfast=False, contrib=None, nocontrib=False): print('python ' + sys.version) print('django ' + django.get_version()) print('*' * 80) # run flake8 first styleguide = get_style_guide( # TODO: Check if we can read a config file # parse_argv=False, # config_file="setup.cfg", ignore=["T000"], max_complexity=-1, doctests=False, max_line_length=120, exclude=[ '__pycache__', '.git', '.tox', 'virtenv', '*egg', 'migrations', ], paths=[ "djangobmf", "tests", ]) styleguide.options.report.start() styleguide.options.report.stop() if styleguide.options.report.get_count() > 0: sys.exit(True) # apply test settings TEMP_DIR = tempfile.mkdtemp(prefix='djangobmf_') settings.BMF_DOCUMENT_ROOT = TEMP_DIR settings.BMF_DOCUMENT_URL = '/documents/' if verbosity > 0: # Ensure any warnings captured to logging are piped through a verbose # logging handler. logger = logging.getLogger('py.warnings') handler = logging.StreamHandler() logger.addHandler(handler) # Load all the app django.setup() # only test one contrib module if contrib: modules = ["djangobmf.contrib.%s" % contrib] # find tests in tests-directory if len(modules) == 0: path = os.path.join(os.path.dirname(__file__), "tests") for module in os.listdir(path): if os.path.isdir(os.path.join(path, module)) and module[0] != '_': modules.append('tests.%s' % module) # find tests in contrib modules SKIPDIRS = [] if not nocontrib: path = bmfcontrib.__path__[0] for module in os.listdir(path): if os.path.isdir(os.path.join(path, module)): if module[0] == '_' or module in SKIPDIRS: continue modules.append('djangobmf.contrib.%s' % module) # add currencies to INSTALLED_APPS path = bmfcurrencies.__path__[0] for module in os.listdir(path): if os.path.isdir(os.path.join(path, module)): if module[0] == '_': continue settings.INSTALLED_APPS += ('djangobmf.currency.%s' % module, ) # update installed apps installed_app_names = set(get_installed()) for module in modules: if module not in installed_app_names: if verbosity >= 2: print("Importing application %s" % module) settings.INSTALLED_APPS += (module, ) apps.set_installed_apps(settings.INSTALLED_APPS) # Load default settings from bmf framework from djangobmf.conf import settings as bmfsettings bmfsettings.patch() failures = djangobmf_tests(verbosity, False, failfast, modules) try: # Removing the temporary TEMP_DIR. Ensure we pass in unicode # so that it will successfully remove temp trees containing # non-ASCII filenames on Windows. (We're assuming the temp dir # name itself does not contain non-ASCII characters.) shutil.rmtree(six.text_type(TEMP_DIR)) except OSError: print('Failed to remove temp directory: %s' % TEMP_DIR) sys.exit(bool(failures))
def test_windows_disables_jobs(self): with mock.patch('flake8.engine.is_windows') as is_windows: is_windows.return_value = True guide = engine.get_style_guide() assert isinstance(guide, reporter.BaseQReport) is False
def run(self, apps_locations, **options): output = open(os.path.join(options['output_dir'], 'flake8.report'), 'w') class JenkinsReport(pep8.BaseReport): def error(instance, line_number, offset, text, check): code = super(JenkinsReport, instance).error(line_number, offset, text, check) if not code: return sourceline = instance.line_offset + line_number output.write('%s:%s:%s: %s\n' % (instance.filename, sourceline, offset + 1, text)) pep8_options = {} config_file = self.get_config_path(options) if config_file is not None: pep8_options['config_file'] = config_file set_option(pep8_options, 'exclude', options['pep8-exclude'], config_file, default=pep8.DEFAULT_EXCLUDE + ",south_migrations", split=',') set_option(pep8_options, 'select', options['pep8-select'], config_file, split=',') set_option(pep8_options, 'ignore', options['pep8-ignore'], config_file, split=',') set_option(pep8_options, 'max_line_length', options['pep8-max-line-length'], config_file, default=pep8.MAX_LINE_LENGTH) set_option(pep8_options, 'max_complexity', options['flake8-max-complexity'], config_file, default=-1) pep8style = get_style_guide(parse_argv=False, reporter=JenkinsReport, jobs='1', **pep8_options) pep8style.options.report.start() for location in apps_locations: pep8style.input_dir(os.path.relpath(location)) pep8style.options.report.stop() output.close()