def _test_visits_file_with_ast_tree_and_relative_path( mock_file_contents, expected_file_contents_for_parsing, ): mock_tree = astroid.Module(doc="", name="mock_module") file_opener = MockFileOpener(mock_file_contents) file_walker = MockFileWalker(MOCK_FILES) parser = MockParser(mock_tree) analyzer = Analyzer( MOCK_BASE_DIRECTORY_PATH, file_walker, file_opener.open, parser.parse ) analyzer.override_node_visitor_master(MockNodeMaster) analyzer.run(Stats()) assert len(file_opener.opens) == len(MOCK_FILES) assert len(MockNodeMaster.visited) == len(MOCK_FILES) assert len(parser.parses) == len(MOCK_FILES) for i, (tree, file_path) in enumerate(MockNodeMaster.visited): assert tree == mock_tree expected_file_path = RELATIVE_MOCK_FILE_PATHS[i] assert file_path == expected_file_path for i, (args, _) in enumerate(parser.parses): file_contents_received_for_parsing, file_name = args assert file_contents_received_for_parsing == _as_unicode( expected_file_contents_for_parsing ) assert file_name == MOCK_FILE_NAMES[i]
def test_multiple_modules_multiple_functions( six_checker: SixEnsureStrBinChecker, modules: Iterable[Tuple[str, str, bool]]) -> None: for import_code, call_code, ref_value in modules: node = astroid.extract_node(import_code) node.parent = astroid.Module("module ", None) six_checker.visit_module(node.parent) six_checker.visit_importfrom(node) node = astroid.extract_node(call_code) assert (six_checker._visit_call(node.value if isinstance( node, astroid.node_classes.Assign) else node) is ref_value)
def test_multiple_modules_typing( namedtuple_checker: TypingNamedTupleChecker, modules: Iterable[Tuple[str, str, bool]]) -> None: for import_code, call_code, ref_value in modules: node = astroid.extract_node(import_code) node.parent = astroid.Module("module ", None) namedtuple_checker.visit_module(node.parent) namedtuple_checker.visit_import(node) node = astroid.extract_node(call_code) assert (namedtuple_checker._visit_call(node.value if isinstance( node, astroid.node_classes.Assign) else node) is ref_value)
def functiondef_node(draw, name=None, annotated=False, returns=False): name = name or draw(valid_identifier()) args = draw(arguments_node(annotated)) body = [] returns_node = astroid.Return() arg_node, arg_type_node = draw( hs.sampled_from(list(zip(args.args, args.annotations)))) if returns: returns_node.postinit(arg_node) else: returns_node.postinit(const_node(None)) body.append(returns_node) node = astroid.FunctionDef(name=name) node.parent = astroid.Module('Default', None) node.postinit(args, body, None, arg_type_node) return node
def test_parse_errors_are_rethrown(): mock_tree = astroid.Module(doc="", name="mock_module") file_opener = MockFileOpener('') file_walker = MockFileWalker(MOCK_FILES) parser = MockErrorParser(mock_tree) analyzer = Analyzer( MOCK_BASE_DIRECTORY_PATH, file_walker, file_opener.open, parser.parse ) analyzer.override_node_visitor_master(MockNodeMaster) expected_err = ( 'An exception occurred while parsing file: ' '.*{}'.format( re.escape(os.path.join(MOCK_DIR_NAME, MOCK_FILE_NAMES[0])), ) ) with pytest.raises(ParseException, match=expected_err): analyzer.run(Stats())
def check_similar_and_add_error_message(self, node, translation_string, **error_message): if translation_string in self._dict_of_strings: return similar_string, similarity = self.check_similar_string( translation_string) if similar_string: if isinstance(node, str): error_message["node"] = astroid.Module(node, file=node, doc=None) else: error_message["node"] = node error_message["args"] = ( translation_string, similarity * 100, similar_string, ) self.error_messages.append(error_message) return self._dict_of_strings[translation_string] = True