def validate_result(input_path, output_path, **kwargs): assert not is_empty(input_path) assert not is_empty(output_path) with open(output_path, 'rt') as f: out_requirements = [line.rstrip() for line in f.readlines()] sorted_requirements = sorted(out_requirements, key=lambda line: line.split('==')[0].casefold()) pytest.assume(sorted_requirements==out_requirements) new_lines = kwargs.get('new_lines', []) for line in new_lines: pytest.assume(line in sorted_requirements) removed_lines = kwargs.get('removed_lines', []) for line in removed_lines: pytest.assume(line not in sorted_requirements) with open(input_path, 'rt') as f: in_requirements = [line.rstrip() for line in f.readlines()] for line in in_requirements: if is_empty(line): continue if line in new_lines: continue if _extract_pck(line) in removed_lines: continue pytest.assume(line in sorted_requirements)
def _validate_mutual_exclusion(add_pckgs, rm_pckgs): if is_empty(add_pckgs) or is_empty(rm_pckgs): return s_add = {_extract_pck(element) for element in add_pckgs} for pck in rm_pckgs: if pck.casefold() in s_add: raise ValueError( f"Mutual_Exclusion enabled, but {pck} was found in both lists")
def _bool_is_empty(self, value): try: ret = parse_boolean(value) return ret is None except ValueError: ret = is_empty(value) return ret
def _parse_white_list_implicitely(self, default_d): subvalue = default_d.get(conf.GENERAL_KEY, {}) value = subvalue.get(conf.WHITE_LIST_KEY, {}) ret = is_empty(value) if ret is None: ret = True return ret
def _do_ensure_list(self, flat_d, list_ensure): b = is_empty(list) if b: return for flat_key in list_ensure: flat_value = self._ensure_list(flat_d, flat_key) flat_d[flat_key] = flat_value
def _parse_profiles(self, sys_d, default_d): profiles = self.merge_list_value_in_dicts(sys_d, default_d, conf.GENERAL_KEY, conf.PROFILES_KEY) b = is_empty(profiles) if b: profiles = [] return profiles
def _extract_pck(element): if element is None or is_empty(element): return '' elif '==' not in element: pck = element else: pck, _ = element.split('==') return pck
def _is_white_listed(self, white_list_flat_keys, flat_key): b = is_empty(white_list_flat_keys) if b: return True for white_list_flat_key in white_list_flat_keys: if flat_key.startswith(white_list_flat_key): return True return False
def _ensure_list(v): value = str(v) elements = value.split(",") # empty string will become null elements = [ None if is_empty(value) else value for value in elements ] ret = [self.mask_value(value) for value in elements] return ret
def _log_config(**kwargs): log_d = kwargs.get(conf.GENERAL_KEY, {}).get(conf.LOG_KEY, {}) if is_empty(log_d): log_d = _config["log_config"] logging.config.dictConfig(log_d) global logger logger = logging.getLogger(__name__) # logger.debug(f"{pprint.pprint(dd)}") #sort_dicts=False is available in Python 3.8 logger.debug(f"Parsed log configurations is {ymlparsers.as_str(kwargs)}")
def _get_white_listed(self, d, white_list_flat_keys): b = is_empty(d) if b: return d dd = OrderedDict() for flat_key, value in d.items(): if not ('.' in flat_key and self._is_white_listed(white_list_flat_keys, flat_key)): logger.info( f"Skipping key {flat_key}. It doesn't white listed.") continue dd[flat_key] = value return dd
def _parse_list_ensure(self, sys_d, default_d): list_ensure = self.merge_list_value_in_dicts(sys_d, default_d, conf.GENERAL_KEY, conf.LIST_ENSURE_KEY) b = is_empty(list_ensure) if not b: # we already proceesed profiles, white_list # we should process list_ensure profiles_flat_key = '.'.join([conf.GENERAL_KEY, conf.PROFILES_KEY]) list_ensure_flat_key = '.'.join( [conf.GENERAL_KEY, conf.LIST_ENSURE_KEY]) white_list_flat_key = '.'.join( [conf.GENERAL_KEY, conf.WHITE_LIST_KEY]) list_ensure = [flat_key for flat_key in list_ensure \ if flat_key not in {profiles_flat_key, list_ensure_flat_key, white_list_flat_key}] else: list_ensure = [] return list_ensure
def test_exception_in_code(self, request, mocker, ymlparsersSetup, ymlparsersCleanup, exp_config_d): logger.info(f'{request._pyfuncitem.name}()') #I check 2 things #1. Explicately passing params #2. Releasing lock even if exception occures in the with mock_lock = create_mock_lock(mocker) orig_jinja2ctx = HiYaPyCo.jinja2ctx mocker.patch.object(HiYaPyCo, 'jinja2Lock', new=mock_lock, spec_set=True) jinja2ctx_mock = mocker.patch.object(HiYaPyCo, 'jinja2ctx', spec_set=True) mock_variable_start_string = mocker.PropertyMock( return_value=orig_jinja2ctx.variable_start_string) type(jinja2ctx_mock).variable_start_string = mock_variable_start_string mock_variable_end_string = mocker.PropertyMock( return_value=orig_jinja2ctx.variable_end_string) type(jinja2ctx_mock).variable_end_string = mock_variable_end_string mock_block_start_string = mocker.PropertyMock( return_value=orig_jinja2ctx.block_start_string) type(jinja2ctx_mock).block_start_string = mock_block_start_string mock_block_end_string = mocker.PropertyMock( return_value=orig_jinja2ctx.block_end_string) type(jinja2ctx_mock).block_end_string = mock_block_end_string mocks = [ mock_variable_start_string, mock_variable_end_string, mock_block_start_string, mock_block_end_string ] # orig_exit = ymlparsers.DisableVarSubst.__exit__ # mock_exit = mocker.patch.object(ymlparsers.DisableVarSubst, '__exit__', side_effect=orig_exit, # autospec=True, spec_set=True) with pytest.raises(ValueError): with ymlparsers.DisableVarSubst(): raise ValueError # pytest.assume(mock_exit.call_count == 1) pytest.assume(mock_lock.acquire.call_count > 0) pytest.assume( mock_lock.release.call_count == mock_lock.acquire.call_count) setter_called = None for mock in mocks: setter_called = None pytest.assume(mock.call_count > 0) for kall in mock.call_args_list: # (param,), _ = kall args, _ = kall if not is_empty(args) and '|' in args[0]: setter_called = True break pytest.assume(setter_called) pytest.assume(jinja2ctx_mock.variable_start_string == orig_jinja2ctx.variable_start_string) pytest.assume(jinja2ctx_mock.variable_end_string == orig_jinja2ctx.variable_end_string) pytest.assume(jinja2ctx_mock.block_start_string == orig_jinja2ctx.block_start_string) pytest.assume( jinja2ctx_mock.block_end_string == orig_jinja2ctx.block_end_string)
def test_intented_usage(self, request, mocker, ymlparsersSetup, ymlparsersCleanup, exp_config_d): logger.info(f'{request._pyfuncitem.name}()') mock_lock = create_mock_lock(mocker) orig_jinja2ctx = HiYaPyCo.jinja2ctx mocker.patch.object(HiYaPyCo, 'jinja2Lock', new=mock_lock, spec_set=True) jinja2ctx_mock = mocker.patch.object(HiYaPyCo, 'jinja2ctx', spec_set=True) mock_variable_start_string = mocker.PropertyMock( return_value=orig_jinja2ctx.variable_start_string) type(jinja2ctx_mock).variable_start_string = mock_variable_start_string mock_variable_end_string = mocker.PropertyMock( return_value=orig_jinja2ctx.variable_end_string) type(jinja2ctx_mock).variable_end_string = mock_variable_end_string mock_block_start_string = mocker.PropertyMock( return_value=orig_jinja2ctx.block_start_string) type(jinja2ctx_mock).block_start_string = mock_block_start_string mock_block_end_string = mocker.PropertyMock( return_value=orig_jinja2ctx.block_end_string) type(jinja2ctx_mock).block_end_string = mock_block_end_string mocks = [ mock_variable_start_string, mock_variable_end_string, mock_block_start_string, mock_block_end_string ] # orig_exit = ymlparsers.DisableVarSubst.__exit__ # mock_exit = mocker.patch.object(ymlparsers.DisableVarSubst, '__exit__', side_effect=orig_exit, # autospec=True, spec_set=True) with ymlparsers.DisableVarSubst(): logger.debug("dff") pass # pytest.assume(mock_exit.call_count == 1) pytest.assume(mock_lock.acquire.call_count > 0) pytest.assume( mock_lock.release.call_count == mock_lock.acquire.call_count) setter_called = None for mock in mocks: setter_called = None pytest.assume(mock.call_count > 0) for kall in mock.call_args_list: #(param,), _ = kall args, _ = kall if not is_empty(args) and '|' in args[0]: setter_called = True break pytest.assume(setter_called) pytest.assume(jinja2ctx_mock.variable_start_string == orig_jinja2ctx.variable_start_string) pytest.assume(jinja2ctx_mock.variable_end_string == orig_jinja2ctx.variable_end_string) pytest.assume(jinja2ctx_mock.block_start_string == orig_jinja2ctx.block_start_string) pytest.assume( jinja2ctx_mock.block_end_string == orig_jinja2ctx.block_end_string)
def test_is_empty(request, value, exp_result): logger.info(f'{request._pyfuncitem.name}()') result = is_empty(value) assert exp_result == result
def _process_line(prev_line, cur_line, **kwargs): cur_pck, _ = ('', None) if cur_line is None else cur_line.split('==') prev_pck, _ = ('', None) if prev_line is None else prev_line.split('==') #lowercase them low_prev_line = None if prev_line is None else prev_line.casefold() low_cur_line = None if cur_line is None else cur_line.casefold() if (low_prev_line is not None) and (low_prev_line == low_cur_line) and not is_empty(low_cur_line): return None #duplicate line, ignore low_prev_pck = prev_pck.casefold() low_cur_pck = cur_pck.casefold() if (low_prev_line is not None) and ( low_prev_pck > low_cur_pck) and not is_empty(low_cur_pck): raise ValueError( "Source file expected to be sorted. Use sort utilities, for example." ) if (is_empty(low_prev_pck)) and ( low_prev_pck == low_cur_pck) and not is_empty(low_cur_pck): raise ValueError( f"Packages in the source file should be unique, but duplicate package {cur_pck} is found." ) add_pckgs = kwargs.get(conf.ADD_KEY, None) rm_pckgs = kwargs.get(conf.RM_KEY, None) ret = deque([]) is_append_curr_line = True #remove packages first while rm_pckgs is not None and not is_empty(rm_pckgs) and not is_empty( low_cur_pck): rm_pck = rm_pckgs[0] low_rm_pck = rm_pck.casefold() if low_cur_pck < low_rm_pck: is_append_curr_line = True break elif low_cur_pck == low_rm_pck: rm_pckgs.popleft() is_append_curr_line = False break else: rm_pckgs.popleft() continue # add packages while add_pckgs is not None and not is_empty(add_pckgs): add_line = add_pckgs[0] add_pck = _extract_pck(add_line) low_add_pck = add_pck.casefold() if is_empty(low_prev_pck) and ( low_prev_pck < low_add_pck <= low_cur_pck): #adding new package at the head of file ret.append(add_line) is_append_curr_line = True add_pckgs.popleft() elif low_prev_pck < low_add_pck <= low_cur_pck: ret.append(add_line) is_append_curr_line = False add_pckgs.popleft() else: break if is_append_curr_line and not is_empty(cur_line): ret.append(cur_line) return ret