def test_yield_ignore_ranges(self): test_file_dict_a = {'f': ('# Ignore aBear\n', 'a_string = "This string should be ignored"\n')} test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = {'f': ('# start Ignoring bBear\n', 'b_string = "This string should be ignored"\n', '# stop ignoring\n')} test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = {'f': ('# Start ignoring cBear\n', '# Stop ignoring cBear This & prev ignored\n')} test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42)
def test_yield_ignore_ranges(self): test_file_dict_a = {'f': ('# Ignore aBear\n', 'a_string = "This string should be ignored"\n')} test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = {'f': ('# start Ignoring bBear\n', 'b_string = "This string should be ignored"\n', '# stop ignoring\n')} test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = {'f': ('# Start ignoring cBear\n', '# Stop ignoring cBear This & prev ignored\n')} test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42) test_file_dict_d = {'f': ('# Start ignoring cBear\n', 'All of this ignored\n')} test_ignore_range_d = list(yield_ignore_ranges(test_file_dict_d)) for test_bears, test_source_range in test_ignore_range_d: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 20) # This case was a bug. test_file_dict_single_line = {'f': ('# ignore XBEAR',)} test_ignore_range_single_line = list(yield_ignore_ranges( test_file_dict_single_line)) self.assertEqual(len(test_ignore_range_single_line), 1) bears, source_range = test_ignore_range_single_line[0] self.assertEqual(bears, ['xbear']) self.assertEqual(source_range.start.line, 1) self.assertEqual(source_range.start.column, 1) self.assertEqual(source_range.end.line, 1) self.assertEqual(source_range.end.column, 14)
def test_yield_ignore_ranges(self): test_file_dict_a = {'f': ('# Ignore aBear\n', 'a_string = "This string should be ignored"\n')} test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = {'f': ('# start Ignoring bBear\n', 'b_string = "This string should be ignored"\n', '# stop ignoring\n')} test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = {'f': ('# Start ignoring cBear\n', '# Stop ignoring cBear This & prev ignored\n')} test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42) test_file_dict_d = {'f': ('# Start ignoring cBear\n', 'All of this ignored\n')} test_ignore_range_d = list(yield_ignore_ranges(test_file_dict_d)) for test_bears, test_source_range in test_ignore_range_d: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 20) # This case was a bug. test_file_dict_single_line = {'f': ('# ignore XBEAR',)} test_ignore_range_single_line = list(yield_ignore_ranges( test_file_dict_single_line)) self.assertEqual(len(test_ignore_range_single_line), 1) bears, source_range = test_ignore_range_single_line[0] self.assertEqual(bears, ['xbear']) self.assertEqual(source_range.start.line, 1) self.assertEqual(source_range.start.column, 1) self.assertEqual(source_range.end.line, 1) self.assertEqual(source_range.end.column, 14)
def test_yield_ignore_ranges(self): test_file_dict_a = { 'f': ('# Ignore aBear\n', 'a_string = "This string should be ignored"\n') } test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = { 'f': ('# start Ignoring bBear\n', 'b_string = "This string should be ignored"\n', '# stop ignoring\n') } test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = { 'f': ('# Start ignoring cBear\n', '# Stop ignoring cBear This & prev ignored\n') } test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42)
def run_quickstartbear(contents, project_dir): """ Runs the QuickstartBear which pareses the file_dict to get the exact value of some settings which can attain any infinite amount of values. :param contents: The python object written to 'PROJECT_DATA' which contains the directory structure and values of some settings which can attain an infinite set of values. :param project_dir: The project directory from which to get the files for the QuickstartBear to run. :return: - An updated contents value after guessing values of certain settings. - Collection of SourceRange objects indicating the parts of code to ignore. - The complete file dict contains file names as keys and file contents as values to those keys. - The complete file name list from the project directory and sub directories. """ section = Section('green_mode') quickstartbear_obj = QuickstartBear(section, None) complete_filename_list = generate_complete_filename_list( contents['dir_structure'], project_dir) complete_file_dict = get_file_dict(complete_filename_list, allow_raw_files=True) ignore_ranges = list(yield_ignore_ranges(complete_file_dict)) find_max = ['max_lines_per_file', 'max_line_length'] find_min = ['min_lines_per_file'] for key in complete_file_dict: return_val = quickstartbear_obj.execute( filename=key, file=complete_file_dict[key]) return_val = return_val[0] # eg. return_val = {'setting_name': value, ...} if return_val is not None: for setting in find_max: contents = find_max_min_of_setting( setting, return_val[setting], contents, operator.gt) for setting in find_min: contents = find_max_min_of_setting( setting, return_val[setting], contents, operator.lt) bear_settings = get_yaml_contents(str( Path(__file__).parent / 'bear_settings.yaml'))['type2'] full_dict = {} for small_dict in bear_settings.values(): full_dict.update(small_dict) resort_to_default_settings = deepcopy(find_max) resort_to_default_settings += find_min for setting_name in resort_to_default_settings: default_val = full_dict[setting_name] insert_index = -1 for index, dict_ in enumerate(contents[settings_key]): if setting_name in dict_: current_val = dict_[setting_name] insert_index = index break if 'current_val' in locals() and current_val < default_val: contents[settings_key][insert_index][setting_name] = default_val return (contents, ignore_ranges, complete_file_dict, complete_filename_list)
def test_yield_ignore_ranges(self): test_file_dict_a = {'f': self.file_dict[self.a_bear_test_path]} test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = {'f': self.file_dict[self.b_bear_test_path]} test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = {'f': self.file_dict[self.c_bear_test_path]} test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42) test_file_dict_d = {'f': self.file_dict[self.d_bear_test_path]} test_ignore_range_d = list(yield_ignore_ranges(test_file_dict_d)) for test_bears, test_source_range in test_ignore_range_d: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 20) test_file_dict_e = {'f': self.file_dict[self.e_bear_test_path]} test_ignore_range_e = list(yield_ignore_ranges(test_file_dict_e)) for test_bears, test_source_range in test_ignore_range_e: self.assertEqual(test_bears, []) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_n = {'f': self.file_dict[self.n_bear_test_path]} test_ignore_range_n = list(yield_ignore_ranges(test_file_dict_n)) for test_bears, test_source_range in test_ignore_range_n: self.assertEqual(test_bears, ['nbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_n = {'f': self.file_dict[self.n_bear_test_path_2]} test_ignore_range_n = list(yield_ignore_ranges(test_file_dict_n)) for test_bears, test_source_range in test_ignore_range_n: self.assertEqual(test_bears, []) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) # This case was a bug. test_file_dict_single_line = ({ 'f': self.file_dict[self.x_bear_test_path] }) test_ignore_range_single_line = list( yield_ignore_ranges(test_file_dict_single_line)) self.assertEqual(len(test_ignore_range_single_line), 1) bears, source_range = test_ignore_range_single_line[0] self.assertEqual(bears, ['xbear']) self.assertEqual(source_range.start.line, 1) self.assertEqual(source_range.start.column, 1) self.assertEqual(source_range.end.line, 1) self.assertEqual(source_range.end.column, 15)
def run_quickstartbear(contents, project_dir): """ Runs the QuickstartBear which pareses the file_dict to get the exact value of some settings which can attain any infinite amount of values. :param contents: The python object written to 'PROJECT_DATA' which contains the directory structure and values of some settings which can attain an infinite set of values. :param project_dir: The project directory from which to get the files for the QuickstartBear to run. :return: - An updated contents value after guessing values of certain settings. - Collection of SourceRange objects indicating the parts of code to ignore. - The complete file dict contains file names as keys and file contents as values to those keys. - The complete file name list from the project directory and sub directories. """ section = Section('green_mode') quickstartbear_obj = QuickstartBear(section, None) complete_filename_list = generate_complete_filename_list( contents['dir_structure'], project_dir) complete_file_dict = get_file_dict(complete_filename_list, allow_raw_files=True) ignore_ranges = list(yield_ignore_ranges(complete_file_dict)) find_max = ['max_lines_per_file', 'max_line_length'] find_min = ['min_lines_per_file'] for key in complete_file_dict: return_val = quickstartbear_obj.execute( filename=key, file=complete_file_dict[key]) return_val = return_val[0] # eg. return_val = {'setting_name': value, ...} if return_val is not None: for setting in find_max: contents = find_max_min_of_setting( setting, return_val[setting], contents, operator.gt) for setting in find_min: contents = find_max_min_of_setting( setting, return_val[setting], contents, operator.lt) bear_settings = get_yaml_contents(str( Path(__file__).parent / 'bear_settings.yaml'))['type2'] full_dict = {} for small_dict in bear_settings.values(): full_dict.update(small_dict) resort_to_default_settings = deepcopy(find_max) resort_to_default_settings += find_min for setting_name in resort_to_default_settings: default_val = full_dict[setting_name] insert_index = -1 for index, dict_ in enumerate(contents[settings_key]): if setting_name in dict_: current_val = dict_[setting_name] insert_index = index break if 'current_val' in locals() and current_val < default_val: contents[settings_key][insert_index][setting_name] = default_val return (contents, ignore_ranges, complete_file_dict, complete_filename_list)
def test_yield_ignore_ranges(self): test_file_dict_a = {'f': self.file_dict[self.a_bear_test_path]} test_ignore_range_a = list(yield_ignore_ranges(test_file_dict_a)) for test_bears, test_source_range in test_ignore_range_a: self.assertEqual(test_bears, ['abear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_b = {'f': self.file_dict[self.b_bear_test_path]} test_ignore_range_b = list(yield_ignore_ranges(test_file_dict_b)) for test_bears, test_source_range in test_ignore_range_b: self.assertEqual(test_bears, ['bbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 3) self.assertEqual(test_source_range.end.column, 16) test_file_dict_c = {'f': self.file_dict[self.c_bear_test_path]} test_ignore_range_c = list(yield_ignore_ranges(test_file_dict_c)) for test_bears, test_source_range in test_ignore_range_c: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 42) test_file_dict_d = {'f': self.file_dict[self.d_bear_test_path]} test_ignore_range_d = list(yield_ignore_ranges(test_file_dict_d)) for test_bears, test_source_range in test_ignore_range_d: self.assertEqual(test_bears, ['cbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 20) test_file_dict_e = {'f': self.file_dict[self.e_bear_test_path]} test_ignore_range_e = list(yield_ignore_ranges(test_file_dict_e)) for test_bears, test_source_range in test_ignore_range_e: self.assertEqual(test_bears, []) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_n = {'f': self.file_dict[self.n_bear_test_path]} test_ignore_range_n = list(yield_ignore_ranges(test_file_dict_n)) for test_bears, test_source_range in test_ignore_range_n: self.assertEqual(test_bears, ['nbear']) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) test_file_dict_n = {'f': self.file_dict[self.n_bear_test_path_2]} test_ignore_range_n = list(yield_ignore_ranges(test_file_dict_n)) for test_bears, test_source_range in test_ignore_range_n: self.assertEqual(test_bears, []) self.assertEqual(test_source_range.start.line, 1) self.assertEqual(test_source_range.start.column, 1) self.assertEqual(test_source_range.end.line, 2) self.assertEqual(test_source_range.end.column, 43) # This case was a bug. test_file_dict_single_line = ( {'f': self.file_dict[self.x_bear_test_path]}) test_ignore_range_single_line = list(yield_ignore_ranges( test_file_dict_single_line)) self.assertEqual(len(test_ignore_range_single_line), 1) bears, source_range = test_ignore_range_single_line[0] self.assertEqual(bears, ['xbear']) self.assertEqual(source_range.start.line, 1) self.assertEqual(source_range.start.column, 1) self.assertEqual(source_range.end.line, 1) self.assertEqual(source_range.end.column, 15)