def main(): # Get our configuration config = ProspectorConfig() paths = config.paths if len(paths) > 1 and not all([os.path.isfile(path) for path in paths]): sys.stderr.write('\nIn multi-path mode, all inputs must be files, ' 'not directories.\n\n') get_parser().print_usage() sys.exit(2) # Make it so prospector = Prospector(config) prospector.execute() prospector.print_messages() if config.exit_with_zero_on_success(): # if we ran successfully, and the user wants us to, then we'll # exit cleanly return 0 # otherwise, finding messages is grounds for exiting with an error # code, to make it easier for bash scripts and similar situations # to know if there any errors have been found. if len(prospector.get_messages()) > 0: return 1 return 0
def main(): # Get our configuration config = ProspectorConfig() paths = config.paths if len(paths) > 1 and not all(os.path.isfile(path) for path in paths): sys.stderr.write("\nIn multi-path mode, all inputs must be files, " "not directories.\n\n") get_parser().print_usage() sys.exit(2) # Make it so prospector = Prospector(config) prospector.execute() prospector.print_messages() if config.exit_with_zero_on_success(): # if we ran successfully, and the user wants us to, then we'll # exit cleanly sys.exit(0) # otherwise, finding messages is grounds for exiting with an error # code, to make it easier for bash scripts and similar situations # to know if any errors have been found. if len(prospector.get_messages()) > 0: sys.exit(1) sys.exit(0)
def run_prospector(base_dir, clear_ignore_patterns=False): """Run prospector and apply white/blacklists to the results""" orig_expand_default = optparse.HelpFormatter.expand_default sys.argv = ['fakename'] sys.argv.extend(PROSPECTOR_OPTIONS) # add/set REPO_BASE_DIR as positional path sys.argv.append(base_dir) config = ProspectorConfig() # prospector will sometimes wrongly autodetect django config.libraries = [] prospector = Prospector(config) # Set defaults for prospector: config.profile.doc_warnings = False config.profile.test_warnings = False config.profile.autodetect = True config.profile.member_warnings = False if clear_ignore_patterns: config.profile.ignore_patterns = [r'.^'] config.ignores = [] else: append_pattern = r'(^|/)\..+' config.profile.ignore_patterns.append(append_pattern) config.ignores.append(re.compile(append_pattern)) # Enable pylint Python3 compatibility tests: config.profile.pylint['options']['enable'] = 'python3' log.debug("prospector argv = %s" % sys.argv) log.debug("prospector profile from config = %s" % vars(config.profile)) prospector.execute() log.debug("prospector profile form prospector = %s" % vars(prospector.config.profile)) blacklist = map(re.compile, PROSPECTOR_BLACKLIST) whitelist = map(re.compile, PROSPECTOR_WHITELIST) failures = [] for msg in prospector.get_messages(): # example msg.as_dict(): # {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring', # 'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py', # 'line': 122, 'character': 8, 'module': 'headers'}} log.debug("prospector message %s" % msg.as_dict()) if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]): continue if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]): failures.append(msg.as_dict()) # There is some ugly monkeypatch code in pylint # (or logilab if no recent enough pylint is installed) # Make sure the original is restored # (before any errors are reported; no need to put this in setUp/tearDown) optparse.HelpFormatter.expand_default = orig_expand_default return failures
def test_prospector(self): """Run prospector.run.main, but apply white/blacklists to the results""" orig_expand_default = optparse.HelpFormatter.expand_default if not HAS_PROSPECTOR: if sys.version_info < (2, 7): log.info('No protector tests are ran on py26 or older.') else: log.info('No protector tests are ran, install prospector manually first') # This is fatal on jenkins/... if 'JENKINS_URL' in os.environ: self.assertTrue(False, 'prospector must be installed in jenkins environment') return sys.argv = ['fakename'] sys.argv.extend(self.PROSPECTOR_OPTIONS) # add/set REPO_BASE_DIR as positional path sys.argv.append(self.setup.REPO_BASE_DIR) config = ProspectorConfig() # prospector will sometimes wrongly autodetect django config.libraries = [] prospector = Prospector(config) prospector.execute() blacklist = map(re.compile, self.PROSPECTOR_BLACKLIST) whitelist = map(re.compile, self.PROSPECTOR_WHITELIST) failures = [] for msg in prospector.get_messages(): # example msg.as_dict(): # {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring', # 'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py', # 'line': 122, 'character': 8, 'module': 'headers'}} log.debug("prospector message %s" % msg.as_dict()) if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]): continue if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]): failures.append(msg.as_dict()) # There is some ugly monkeypatch code in pylint # (or logilab if no recent enough pylint is installed) # Make sure the original is restored # (before any errors are reported; no need to put this in setUp/tearDown) optparse.HelpFormatter.expand_default = orig_expand_default self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
def test_prospector(self): """Run prospector.run.main, but apply white/blacklists to the results""" orig_expand_default = optparse.HelpFormatter.expand_default if not HAS_PROTECTOR: if sys.version_info < (2, 7): log.info('No protector tests are ran on py26 or older.') else: log.info('No protector tests are ran, install prospector manually first') # This is fatal on jenkins/... if 'JENKINS_URL' in os.environ: self.assertTrue(False, 'prospector must be installed in jenkins environment') return sys.argv = ['fakename'] sys.argv.extend(self.PROSPECTOR_OPTIONS) # add/set REPO_BASE_DIR as positional path sys.argv.append(self.setup.REPO_BASE_DIR) config = ProspectorConfig() # prospector will sometimes wrongly autodetect django config.libraries = [] prospector = Prospector(config) prospector.execute() blacklist = map(re.compile, self.PROSPECTOR_BLACKLIST) whitelist = map(re.compile, self.PROSPECTOR_WHITELIST) failures = [] for msg in prospector.get_messages(): # example msg.as_dict(): # {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring', # 'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py', # 'line': 122, 'character': 8, 'module': 'headers'}} log.debug("prospector message %s" % msg.as_dict()) if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]): continue if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]): failures.append(msg.as_dict()) # There is some ugly monkeypatch code in pylint # (or logilab if no recent enough pylint is installed) # Make sure the original is restored # (before any errors are reported; no need to put this in setUp/tearDown) optparse.HelpFormatter.expand_default = orig_expand_default self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
def _get_pylint_tool_and_prospector_config(argv_patch=None): if argv_patch is None: argv_patch = [""] with patch("sys.argv", argv_patch): config = ProspectorConfig() pylint_tool = PylintTool() return pylint_tool, config
def _get_config(profile_name: str) -> ProspectorConfig: profile_path = Path( __file__).parent / f"test_profiles/{profile_name}.yaml" with patch("sys.argv", ["prospector", "--profile", str(profile_path.absolute())]): return ProspectorConfig()
def test_determine_ignores_all_str(self): with patch("sys.argv", ["", "-P", "prospector-str-ignores"]), patch( "os.getcwd", return_value=os.path.realpath("tests/config")): config = ProspectorConfig() self.assertNotEqual(len(config.ignores), 0) boundary = r"(^|/|\\)%s(/|\\|$)" paths = ["2017", "2018"] for path in paths: compiled_ignored_path = re.compile(boundary % re.escape(path)) self.assertIn(compiled_ignored_path, config.ignores)
def test_prospector(self): """Run prospector tests""" PROSPECTOR_OPTIONS = [ '--strictness', 'medium', '--max-line-length', '120', '--absolute-paths', ] sys.argv = ['fakename'] sys.argv.extend(PROSPECTOR_OPTIONS) sys.argv.append(REPO_BASE_DIR) config = ProspectorConfig() config.libraries = [] prospector = Prospector(config) prospector.execute() failures = [msg.as_dict() for msg in prospector.get_messages()] self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
def test_prospector(self): """Run prospector on project.""" sys.argv = ['fakename'] sys.argv.append(self.REPO_BASE_DIR) config = ProspectorConfig() prospector = Prospector(config) prospector.execute() failures = [] for msg in prospector.get_messages(): failures.append(msg.as_dict()) self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
def test_determine_ignores_containing_int_values_wont_throw_attr_exc(self): try: with patch("sys.argv", ["", "-P", "prospector-int-ignores"]), patch( "os.getcwd", return_value=os.path.realpath("tests/config")): config = ProspectorConfig() self.assertNotEqual(len(config.ignores), 0) boundary = r"(^|/|\\)%s(/|\\|$)" paths = ["2017", "2018"] for path in paths: compiled_ignored_path = re.compile(boundary % re.escape(path)) self.assertIn(compiled_ignored_path, config.ignores) except AttributeError as attr_exc: self.fail(attr_exc)
def setUp(self): with patch('sys.argv', ['']): self.config = ProspectorConfig() self.pylint_tool = PylintTool()
def setUp(self): with patch("sys.argv", [""]): self.config = ProspectorConfig() self.pep8_tool = Pep8Tool()
def setUp(self): with patch("sys.argv", [""]): self.config = ProspectorConfig() self.bandit_tool = BanditTool()
def run_prospector(base_dir, clear_ignore_patterns=False): """Run prospector and apply white/blacklists to the results""" orig_expand_default = optparse.HelpFormatter.expand_default log.info("Using prosector version %s", prospector_version) sys.argv = ['fakename'] sys.argv.extend(PROSPECTOR_OPTIONS) if PROSPECTOR_USE_LIBS: sys.argv.extend(["--uses", ",".join(PROSPECTOR_USE_LIBS)]) # add/set REPO_BASE_DIR as positional path sys.argv.append(base_dir) log.debug("prospector commandline %s" % sys.argv) config = ProspectorConfig() # prospector will sometimes wrongly autodetect django config.libraries = [] prospector = Prospector(config) # Set defaults for prospector: config.profile.doc_warnings = False config.profile.test_warnings = False config.profile.autodetect = True config.profile.member_warnings = False if clear_ignore_patterns: config.profile.ignore_patterns = [r'.^'] config.ignores = [] else: append_pattern = r'(^|/)\..+' config.profile.ignore_patterns.append(append_pattern) config.ignores.append(re.compile(append_pattern)) # Enable pylint Python3 compatibility tests: config.profile.pylint['options']['enable'] = 'python3' log.debug("prospector argv = %s" % sys.argv) log.debug("prospector profile from config = %s" % vars(config.profile)) prospector.execute() log.debug("prospector profile form prospector = %s" % vars(prospector.config.profile)) # map yields a generator object in Python 3, but since we want to iterate over the whitelist/blacklist # multiple times, we need to make sure it's a list (since you can only iterate once over a generator) blacklist = list(map(re.compile, PROSPECTOR_BLACKLIST)) whitelist = list(map(re.compile, PROSPECTOR_WHITELIST)) failures = [] for msg in prospector.get_messages(): # example msg.as_dict(): # {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring', # 'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py', # 'line': 122, 'character': 8, 'module': 'headers'}} log.debug("prospector message %s" % msg.as_dict()) if any([ bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist ]): continue if any([ bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist ]): failures.append(msg.as_dict()) # There is some ugly monkeypatch code in pylint # (or logilab if no recent enough pylint is installed) # Make sure the original is restored # (before any errors are reported; no need to put this in setUp/tearDown) optparse.HelpFormatter.expand_default = orig_expand_default return failures
def setUp(self): with patch("sys.argv", [""]): self.config = ProspectorConfig() self.vulture_tool = VultureTool()
def setUp(self): with patch("sys.argv", [""]): self.config = ProspectorConfig() self._tool = PycodestyleTool()