def test_dependency_resolving(self): sections = {"test": self.section} self.section['bears'] = "DependentBear" with simulate_console_inputs("True"), bear_test_module(): fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(bool(self.section["use_spaces"]), True)
def test_dependency_resolving(self): sections = {'test': self.section} self.section['bears'] = 'DependentBear' with simulate_console_inputs('True'), bear_test_module(): fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(bool(self.section['use_spaces']), True)
def test_fill_settings(self): sections = {'test': self.section} with simulate_console_inputs() as generator: fill_settings(sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(generator.last_input, -1) self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs('True'), bear_test_module(): local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(len(local_bears['test']), 1) self.assertEqual(len(global_bears['test']), 0) self.assertEqual(bool(self.section['use_spaces']), True) self.assertEqual(len(self.section.contents), 3)
def generate_settings(project_dir, project_files, ignore_globs, relevant_bears, incomplete_sections=False): """ Generates the settings for the given project. :param project_dir: Full path of the user's project directory. :param project_files: A list of file paths matched in the user's project directory. :param ignore_globs: The list of ignore glob expressions. :param relevant_bears: A dict with language name as key and bear classes as value. :param incomplete_sections: When bears with non optional settings are found, user is asked for setting value of non optional bears and then a ``Section`` object having ``files`` field, ``bears`` field and settings, is returned. If incomplete_sections is set to ``True``, then no user input will be asked and a ``Section`` object having only the ``files`` and ``bears`` field will be returned. In CI mode, bears with non optional setting are not added in coafile. But if incomplete_sections is set to ``True`` in CI mode, then those bears are also added in the coafile. :return: A dict with section name as key and a ``Section`` object as value. """ lang_map = {lang.lower(): lang for lang in relevant_bears} lang_files = split_by_language(project_files) extset = get_extensions(project_files) settings = OrderedDict() settings["default"] = generate_section( "default", [ext for lang in lang_files for ext in extset[lang]], relevant_bears[lang_map["all"]]) ignored_files = generate_ignore_field(project_dir, lang_files.keys(), extset, ignore_globs) if ignored_files: settings["default"]["ignore"] = ignored_files for lang in lang_files: if lang != "unknown" and lang != "all": settings[lang_map[lang]] = generate_section( lang, extset[lang], relevant_bears[lang_map[lang]]) log_printer = LogPrinter(ConsolePrinter()) if not incomplete_sections: fill_settings(settings, acquire_settings, log_printer) return settings
def gather_configuration(acquire_settings, log_printer=None, arg_list=None, arg_parser=None, args=None): """ Loads all configuration files, retrieves bears and all needed settings, saves back if needed and warns about non-existent targets. This function: - Reads and merges all settings in sections from - Default config - User config - Configuration file - CLI - Collects all the bears - Fills up all needed settings - Writes back the new sections to the configuration file if needed - Gives all information back to caller :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param log_printer: The log printer to use for logging. The log level will be adjusted to the one given by the section. :param arg_list: CLI args to use :param arg_parser: Instance of ArgParser that is used to parse none-setting arguments. :param args: Alternative pre-parsed CLI arguments. :return: A tuple with the following contents: - A dictionary with the sections - Dictionary of list of local bears for each section - Dictionary of list of global bears for each section - The targets list """ if args is None: # Note: arg_list can also be []. Hence we cannot use # `arg_list = arg_list or default_list` arg_list = sys.argv[1:] if arg_list is None else arg_list sections, targets = load_configuration(arg_list, arg_parser=arg_parser, args=args) _set_section_language(sections) aspectize_sections(sections) local_bears, global_bears = fill_settings(sections, acquire_settings) save_sections(sections) warn_nonexistent_targets(targets, sections) return (sections, local_bears, global_bears, targets)
def main(): # Note: We parse the args here once to check whether to show bears or not. arg_parser = default_arg_parser() args = arg_parser.parse_args() console_printer = ConsolePrinter() if args.show_bears or args.show_all_bears: log_printer = LogPrinter(console_printer) sections, _ = load_configuration(arg_list=None, log_printer=log_printer) if args.show_all_bears: local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) else: # We ignore missing settings as it's not important. local_bears, global_bears = fill_settings( sections, acquire_settings=lambda *args, **kwargs: {}, log_printer=log_printer) show_bears(local_bears, global_bears, args.show_all_bears, console_printer) return 0 partial_print_sec_beg = functools.partial( print_section_beginning, console_printer) results, exitcode, _ = run_coala( print_results=print_results, acquire_settings=acquire_settings, print_section_beginning=partial_print_sec_beg, nothing_done=nothing_done) return exitcode
def test_fill_settings_section_match_with_conflicts(self): self.section = Section('test1') self.section["files"] = "hello.py" sections = {'test1': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs("False") as generator, \ bear_test_module(), retrieve_stdout() as sio: with generate_files([".editorconfig", "hello.py"], [editorconfig_4, "pass"], self.project_dir): extracted_info = collect_info(self.project_dir) local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info=extracted_info) self.assertEqual(len(local_bears['test1']), 1) self.assertEqual(len(global_bears['test1']), 0) prompt_msg = ( 'coala-quickstart has detected multiple potential values ' 'for the setting "use_spaces"') self.assertIn(prompt_msg, sio.getvalue()) self.assertEqual(generator.last_input, 0) self.assertEqual(bool(self.section['use_spaces']), False)
def gather_configuration(acquire_settings, log_printer, arg_list=None, arg_parser=None, args=None): """ Loads all configuration files, retrieves bears and all needed settings, saves back if needed and warns about non-existent targets. This function: - Reads and merges all settings in sections from - Default config - User config - Configuration file - CLI - Collects all the bears - Fills up all needed settings - Writes back the new sections to the configuration file if needed - Gives all information back to caller :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param log_printer: The log printer to use for logging. The log level will be adjusted to the one given by the section. :param arg_list: CLI args to use :param arg_parser: Instance of ArgParser that is used to parse none-setting arguments. :param args: Alernative pre-parsed CLI arguments. :return: A tuple with the following contents: - A dictionary with the sections - Dictionary of list of local bears for each section - Dictionary of list of global bears for each section - The targets list """ if args is None: # Note: arg_list can also be []. Hence we cannot use # `arg_list = arg_list or default_list` arg_list = sys.argv[1:] if arg_list is None else arg_list sections, targets = load_configuration(arg_list, log_printer, arg_parser, args=args) aspectize_sections(sections) local_bears, global_bears = fill_settings(sections, acquire_settings, log_printer) save_sections(sections) warn_nonexistent_targets(targets, sections, log_printer) return (sections, local_bears, global_bears, targets)
def gather_configuration(acquire_settings, log_printer, autoapply=None, arg_list=None, arg_parser=None): """ Loads all configuration files, retrieves bears and all needed settings, saves back if needed and warns about non-existent targets. This function: - Reads and merges all settings in sections from - Default config - User config - Configuration file - CLI - Collects all the bears - Fills up all needed settings - Writes back the new sections to the configuration file if needed - Gives all information back to caller :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param log_printer: The log printer to use for logging. The log level will be adjusted to the one given by the section. :param autoapply: Set whether to autoapply patches. This is overridable via any configuration file/CLI. :param arg_list: CLI args to use :return: A tuple with the following contents: - A dictionary with the sections - Dictionary of list of local bears for each section - Dictionary of list of global bears for each section - The targets list """ # Note: arg_list can also be []. Hence we cannot use # `arg_list = arg_list or default_list` arg_list = sys.argv[1:] if arg_list is None else arg_list sections, targets = load_configuration(arg_list, log_printer, arg_parser) local_bears, global_bears = fill_settings(sections, acquire_settings, log_printer) save_sections(sections) warn_nonexistent_targets(targets, sections, log_printer) if autoapply is not None: if not autoapply and 'autoapply' not in sections['default']: sections['default']['autoapply'] = "False" return (sections, local_bears, global_bears, targets)
def test_fill_settings_section_match_no_conflicts(self): self.section = Section('test') self.section["files"] = "*.py" sections = {'test': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs() as generator, bear_test_module(): with generate_files([".editorconfig", "hello.py"], [editorconfig_3, "pass"], self.project_dir) as gen_files: extracted_info = collect_info(self.project_dir) local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info=extracted_info) self.assertEqual(len(local_bears['test']), 1) self.assertEqual(len(global_bears['test']), 0) # The value for the setting is automatically taken # from .editorconfig file. self.assertEqual(generator.last_input, -1) self.assertEqual(bool(self.section['use_spaces']), True)
def test_fill_settings(self): sections = {"test": self.section} with simulate_console_inputs() as generator: fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(generator.last_input, -1) self.section.append(Setting("bears", "SpaceConsistencyTestBear")) with simulate_console_inputs("True"), bear_test_module(): local_bears, global_bears = fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(len(local_bears["test"]), 1) self.assertEqual(len(global_bears["test"]), 0) self.assertEqual(bool(self.section["use_spaces"]), True) self.assertEqual(len(self.section.contents), 3)
def test_fill_section_boolean_setting(self): self.section = Section('test') sections = {'test': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs(" hell yeah!!! ") as generator, \ bear_test_module(): local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(generator.last_input, 0) self.assertEqual(bool(self.section['use_spaces']), True) self.section = Section('test') sections = {'test': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs("not in a million years") as generator, \ bear_test_module(): local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(generator.last_input, 0) self.assertEqual(bool(self.section['use_spaces']), False) self.section = Section('test') sections = {'test': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs("don't know", "nah") as generator, \ bear_test_module(): local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(generator.last_input, 1) self.assertEqual(bool(self.section['use_spaces']), False)
def test_fill_settings(self): sections = {"test": self.section} with simulate_console_inputs() as generator: fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(generator.last_input, -1) self.section.append(Setting("bears", "SpaceConsistencyBear")) with simulate_console_inputs("True"): local_bears, global_bears = fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(len(local_bears["test"]), 1) self.assertEqual(len(global_bears["test"]), 0) self.assertEqual(bool(self.section["use_spaces"]), True) self.assertEqual(len(self.section.contents), 3)
def test_fill_settings(self): sections = {'test': self.section} with simulate_console_inputs() as generator: fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(generator.last_input, -1) self.section.append(Setting('bears', 'SpaceConsistencyTestBear')) with simulate_console_inputs('True'), bear_test_module(): local_bears, global_bears = fill_settings(sections, acquire_settings, self.log_printer) self.assertEqual(len(local_bears['test']), 1) self.assertEqual(len(global_bears['test']), 0) self.assertEqual(bool(self.section['use_spaces']), True) self.assertEqual(len(self.section.contents), 3)
def test_fill_settings(self): sections = {'test': self.section} targets = [] with simulate_console_inputs() as generator: fill_settings(sections, targets, acquire_settings, self.log_printer) self.assertEqual(generator.last_input, -1) self.section.append(Setting('bears', 'SpaceConsistencyTestBear')) with simulate_console_inputs('True'), bear_test_module(): local_bears, global_bears = fill_settings(sections, targets, acquire_settings, self.log_printer) self.assertEqual(len(local_bears['test']), 1) self.assertEqual(len(global_bears['test']), 0) self.assertEqual(bool(self.section['use_spaces']), True) self.assertEqual(len(self.section.contents), 3)
def generate_settings(project_dir, project_files, ignore_globs, relevant_bears): """ Generates the settings for the given project. :param project_dir: Full path of the user's project directory. :param project_files: A list of file paths matched in the user's project directory. :param ignore_globs: The list of ignore glob expressions. :param relevant_bears: A dict with language name as key and bear classes as value. :return: A dict with section name as key and a ``Section`` object as value. """ lang_map = {lang.lower(): lang for lang in relevant_bears} lang_files = split_by_language(project_files) extset = get_extensions(project_files) settings = OrderedDict() settings["default"] = generate_section( "default", [ext for lang in lang_files for ext in extset[lang]], relevant_bears[lang_map["all"]]) settings["default"]["ignore"] = generate_ignore_field( project_dir, lang_files.keys(), extset, ignore_globs) for lang in lang_files: if lang != "unknown" and lang != "all": settings[lang_map[lang]] = generate_section( lang, extset[lang], relevant_bears[lang_map[lang]]) log_printer = LogPrinter(ConsolePrinter()) fill_settings(settings, acquire_settings, log_printer) return settings
def test_language_setting_autofill(self): self.section = Section('ruby') sections = {'ruby': self.section} self.section.append(Setting('bears', 'LanguageSettingBear')) with simulate_console_inputs() as generator, bear_test_module(): local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info={}) self.assertEqual(generator.last_input, -1) self.assertEqual(str(self.section['language']), 'ruby')
def gather_configuration(acquire_settings, log_printer, arg_list=sys.argv[1:]): """ Loads all configuration files, retrieves bears and all needed settings, saves back if needed and warns about non-existent targets. This function: - Reads and merges all settings in sections from - Default config - User config - Configuration file - CLI - Collects all the bears - Fills up all needed settings - Writes back the new sections to the configuration file if needed - Gives all information back to caller :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param log_printer: The log printer to use for logging. The log level will be adjusted to the one given by the section. :param arg_list: CLI args to use :return: A tuple with the following contents: * A dictionary with the sections * Dictionary of list of local bears for each section * Dictionary of list of global bears for each section * The targets list """ sections, targets = load_configuration(arg_list, log_printer) local_bears, global_bears = fill_settings(sections, acquire_settings, log_printer) save_sections(sections) warn_nonexistent_targets(targets, sections, log_printer) return (sections, local_bears, global_bears, targets)
def test_fill_settings_autofill(self): self.section = Section('test') sections = {'test': self.section} self.section.append(Setting('bears', 'BearC')) with simulate_console_inputs() as generator, bear_test_module(): with generate_files([".editorconfig"], [editorconfig_1], self.project_dir) as gen_files: extracted_info = collect_info(self.project_dir) local_bears, global_bears = fill_settings( sections, acquire_settings, self.log_printer, fill_section_method=fill_section, extracted_info=extracted_info) self.assertEqual(len(local_bears['test']), 1) self.assertEqual(len(global_bears['test']), 0) # The value for the setting is automatically taken # from .editorconfig file. self.assertEqual(generator.last_input, -1) self.assertEqual(bool(self.section['use_spaces']), False)
def generate_settings(project_dir, project_files, ignore_globs, relevant_bears, extracted_info, incomplete_sections=False, log_printer=None): """ Generates the settings for the given project. :param project_dir: Full path of the user's project directory. :param project_files: A list of file paths matched in the user's project directory. :param ignore_globs: The list of ignore glob expressions. :param relevant_bears: A dict with language name as key and bear classes as value. :param extracted_info: A list information extracted from the project files by ``InfoExtractor`` classes. :param incomplete_sections: When bears with non optional settings are found, user is asked for setting value of non optional bears and then a ``Section`` object having ``files`` field, ``bears`` field and settings, is returned. If incomplete_sections is set to ``True``, then no user input will be asked and a ``Section`` object having only the ``files`` and ``bears`` field will be returned. In CI mode, bears with non optional setting are not added in coafile. But if incomplete_sections is set to ``True`` in CI mode, then those bears are also added in the coafile. :return: A dict with section name as key and a ``Section`` object as value. """ lang_map = {lang.lower(): lang for lang in relevant_bears} lang_files = split_by_language(project_files) extset = get_extensions(project_files) settings = OrderedDict() settings['all'] = generate_section( 'all', [ext for lang in lang_files for ext in extset[lang]], relevant_bears[lang_map['all']]) ignored_files = generate_ignore_field(project_dir, lang_files.keys(), extset, ignore_globs) if ignored_files: settings['all']['ignore'] = ignored_files for lang in lang_files: if lang != 'unknown' and lang != 'all': settings['all.' + lang_map[lang]] = generate_section( 'all.' + lang, extset[lang], relevant_bears[lang_map[lang]]) if not incomplete_sections: fill_settings(settings, acquire_settings, log_printer, fill_section_method=fill_section, extracted_info=extracted_info) return settings