def test_good_format(self): self.section.append(Setting('format', '{origin}')) with retrieve_stdout() as stdout: print_results_formatted(self.logger, self.section, [Result('1', '2')], None, None) self.assertEqual(stdout.getvalue(), '1\n')
def test_str_list(self): self.uut = Setting('key', 'a, b, c') self.assertEqual(str_list(self.uut), ['a', 'b', 'c']) self.assertEqual(repr(str_list), 'typed_list(str)')
def test_good_file(self): self.section.append(Setting('language', 'Python')) self.check_validity(self.uut, good_file.splitlines())
def test_copyright_with_given_author(self): file_contents = load_testfile('copyright_with_given_author.txt') self.section.append(Setting('author_name', 'The coala developers')) self.check_validity(self.uut, file_contents)
def setUp(self): self.section = Section('aspect section') self.section.append(Setting('aspects', 'commitmessage')) self.section.language = Language['python'] self.sections = {'aspect section': self.section}
def test_process_queues(self): ctrlq = queue.Queue() # Append custom controlling sequences. # Simulated process 1 ctrlq.put((CONTROL_ELEMENT.LOCAL, 1)) ctrlq.put((CONTROL_ELEMENT.LOCAL_FINISHED, None)) ctrlq.put((CONTROL_ELEMENT.GLOBAL, 1)) # Simulated process 2 ctrlq.put((CONTROL_ELEMENT.LOCAL, 2)) # Simulated process 1 ctrlq.put((CONTROL_ELEMENT.GLOBAL_FINISHED, None)) # Simulated process 2 ctrlq.put((CONTROL_ELEMENT.LOCAL_FINISHED, None)) ctrlq.put((CONTROL_ELEMENT.GLOBAL, 1)) ctrlq.put((CONTROL_ELEMENT.GLOBAL_FINISHED, None)) first_local = Result.from_values('o', 'The first result.', file='f') second_local = Result.from_values('ABear', 'The second result.', file='f', line=1) third_local = Result.from_values('ABear', 'The second result.', file='f', line=4) fourth_local = Result.from_values('ABear', 'Another result.', file='f', line=7) first_global = Result('o', 'The one and only global result.') section = Section('') section.append(Setting('min_severity', 'normal')) process_queues( [DummyProcess(control_queue=ctrlq) for i in range(3)], ctrlq, { 1: [ first_local, second_local, third_local, # The following are to be ignored Result('o', 'm', severity=RESULT_SEVERITY.INFO), Result.from_values('ABear', 'u', 'f', 2, 1), Result.from_values('ABear', 'u', 'f', 3, 1) ], 2: [ fourth_local, # The following are to be ignored HiddenResult('t', 'c'), Result.from_values('ABear', 'u', 'f', 5, 1), Result.from_values('ABear', 'u', 'f', 6, 1) ] }, {1: [first_global]}, {'f': self.file_dict[self.factory_test_file]}, lambda *args: self.queue.put(args[2]), section, None, self.log_printer, self.console_printer) self.assertEqual(self.queue.get(timeout=0), ([second_local, third_local])) self.assertEqual(self.queue.get(timeout=0), ([fourth_local])) self.assertEqual(self.queue.get(timeout=0), ([first_global])) self.assertEqual(self.queue.get(timeout=0), ([first_global]))
def test_keyword_case(self): self.section.append(Setting('keyword_case', 'lower')) self.check_invalidity(self.uut, ['SELECT *\n', 'FROM tablename;\n', '\n']) self.check_validity(self.uut, ['select *\n', 'from tablename;\n', '\n'])
def setUp(self): self.section = Section('name') self.uut = bear(self.section, queue.Queue()) for name, value in settings.items(): self.section.append(Setting(name, value))
def test_glob(self): self.uut = Setting('key', '.', origin=os.path.join('test (1)', 'somefile')) self.assertEqual(glob(self.uut), glob_escape(os.path.abspath('test (1)')))
def load_configuration(arg_list, log_printer, arg_parser=None): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of command line arguments. :param log_printer: The LogPrinter object for logging. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser) check_conflicts(cli_sections) if (bool(cli_sections["default"].get("find_config", "False")) and str(cli_sections["default"].get("config")) == ""): cli_sections["default"].add_or_create_setting( Setting("config", re.escape(find_user_config(os.getcwd())))) targets = [] # We don't want to store targets argument back to file, thus remove it for item in list(cli_sections["default"].contents.pop("targets", "")): targets.append(item.lower()) if bool(cli_sections["default"].get("no_config", "False")): sections = cli_sections else: default_sections = load_config_file(Constants.system_coafile, log_printer) user_sections = load_config_file(Constants.user_coafile, log_printer, silent=True) default_config = str(default_sections["default"].get( "config", ".coafile")) user_config = str(user_sections["default"].get("config", default_config)) config = os.path.abspath( str(cli_sections["default"].get("config", user_config))) try: save = bool(cli_sections["default"].get("save", "False")) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, log_printer, silent=save) sections = merge_section_dicts(default_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) sections = merge_section_dicts(sections, cli_sections) for section in sections: if section != "default": sections[section].defaults = sections["default"] str_log_level = str(sections["default"].get("log_level", "")).upper() log_printer.log_level = LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO) warn_config_absent(sections, 'files', log_printer) warn_config_absent(sections, 'bears', log_printer) return sections, targets
def test_line_length(self): self.check_validity(self.uut, ['a = 1 + 1 + 1 + 1 + 1 + 1 + 1']) self.section.append(Setting('max_line_length', '10')) self.check_validity(self.uut, ['a = 1 + 1 + 1 + 1 + 1 + 1 + 1'], valid=False)
def load_configuration(arg_list, log_printer=None, arg_parser=None, args=None, silent=False): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of CLI arguments. :param log_printer: The LogPrinter object for logging. :param arg_parser: An ``argparse.ArgumentParser`` instance used for parsing the CLI arguments. :param args: Alternative pre-parsed CLI arguments. :param silent: Whether or not to display warnings, ignored if ``save`` is enabled. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser, args=args) check_conflicts(cli_sections) if (bool(cli_sections['cli'].get('find_config', 'False')) and str(cli_sections['cli'].get('config')) == ''): cli_sections['cli'].add_or_create_setting( Setting('config', PathArg(find_user_config(os.getcwd())))) # We don't want to store targets argument back to file, thus remove it targets = [ item.lower() for item in list(cli_sections['cli'].contents.pop('targets', '')) ] if bool(cli_sections['cli'].get('no_config', 'False')): sections = cli_sections else: base_sections = load_config_file(Constants.system_coafile, silent=silent) user_sections = load_config_file(Constants.user_coafile, silent=True) default_config = str(base_sections['default'].get( 'config', '.coafile')) user_config = str(user_sections['default'].get('config', default_config)) config = os.path.abspath( str(cli_sections['cli'].get('config', user_config))) try: save = bool(cli_sections['cli'].get('save', 'False')) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, silent=save or silent) sections = merge_section_dicts(base_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) if 'cli' in sections: logging.warning('\'cli\' is an internally reserved section name. ' 'It may have been generated into your coafile ' 'while running coala with `--save`. The settings ' 'in that section will inherit implicitly to all ' 'sections as defaults just like CLI args do. ' 'Please change the name of that section in your ' 'coafile to avoid any unexpected behavior.') sections = merge_section_dicts(sections, cli_sections) for name, section in list(sections.items()): section.set_default_section(sections) if name == 'default': if section.contents: logging.warning('Implicit \'Default\' section inheritance is ' 'deprecated. It will be removed soon. To ' 'silence this warning remove settings in the ' '\'Default\' section from your coafile. You ' 'can use dots to specify inheritance: the ' 'section \'all.python\' will inherit all ' 'settings from \'all\'.') sections['default'].update(sections['cli']) sections['default'].name = 'cli' sections['cli'] = sections['default'] del sections['default'] str_log_level = str(sections['cli'].get('log_level', '')).upper() logging.getLogger().setLevel( LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO)) return sections, targets
def test_empty_list(self): self.section.append(Setting('format', '{origin}')) # Shouldn't attempt to format the string None and will fail badly if # its done wrong. print_results_formatted(None, self.section, [], None, None, None)
def test_inherited_conversions(self): self.uut = Setting('key', ' 22\n', '.', strip_whitespaces=True) self.assertEqual(str(self.uut), '22') self.assertEqual(int(self.uut), 22) self.assertRaises(ValueError, bool, self.uut)
def test_identifier_case(self): self.section.append(Setting('identifier_case', 'upper')) self.check_invalidity(self.uut, ['SELECT *\n', 'FROM tablename;\n', '\n']) self.check_validity(self.uut, ['SELECT *\n', 'FROM TABLE;\n', '\n'])
def test_empty_key(self): with self.assertRaisesRegex( ValueError, 'An empty key is not allowed for a setting'): Setting('', 2)
def test_keyword_between_code(self): self.section.append(Setting('language', 'c')) self.section.append(Setting('keywords', 'todo')) text = ['int a=0; /* TODO: Test */ int b=1;\n'] comments = [SourceRange.from_values('F', 1, 10, 1, 25)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, filename='F', file=text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-int a=0; /* TODO: Test */ int b=1;\n' '+int a=0; int b=1;\n') text = ['int a = 0; /* TODO test\n', 'another test\n', '*/\n'] comments = [SourceRange.from_values('F', 1, 12, 3, 2)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, filename='F', file=text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-int a = 0; /* TODO test\n' '+int a = 0; /*\n' ' another test\n' ' */\n') text = ['/* TODO\n', 'test\n', '*/\n'] comments = [SourceRange.from_values('F', 1, 1, 3, 2)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, filename='F', file=text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-/* TODO\n' '+/*\n' ' test\n' ' */\n')
def test_create_params_from_section(self): section = Section("name") section.append(Setting("bad_param", "value")) uut = FunctionMetadata.from_function(TestClass(5, 5).bad_function) params = uut.create_params_from_section(section) self.assertIsInstance(params["bad_param"], Setting)
def setUp(self): self.section = Section('name') self.section.append(Setting('max_line_length', '80')) self.uut = PEP8Bear(self.section, Queue())
def setUp(self): self.section = Section('language section') self.section.append(Setting('language', 'python')) self.sections = {'language section': self.section} self.language = Language['python']
def test_line_length(self): self.assertLinesValid(self.uut, ["a = 1 + 1 + 1 + 1 + 1 + 1 + 1"]) self.section.append(Setting('max_line_length', '10')) self.assertLinesInvalid(self.uut, ["a = 1 + 1 + 1 + 1 + 1 + 1 + 1"])
def test_no_value(self): self.section.append(Setting('default_actions', '')) self.assertEqual(get_default_actions(self.section), ({}, {}))
def __parse_lines(self, lines, origin): current_section_name = 'default' current_section = self.get_section(current_section_name) current_keys = [] no_section = True for line in lines: (section_name, keys, value, append, comment) = self.line_parser._parse(line) if comment != '': self.__add_comment(current_section, comment, origin) if section_name != '': no_section = False current_section_name = section_name current_section = self.get_section(current_section_name, True) current_keys = [] continue if comment == '' and keys == [] and value == '': self.__add_comment(current_section, '', origin) continue if keys != []: current_keys = keys for section_override, key in current_keys: if no_section: logging.warning('A setting does not have a section.' 'This is a deprecated feature please ' 'put this setting in a section defined' ' with `[<your-section-name]` in a ' 'configuration file.') if key == '': continue if key in current_section.contents and keys != []: logging.warning('{} setting has already been defined in ' 'section {}. The previous setting will be ' 'overridden.'.format( key, current_section.name)) if section_override == '': current_section.add_or_create_setting( Setting( key, value, origin, to_append=append, # Start ignoring PEP8Bear, PycodestyleBear* # they fail to resolve this remove_empty_iter_elements=self. __remove_empty_iter_elements), # Stop ignoring allow_appending=(keys == [])) else: self.get_section( section_override, True ).add_or_create_setting( Setting( key, value, origin, to_append=append, # Start ignoring PEP8Bear, PycodestyleBear* # they fail to resolve this remove_empty_iter_elements=self. __remove_empty_iter_elements), # Stop ignoring allow_appending=(keys == []))
def setUp(self): self.section = Section('') self.section.append(Setting('language', 'TestLanguage')) self.section.append(Setting('use_spaces', False)) self.dep_uut = AnnotationBear(self.section, Queue())
def test_language(self): self.uut = Setting('key', 'python 3.4') result = language(self.uut) self.assertIsInstance(result, Language) self.assertEqual(str(result), 'Python 3.4')
def test_get_config_dir(self): section = Section('default') section.append(Setting('files', '**', '/path/to/dir/config')) uut = TestBear(section, None) self.assertEqual(uut.get_config_dir(), abspath('/path/to/dir'))
def test_bad_format(self): self.section.append(Setting('format', '{nonexistant}')) print_results_formatted(self.logger, self.section, [Result('1', '2')], None, None) self.assertRegex(''.join(log.message for log in self.logger.logs), '.*Unable to print.*')