def test_legacy_names_equivalent(self): """ 'pep8' tool was renamed to pycodestyle, 'pep257' tool was renamed to pydocstyle This test is to ensure that, for backwards compatibility until it is removed in prospector 2.0, that the old names and the new names are equivalent in profiles """ profile_old = ProspectorProfile.load("renaming_oldname", self._profile_path, allow_shorthand=False) profile_new = ProspectorProfile.load("renaming_newname", self._profile_path, allow_shorthand=False) # do they serialise to the same thing? for tool in ("pycodestyle", "pydocstyle"): old_dict = profile_old.as_dict()[tool] new_dict = profile_new.as_dict()[tool] self.assertListEqual(sorted(old_dict["disable"]), sorted(new_dict["disable"])) self.assertListEqual(sorted(old_dict["enable"]), sorted(new_dict["enable"])) self.assertDictEqual(profile_old.as_dict(), profile_new.as_dict()) # do they have the same settings for everything? for prof in (profile_old, profile_new): self.assertTrue(prof.is_tool_enabled("pycodestyle")) self.assertTrue(prof.is_tool_enabled("pydocstyle")) self.assertEqual(prof.pycodestyle["options"]["max-line-length"], 120)
def test_simple_inheritance(self): profile = ProspectorProfile.load("inherittest3", self._profile_path, allow_shorthand=False) disable = profile.pylint["disable"] disable.sort() self.assertEqual(["I0002", "I0003", "raw-checker-failed"], disable)
def test_empty_disable_list(self): """ This test verifies that a profile can still be loaded if it contains an empty 'pylint.disable' list """ profile = ProspectorProfile.load('empty_disable_list', self._profile_path, allow_shorthand=False) self.assertEqual([], profile.pylint['disable'])
def test_empty_disable_list(self): """ This test verifies that a profile can still be loaded if it contains an empty 'pylint.disable' list """ profile = ProspectorProfile.load("empty_disable_list", self._profile_path, allow_shorthand=False) self.assertEqual([], profile.pylint["disable"])
def test_simple_inheritance(self): profile = ProspectorProfile.load('inherittest3', self._profile_path, allow_shorthand=False) disable = profile.pylint['disable'] disable.sort() self.assertEqual(['I0002', 'I0003', 'raw-checker-failed'], disable)
def test_empty_profile(self): """ Verifies that a completely empty profile can still be parsed and have default values """ profile = ProspectorProfile.load("empty_profile", self._profile_path, allow_shorthand=False) self.assertEqual([], profile.pylint["disable"])
def test_empty_profile(self): """ Verifies that a completely empty profile can still be parsed and have default values """ profile = ProspectorProfile.load('empty_profile', self._profile_path, allow_shorthand=False) self.assertEqual([], profile.pylint['disable'])
def test_strictness_equivalence(self): profile = self._load("strictness_equivalence") medium_strictness = ProspectorProfile.load("strictness_medium", self._profile_path) self.assertListEqual( sorted(profile.pylint["disable"]), sorted(medium_strictness.pylint["disable"]), )
def test_shorthand_inheritance(self): profile = self._load('shorthand_inheritance') high_strictness = ProspectorProfile.load('strictness_high', self._profile_path, # don't implicitly add things allow_shorthand=False, # but do include the profiles that the start.yaml will forced_inherits=['doc_warnings', 'no_member_warnings'] ) self.assertDictEqual(profile.pylint, high_strictness.pylint) self.assertDictEqual(profile.pep8, high_strictness.pep8) self.assertDictEqual(profile.pyflakes, high_strictness.pyflakes)
def test_pep8_shorthand_with_newname(self): """ 'pep8' is still a valid entry in the profile but in the future, only as a shorthand ("pep8: full") however for now, it also has to be able to configure pycodestyle """ profile = ProspectorProfile.load("pep8_shorthand_pycodestyle", self._profile_path, allow_shorthand=True) self.assertTrue("full_pep8" in profile.inherit_order) self.assertTrue(profile.is_tool_enabled("pycodestyle")) self.assertEqual(profile.pycodestyle["options"]["max-line-length"], 120)
def test_shorthand_inheritance(self): profile = self._load('shorthand_inheritance') high_strictness = ProspectorProfile.load( 'strictness_high', self._profile_path, # don't implicitly add things allow_shorthand=False, # but do include the profiles that the start.yaml will forced_inherits=['doc_warnings', 'no_member_warnings']) self.assertDictEqual(profile.pylint, high_strictness.pylint) self.assertDictEqual(profile.pep8, high_strictness.pep8) self.assertDictEqual(profile.pyflakes, high_strictness.pyflakes)
def test_shorthand_inheritance(self): profile = self._load("shorthand_inheritance") high_strictness = ProspectorProfile.load( "strictness_high", self._profile_path, # don't implicitly add things allow_shorthand=False, # but do include the profiles that the start.yaml will forced_inherits=["doc_warnings", "no_member_warnings"], ) self.assertDictEqual(profile.pylint, high_strictness.pylint) self.assertDictEqual(profile.pycodestyle, high_strictness.pycodestyle) self.assertDictEqual(profile.pyflakes, high_strictness.pyflakes)
def test_old_inherits_from_new(self): profile = ProspectorProfile.load("child_oldname.yaml", self._profile_path, allow_shorthand=False) assert profile.is_tool_enabled("pydocstyle") assert profile.is_tool_enabled("pycodestyle") assert "D401" in profile.pydocstyle["enable"] assert "D401" not in profile.pydocstyle["disable"] assert "E266" not in profile.pycodestyle["disable"] assert 120 == profile.pycodestyle["options"]["max-line-length"]
def test_formatter_types(self): summary = {'started': datetime.datetime(2014, 1, 1), 'completed': datetime.datetime(2014, 1, 1), 'message_count': 0, 'time_taken': '0', 'libraries': [], 'strictness': 'veryhigh', 'profiles': '', 'tools': []} profile = ProspectorProfile(name='horse', profile_dict={}, inherit_order=['horse']) for formatter_name, formatter in FORMATTERS.items(): formatter_instance = formatter(summary, [], profile) self.assertIsInstance(formatter_instance.render(True, True, False), six.string_types)
def test_new_inherits_from_old(self): """ Ensure that `pep8` can inherit from a `pycodecstyle` block and vice versa """ profile = ProspectorProfile.load("child_newname.yaml", self._profile_path, allow_shorthand=False) assert profile.is_tool_enabled("pydocstyle") assert profile.is_tool_enabled("pycodestyle") assert "D401" not in profile.pydocstyle["disable"] assert "D401" in profile.pydocstyle["enable"] assert "E266" not in profile.pycodestyle["disable"] assert 140 == profile.pycodestyle["options"]["max-line-length"]
def test_formatter_types(self): summary = { "started": datetime.datetime(2014, 1, 1), "completed": datetime.datetime(2014, 1, 1), "message_count": 0, "time_taken": "0", "libraries": [], "strictness": "veryhigh", "profiles": "", "tools": [], } profile = ProspectorProfile(name="horse", profile_dict={}, inherit_order=["horse"]) for formatter_name, formatter in FORMATTERS.items(): formatter_instance = formatter(summary, [], profile) self.assertIsInstance(formatter_instance.render(True, True, False), six.string_types)
def test_ignores(self): profile = ProspectorProfile.load("ignores", self._profile_path) self.assertEqual(["^tests/", "/migrations/"].sort(), profile.ignore_patterns.sort())
def test_ignores(self): profile = ProspectorProfile.load('ignores', self._profile_path) self.assertEqual(['^tests/', '/migrations/'].sort(), profile.ignore_patterns.sort())
def test_disable_tool(self): profile = ProspectorProfile.load('pylint_disabled', self._profile_path) self.assertFalse(profile.is_tool_enabled('pylint')) self.assertTrue(profile.is_tool_enabled('pep8') is None)
def _get_profile(self, path, config): # Use the specified profiles profile_provided = False if len(config.profiles) > 0: profile_provided = True cmdline_implicit = [] # if there is a '.prospector.ya?ml' or a '.prospector/prospector.ya?ml' or equivalent landscape config # file then we'll include that profile_name = None if not profile_provided: for possible_profile in AUTO_LOADED_PROFILES: prospector_yaml = os.path.join(path, possible_profile) if os.path.exists(prospector_yaml) and os.path.isfile( prospector_yaml): profile_provided = True profile_name = possible_profile break strictness = None if profile_provided: if profile_name is None: profile_name = config.profiles[0] extra_profiles = config.profiles[1:] else: extra_profiles = config.profiles strictness = 'from profile' else: # Use the preconfigured prospector profiles profile_name = 'default' extra_profiles = [] if config.doc_warnings is not None and config.doc_warnings: cmdline_implicit.append('doc_warnings') if config.test_warnings is not None and config.test_warnings: cmdline_implicit.append('test_warnings') if config.no_style_warnings is not None and config.no_style_warnings: cmdline_implicit.append('no_pep8') if config.full_pep8 is not None and config.full_pep8: cmdline_implicit.append('full_pep8') if config.member_warnings is not None and config.member_warnings: cmdline_implicit.append('member_warnings') # Use the strictness profile only if no profile has been given if config.strictness is not None and config.strictness: cmdline_implicit.append('strictness_%s' % config.strictness) strictness = config.strictness # the profile path is # * anything provided as an argument # * a directory called .prospector in the check path # * the check path # * prospector provided profiles profile_path = config.profile_path prospector_dir = os.path.join(path, '.prospector') if os.path.exists(prospector_dir) and os.path.isdir(prospector_dir): profile_path.append(prospector_dir) profile_path.append(path) profile_path.append(BUILTIN_PROFILE_PATH) try: forced_inherits = cmdline_implicit + extra_profiles profile = ProspectorProfile.load(profile_name, profile_path, forced_inherits=forced_inherits) except CannotParseProfile as cpe: sys.stderr.write( "Failed to run:\nCould not parse profile %s as it is not valid YAML\n%s\n" % (cpe.filepath, cpe.get_parse_message())) sys.exit(1) except ProfileNotFound as nfe: sys.stderr.write( "Failed to run:\nCould not find profile %s. Search path: %s\n" % (nfe.name, ':'.join(nfe.profile_path))) sys.exit(1) else: return profile, strictness
def _get_profile(self, path, config): # Use the specified profiles profile_provided = False if len(config.profiles) > 0: profile_provided = True cmdline_implicit = [] # if there is a '.prospector.ya?ml' or a '.prospector/prospector.ya?ml' or equivalent landscape config # file then we'll include that profile_name = None if not profile_provided: for possible_profile in AUTO_LOADED_PROFILES: prospector_yaml = os.path.join(path, possible_profile) if os.path.exists(prospector_yaml) and os.path.isfile(prospector_yaml): profile_provided = True profile_name = possible_profile break strictness = None if profile_provided: if profile_name is None: profile_name = config.profiles[0] extra_profiles = config.profiles[1:] else: extra_profiles = config.profiles strictness = 'from profile' else: # Use the preconfigured prospector profiles profile_name = 'default' extra_profiles = [] if config.doc_warnings is not None and config.doc_warnings: cmdline_implicit.append('doc_warnings') if config.test_warnings is not None and config.test_warnings: cmdline_implicit.append('test_warnings') if config.no_style_warnings is not None and config.no_style_warnings: cmdline_implicit.append('no_pep8') if config.full_pep8 is not None and config.full_pep8: cmdline_implicit.append('full_pep8') if config.member_warnings is not None and config.member_warnings: cmdline_implicit.append('member_warnings') # Use the strictness profile only if no profile has been given if config.strictness is not None and config.strictness: cmdline_implicit.append('strictness_%s' % config.strictness) strictness = config.strictness # the profile path is # * anything provided as an argument # * a directory called .prospector in the check path # * the check path # * prospector provided profiles profile_path = config.profile_path prospector_dir = os.path.join(path, '.prospector') if os.path.exists(prospector_dir) and os.path.isdir(prospector_dir): profile_path.append(prospector_dir) profile_path.append(path) profile_path.append(BUILTIN_PROFILE_PATH) try: forced_inherits = cmdline_implicit + extra_profiles profile = ProspectorProfile.load(profile_name, profile_path, forced_inherits=forced_inherits) except CannotParseProfile as cpe: sys.stderr.write("Failed to run:\nCould not parse profile %s as it is not valid YAML\n%s\n" % (cpe.filepath, cpe.get_parse_message())) sys.exit(1) except ProfileNotFound as nfe: sys.stderr.write("Failed to run:\nCould not find profile %s. Search path: %s\n" % (nfe.name, ':'.join(nfe.profile_path))) sys.exit(1) else: return profile, strictness
def test_optional_present(self): # optional does not mean ignore so verify that values are inherited if present profile = ProspectorProfile.load("optional_present", self._profile_path) self.assertFalse(profile.is_tool_enabled("dodgy"))
def _load(self, testname): profile_path = self._profile_path + [self._example_path(testname)] return ProspectorProfile.load("start", profile_path)
def test_load_plugins(self): profile = ProspectorProfile.load('pylint_load_plugins', self._profile_path) self.assertEqual(['first_plugin', 'second_plugin'], profile.pylint['load-plugins'])
def test_disable_tool_inheritance(self): profile = ProspectorProfile.load('pep8_and_pylint_disabled', self._profile_path) self.assertFalse(profile.is_tool_enabled('pylint')) self.assertFalse(profile.is_tool_enabled('pep8'))
def test_strictness_equivalence(self): profile = self._load('strictness_equivalence') medium_strictness = ProspectorProfile.load('strictness_medium', self._profile_path) self.assertListEqual(sorted(profile.pylint['disable']), sorted(medium_strictness.pylint['disable']))
def test_disable_tool(self): profile = ProspectorProfile.load("pylint_disabled", self._profile_path) self.assertFalse(profile.is_tool_enabled("pylint")) self.assertTrue(profile.is_tool_enabled("pep8") is None)
def test_load_plugins(self): profile = ProspectorProfile.load("pylint_load_plugins", self._profile_path) self.assertEqual(["first_plugin", "second_plugin"], profile.pylint["load-plugins"])
def _load(self, testname): profile_path = self._profile_path + [self._example_path(testname)] return ProspectorProfile.load('start', profile_path)
def test_disable_tool_inheritance(self): profile = ProspectorProfile.load("pep8_and_pylint_disabled", self._profile_path) self.assertFalse(profile.is_tool_enabled("pylint")) self.assertFalse(profile.is_tool_enabled("pep8"))
def test_optional_missing(self): # ensure loads without an exception to verify that a missing inherits works fine profile = ProspectorProfile.load("optional_missing", self._profile_path) self.assertTrue(profile.is_tool_enabled("dodgy"))