def test_no_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) child_dir = os.path.join(current_dir, 'section_manager_test_files', 'child_dir') with change_directory(child_dir): sections, targets = load_configuration([], self.log_printer) self.assertIn('value', sections['cli']) sections, targets = load_configuration(['--no-config'], self.log_printer) self.assertNotIn('value', sections['cli']) sections, targets = load_configuration( ['--no-config', '-S', 'use_spaces=True'], self.log_printer) self.assertIn('use_spaces', sections['cli']) self.assertNotIn('values', sections['cli']) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--save'], self.log_printer) self.assertEqual(cm.exception.code, 2) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--find-config'], self.log_printer) self.assertEqual(cm.exception.code, 2)
def test_no_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) child_dir = os.path.join(current_dir, "section_manager_test_files", "child_dir") with change_directory(child_dir): sections, targets = load_configuration([], self.log_printer) self.assertIn('value', sections["default"]) sections, targets = load_configuration( ['--no-config'], self.log_printer) self.assertNotIn('value', sections["default"]) sections, targets = load_configuration( ['--no-config', '-S', 'use_spaces=True'], self.log_printer) self.assertIn('use_spaces', sections["default"]) self.assertNotIn('values', sections["default"]) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--save'], self.log_printer) self.assertEqual(cm.exception.code, 2) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--find-config'], self.log_printer) self.assertEqual(cm.exception.code, 2)
def test_default_section_deprecation_warning(self): logger = logging.getLogger() with self.assertLogs(logger, "WARNING") as cm: # This gathers the configuration from the '.coafile' of this repo. gather_configuration(lambda *args: True, self.log_printer, arg_list=[]) self.assertIn("WARNING", cm.output[0]) with retrieve_stdout() as stdout: load_configuration(["--no-config"], self.log_printer) self.assertNotIn("WARNING", stdout.getvalue())
def test_default_section_deprecation_warning(self): logger = logging.getLogger() with self.assertLogs(logger, 'WARNING') as cm: # This gathers the configuration from the '.coafile' of this repo. gather_configuration(lambda *args: True, self.log_printer, arg_list=[]) self.assertIn('WARNING', cm.output[0]) with retrieve_stdout() as stdout: load_configuration(['--no-config'], self.log_printer) self.assertNotIn('WARNING', stdout.getvalue())
def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = ['[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
def test_write(self): result_file = ['[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
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 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 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()
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()
def get_bears(): """ Get a dict of bears with the bear class as key. :return: A dict with bear classes as key and the list of sections as value. """ log_printer = LogPrinter(NullPrinter()) sections, _ = load_configuration(None, log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) return inverse_dicts(local_bears, global_bears)
def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = [ '[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
def test_write(self): result_file = [ '[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
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 mode_statan(args, debug=False): import json import sys from coalib.coala_main import run_coala from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder from coalib.results.AnalyzerResult import AnalyzerResult from coalib.settings.ConfigurationGathering import load_configuration from coalib.output.database import StoreException, AnalyzerResultStore JSONEncoder = create_json_encoder(use_relpath=args.relpath) results, exitcode, _ = run_coala(args=args, debug=debug) arg_parser=None arg_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) lan = sections['cli'].get('language', '') lan_v = sections['cli'].get('language_version', '') prj = sections['cli'].get('project', '') prj_v = sections['cli'].get('project_version', '') params = sections['cli'].get('params', '') messages = results['cli'] analyzer_results = [] for message in messages: dif = json.dumps(message.diffs, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) ''' analyzer_results.append(AnalyzerResult(message.origin, lan.value, lan_v.value, prj.value, prj_v.value, message.affected_code[0].file, message.message, message.severity, dif, message.confidence)) ''' try: with AnalyzerResultStore() as result_store: result_store.addResult(AnalyzerResult(message.origin, lan.value, lan_v.value, prj.value, prj_v.value, message.affected_code[0].file, message.message, params.value, message.affected_code[0].start.line, message.severity, dif, message.confidence)) result_store.complete() except StoreException as e: print(e) ''' retval = {'results': results} 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