def test_logging(self): uut = LogPrinter(timestamp_format='') uut.logger = mock.MagicMock() uut.log_message(self.log_message) msg = Constants.COMPLEX_TEST_STRING uut.logger.log.assert_called_with(logging.ERROR, msg) uut = LogPrinter(log_level=LOG_LEVEL.DEBUG) uut.logger = mock.MagicMock() uut.log(LOG_LEVEL.ERROR, Constants.COMPLEX_TEST_STRING) uut.logger.log.assert_called_with(logging.ERROR, msg) uut.debug(Constants.COMPLEX_TEST_STRING, 'd') uut.logger.log.assert_called_with(logging.DEBUG, msg + ' d') uut.log_level = LOG_LEVEL.DEBUG uut.log_exception('Something failed.', NotImplementedError(msg)) uut.logger.log.assert_any_call(logging.ERROR, 'Something failed.') uut.logger.log.assert_called_with( logging.INFO, 'Exception was:\n{exception}: {msg}'.format( exception='NotImplementedError', msg=msg))
def test_run_coala_no_autoapply(self, debug=False): with bear_test_module(), \ prepare_file(['#fixme '], None) as (lines, filename): self.assertEqual( 1, len( run_coala(console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=('-c', os.devnull, '-f', filename, '-b', 'SpaceConsistencyTestBear', '--apply-patches', '-S', 'use_spaces=yeah'), autoapply=False, debug=debug)[0]['cli'])) self.assertEqual( 0, len( run_coala(console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=('-c', os.devnull, '-f', filename, '-b', 'SpaceConsistencyTestBear', '--apply-patches', '-S', 'use_spaces=yeah'), debug=debug)[0]['cli']))
def setUp(self): config_path = os.path.abspath(os.path.join( os.path.dirname(__file__), 'section_executor_test_files', '.coafile')) self.testcode_c_path = os.path.join(os.path.dirname(config_path), 'testcode.c') self.unreadable_path = os.path.join(os.path.dirname(config_path), 'unreadable') self.result_queue = queue.Queue() self.queue = queue.Queue() self.log_queue = queue.Queue() self.console_printer = ConsolePrinter() log_printer = LogPrinter(ConsolePrinter()) self.log_printer = ProcessingTestLogPrinter(self.log_queue) (self.sections, self.local_bears, self.global_bears, targets) = gather_configuration(lambda *args: True, log_printer, arg_list=['--config', re.escape(config_path)]) self.assertEqual(len(self.local_bears['cli']), 1) self.assertEqual(len(self.global_bears['cli']), 1) self.assertEqual(targets, [])
def main(log_printer=None, section: Section = None): start_path = get_config_directory(section) log_printer = log_printer or LogPrinter(ConsolePrinter()) if start_path is None: log_printer.err("Can only delete .orig files if .coafile is found") return 255 orig_files = Globbing.glob( os.path.abspath(os.path.join(start_path, '**', '*.orig'))) not_deleted = 0 for ofile in orig_files: log_printer.info("Deleting old backup file... " + os.path.relpath(ofile)) try: os.remove(ofile) except OSError as oserror: not_deleted += 1 log_printer.warn("Couldn't delete... {}. {}".format( os.path.relpath(ofile), oserror.strerror)) if not_deleted: log_printer.warn( str(not_deleted) + " .orig backup files could not be" " deleted, possibly because you lack the permission" " to do so. coala may not be able to create" " backup files when patches are applied.") return 0
def test_profiler_dependency(self, debug=False): with bear_test_module(): with prepare_file(['#fixme '], None) as (lines, filename): results = run_coala(console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=( '-c', os.devnull, '-f', filename, '-b', 'DependentBear', '-S', 'use_spaces=yeah', '--profile', 'profiled_bears', ), autoapply=False, debug=debug) cli_result = results[0]['cli'] self.assertEqual(len(cli_result), 1) profiled_files = os.listdir('profiled_bears') self.assertEqual(len(profiled_files), 1) self.assertEqual(profiled_files[0], 'cli_SpaceConsistencyTestBear.prof') shutil.rmtree('profiled_bears')
def apply(self, result, original_file_dict, file_diff_dict): """ (G)enerate patches """ console_printer = ConsolePrinter() log_printer = LogPrinter() to_filename = sorted( result.diffs.items())[OBJECT_INDEX][FILENAME_INDEX] filtered_bears = filter_bears(find_language(to_filename)) filtered_bears.insert(0, DefaultBear()) possible_options = [b.name for b in filtered_bears] console_printer.print('[{:>4}] *0. Do Nothing'.format('')) # Let the user choose a bear that wants to apply on the files for i, action in enumerate(possible_options, 1): show_possibilities(console_printer, i, action) choose_action = str(input('[{:>4}] Enter a number: '.format(''))) if choose_action is '' or choose_action is '0': return False choose_action = int(choose_action) chosen_bear = [possible_options[choose_action - 1]] return mode_normal(console_printer, log_printer, create_arg_parser([to_filename], chosen_bear), debug=False)
def main(log_printer=None, section: Section = None): configure_logging() start_path = get_config_directory(section) log_printer = (LogPrinter(ConsolePrinter()) if log_printer is None else log_printer) if start_path is None: return 255 # start_path may have unintended glob characters orig_files = Globbing.glob( os.path.join(glob_escape(start_path), '**', '*.orig')) not_deleted = 0 for ofile in orig_files: log_printer.info('Deleting old backup file... ' + os.path.relpath(ofile)) try: os.remove(ofile) except OSError as oserror: not_deleted += 1 log_printer.warn("Couldn't delete {}. {}".format( os.path.relpath(ofile), oserror.strerror)) if not_deleted: log_printer.warn( str(not_deleted) + ' .orig backup files could not be' ' deleted, possibly because you lack the permission' ' to do so. coala may not be able to create' ' backup files when patches are applied.') return 0
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 setUp(self): current_dir = os.path.split(__file__)[0] self.caching_test_dir = os.path.join(current_dir, 'caching_testfiles') self.log_printer = LogPrinter(NullPrinter()) self.cache = FileCache(self.log_printer, 'coala_test', flush_cache=True)
def test_file_cache_proxy_integration(self, debug=False): with bear_test_module(), \ prepare_file(['disk-copy\n'], None) as (_, filename): memory_data = 'in-memory\n' proxy = FileProxy(filename, None, memory_data) proxymap = FileProxyMap([proxy]) self.cache.set_proxymap(proxymap) results, exitcode, file_dicts = run_coala( console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=( '-c', os.devnull, '-f', filename, '-b', 'TestBear', ), autoapply=False, debug=debug, cache=self.cache) self.assertEqual(exitcode, 0) self.assertEqual(len(results), 1) # run_coala() output's name is always lower case self.assertEqual(file_dicts['cli'][filename.lower()], (memory_data, ))
def main(): try: console_printer = ConsolePrinter() log_printer = LogPrinter(console_printer) # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() if args.show_bears: sections, _ = load_configuration(arg_list=None, log_printer=log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) if args.filter_by_language: local_bears = filter_section_bears_by_languages( local_bears, args.filter_by_language) global_bears = filter_section_bears_by_languages( global_bears, args.filter_by_language) show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer) return 0 except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception, log_printer) 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 generate_ignore_field(project_dir, languages, extset, ignore_globs): """ Generate the ignore field for the ``default`` section. :param project_dir: Full path of the user's project directory. :param languages: A list of languages present in the project. :param extset: A dict with language name as key and a set of extensions as value. This includes only those extensions used by the project. :return: A comma-separated string containing the globs to ignore. """ null_printer = LogPrinter(NullPrinter()) all_files = set( collect_files("**", null_printer, ignored_file_paths=ignore_globs)) ignores = [] for glob in ignore_globs: gitignore_files = { file for file in collect_files([glob], null_printer) } if not all_files.isdisjoint(gitignore_files): ignores.append(os.path.relpath(glob, project_dir)) return ", ".join(ignores)
def main(): arg_parser = _get_arg_parser() args = arg_parser.parse_args() printer = ConsolePrinter() log_printer = LogPrinter(printer) project_dir = os.getcwd() if not args.non_interactive: print_welcome_message(printer) project_dir = ask_question("What is your project directory?", default=project_dir, typecast=valid_path) project_files, ignore_globs = get_project_files(log_printer, printer, project_dir) used_languages = list(get_used_languages(project_files)) print_used_languages(printer, used_languages) relevant_bears = filter_relevant_bears(used_languages, arg_parser) print_relevant_bears(printer, relevant_bears) if args.non_interactive: unusable_bears = get_non_optional_settings_bears(relevant_bears) remove_unusable_bears(relevant_bears, unusable_bears) print_relevant_bears(printer, relevant_bears, 'usable') settings = generate_settings(project_dir, project_files, ignore_globs, relevant_bears) write_coafile(printer, project_dir, settings)
def test_section_deprecation(self): section = Section('') log_printer = LogPrinter(NullPrinter()) with LogCapture() as capture: fail_acquire_settings(log_printer, {}, section) capture.check( ('root', 'WARNING', 'fail_acquire_settings: section parameter ' 'is deprecated.'))
def test_(self): log_printer = LogPrinter(NullPrinter()) self.assertRaises(TypeError, fail_acquire_settings, log_printer, None) self.assertRaises(AssertionError, fail_acquire_settings, log_printer, {"setting": ["description", "bear"]}) self.assertEqual(fail_acquire_settings(log_printer, {}), None)
def setUp(self): self.log_printer = LogPrinter(NullPrinter()) # Needed so coala doesn't error out self.min_args = ['-f', '*.java', '-b', 'JavaTestBear'] self.original_user_coafile = Constants.user_coafile self.original_system_coafile = Constants.system_coafile
def get_all_bears_names(): from coalib.settings.Section import Section printer = LogPrinter(NullPrinter()) local_bears, global_bears = collect_bears( Section("").bear_dirs(), ["**"], [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL], printer, warn_if_unused_glob=False) return [bear.name for bear in itertools.chain(local_bears, global_bears)]
def main(): configure_logging() try: console_printer = ConsolePrinter() log_printer = LogPrinter(console_printer) # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() # Defer imports so if e.g. --help is called they won't be run from coalib.coala_modes import (mode_format, mode_json, mode_non_interactive, mode_normal) from coalib.output.ConsoleInteraction import ( show_bears, show_language_bears_capabilities) console_printer = ConsolePrinter(print_colored=not args.no_color) configure_logging(not args.no_color) if args.json: # needs to be checked in order to display bears in json return mode_json(args) if args.show_bears: from coalib.settings.ConfigurationGathering import ( get_filtered_bears) local_bears, global_bears = get_filtered_bears( args.filter_by_language, log_printer) show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer) return 0 elif args.show_capabilities: from coalib.collecting.Collectors import ( filter_capabilities_by_languages) from coalib.settings.ConfigurationGathering import ( get_filtered_bears) local_bears, global_bears = get_filtered_bears( args.filter_by_language, log_printer) capabilities = filter_capabilities_by_languages( local_bears, args.show_capabilities) show_language_bears_capabilities(capabilities, console_printer) return 0 except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception, log_printer) if args.format: return mode_format() if args.non_interactive: return mode_non_interactive(console_printer, args) return mode_normal(console_printer, log_printer)
def setUp(self): self.project_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), "project_dir") os.makedirs(self.project_dir, exist_ok=True) self.arg_parser = _get_arg_parser() self.printer = ConsolePrinter() self.log_printer = LogPrinter(self.printer) self.old_argv = deepcopy(sys.argv) del sys.argv[1:]
def test_(self): log_printer = LogPrinter(NullPrinter()) section = Section('') self.assertRaises(TypeError, fail_acquire_settings, log_printer, None, section) self.assertRaises(AssertionError, fail_acquire_settings, log_printer, {'setting': ['description', 'bear']}, section) self.assertEqual(fail_acquire_settings(log_printer, {}, section), None, section)
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 setUp(self): self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile') with open(self.file, 'w', encoding='utf-8') as file: file.write(self.example_file) self.log_printer = LogPrinter() self.write_file_name = os.path.join(tempfile.gettempdir(), 'ConfWriterTestFile') self.uut = ConfWriter(self.write_file_name)
def test_tagging(self): log_printer = LogPrinter(NullPrinter()) execute_coala(coala_ci.main, "coala-ci", 'docs', "-S", "tag=test_tag", "-c", self.coafile) tag_path = get_tag_path("test_tag", self.unescaped_coafile, log_printer) self.assertTrue(os.path.exists(tag_path)) execute_coala(coala_ci.main, "coala-ci", 'docs', "-S", "dtag=test_tag", "-c", self.coafile) self.assertFalse(os.path.exists(tag_path))
def mode_json(args): import json from coalib.coala_main import run_coala from coalib.misc.DictUtilities import inverse_dicts from coalib.misc.Exceptions import get_exitcode from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder from coalib.output.printers.LogPrinter import LogPrinter from coalib.settings.ConfigurationGathering import get_filtered_bears if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results = [] if args.show_bears: try: local_bears, global_bears = get_filtered_bears( args.filter_by_language, LogPrinter()) bears = inverse_dicts(local_bears, global_bears) for bear, _ in sorted(bears.items(), key=lambda bear_tuple: bear_tuple[0].name): results.append(bear) except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception) else: results, exitcode, _ = run_coala() retval = {'bears': results} if args.show_bears else {'results': results} if args.log_json: retval['logs'] = [ json.loads(line) for line in log_stream.getvalue().splitlines() ] if args.output: filename = str(args.output[0]) with open(filename, 'w+') as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) else: print( json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': '))) return 0 if args.show_bears else exitcode
def test_run_coala_bear_run_raises(self): configure_logging() with bear_test_module(), \ prepare_file(['#fixme '], None) as (lines, filename), \ self.assertRaisesRegex( RuntimeError, r"^That's all the RaiseTestBear can do\.$"): run_coala(console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=('-c', os.devnull, '-f', re.escape(filename), '-b', 'RaiseTestBear'), debug=True)
def get_all_bears(): """ Get a ``list`` of all available bears. """ from coalib.settings.Section import Section printer = LogPrinter(NullPrinter()) local_bears, global_bears = collect_bears( Section('').bear_dirs(), ['**'], [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL], printer, warn_if_unused_glob=False) return list(itertools.chain(local_bears, global_bears))
def setUp(self): config_path = os.path.abspath( os.path.join(os.path.dirname(__file__), 'section_executor_test_files', '.coafile')) self.testcode_c_path = os.path.join(os.path.dirname(config_path), 'testcode.c') self.unreadable_path = os.path.join(os.path.dirname(config_path), 'unreadable') factory_test_path = os.path.abspath( os.path.join(os.path.dirname(__file__), 'file_factory_test_files')) self.factory_test_file = os.path.join(factory_test_path, 'factory_test.txt') self.a_bear_test_path = os.path.join(factory_test_path, 'a_bear_test.txt') self.b_bear_test_path = os.path.join(factory_test_path, 'b_bear_test.txt') self.c_bear_test_path = os.path.join(factory_test_path, 'c_bear_test.txt') self.d_bear_test_path = os.path.join(factory_test_path, 'd_bear_test.txt') self.e_bear_test_path = os.path.join(factory_test_path, 'e_bear_test.txt') self.n_bear_test_path = os.path.join(factory_test_path, 'n_bear_test.txt') self.n_bear_test_path_2 = os.path.join(factory_test_path, 'n_bear_test2.txt') self.x_bear_test_path = os.path.join(factory_test_path, 'x_bear_test.txt') filename_list = [ self.factory_test_file, self.a_bear_test_path, self.b_bear_test_path, self.c_bear_test_path, self.d_bear_test_path, self.e_bear_test_path, self.n_bear_test_path, self.n_bear_test_path_2, self.x_bear_test_path ] self.file_dict = get_file_dict(filename_list) self.result_queue = queue.Queue() self.queue = queue.Queue() self.log_queue = queue.Queue() self.console_printer = ConsolePrinter() log_printer = LogPrinter(ConsolePrinter()) self.log_printer = ProcessingTestLogPrinter(self.log_queue) (self.sections, self.local_bears, self.global_bears, targets) = gather_configuration(lambda *args: True, log_printer, arg_list=['--config', config_path]) self.assertEqual(len(self.local_bears['cli']), 1) self.assertEqual(len(self.global_bears['cli']), 1) self.assertEqual(targets, [])
def main(): configure_logging() try: console_printer = ConsolePrinter() log_printer = LogPrinter(console_printer) # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() console_printer = ConsolePrinter(print_colored=not args.no_color) if args.show_bears: from coalib.settings.ConfigurationGathering import ( get_filtered_bears) local_bears, global_bears = get_filtered_bears( args.filter_by_language, log_printer) show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer) return 0 elif args.show_capabilities: from coalib.collecting.Collectors import ( filter_capabilities_by_languages) from coalib.settings.ConfigurationGathering import ( get_filtered_bears) local_bears, global_bears = get_filtered_bears( args.filter_by_language, log_printer) capabilities = filter_capabilities_by_languages( local_bears, args.show_capabilities) show_language_bears_capabilities(capabilities, console_printer) return 0 except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception, log_printer) import functools from coalib.coala_main import run_coala 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, console_printer=console_printer) return exitcode
def test_logged_error_causes_non_zero_exitcode(self): configure_logging() with bear_test_module(), \ prepare_file(['#fixme '], None) as (lines, filename): _, exitcode, _ = run_coala(console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=('-c', os.devnull, '-f', filename, '-b', 'ErrorTestBear'), autoapply=False) assert exitcode == 1
def read_coafile(self): if os.path.isfile(self.src + '/.coafile'): self.sections_dict = load_configuration( ["-c", self.src + '/.coafile'], LogPrinter(NullPrinter()))[0] for section in self.sections_dict: section_row = self.add_section(name=section) for setting in self.sections_dict[section].contents: if "comment" in setting: continue self.section_stack_map[section_row].add_setting( self.sections_dict[section].contents[setting]) self.section_stack_map[section_row].add_setting()