import pytest from calculate.templates.format.patch_format import PatchFormat from calculate.utils.files import Process from os import path import os @pytest.mark.patch class TestExecuteMethods: def test_if_diff_patch_used_for_patching_of_several_files__it_changes_patched_file_correctly(self): test_result = True cwd_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/') with open(path.join(cwd_path, 'diff_1.patch')) as patch_file: patch_text = patch_file.read() diff_patch = PatchFormat(patch_text) print('Path:', cwd_path) output = diff_patch.execute_format(target_path=cwd_path) print('Output:') print(output) if output: print('Changed files:') for changed_file, change_type in\ diff_patch.changed_files.items(): if changed_file.startswith(cwd_path): changed_file = changed_file[len(cwd_path):] # print(changed_file, ':', sep='') with open(path.join(diff_patch._cwd_path, changed_file)) as patched_file: patched_file_text = patched_file.read() # print(patched_file_text) other_file_name = 'b' + changed_file[1:] with open(path.join(diff_patch._cwd_path, other_file_name)) as other_file: other_file_text = other_file.read() test_result = test_result and (other_file_text == patched_file_text) return_patch_run = Process('patch', '-R', '-p{}'.format(diff_patch._last_level), cwd=cwd_path) return_patch_run.write(patch_text) output = return_patch_run.read() if return_patch_run.success(): print('[*] Changes was returned...') else: print('[!] Changes was not returned...') else: test_result = False assert test_result def test_if_diff_patch_used_for_patching_of_directories__it_changes_files_in_directories_and_adds_ones(self): test_result = True cwd_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/a1') patch_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/diff_2.patch') with open(path.join(patch_path)) as patch_file: patch_text = patch_file.read() diff_patch = PatchFormat(patch_text) diff_patch.execute_format(target_path=cwd_path) for changed_file, change_type in\ diff_patch.changed_files.items(): if changed_file.startswith(cwd_path): changed_file = changed_file[len(cwd_path) + 1:] print(changed_file, '', sep='') file_path = path.join(diff_patch._cwd_path, changed_file) with open(file_path) as patched_file: patched_file_text = patched_file.read() # print(patched_file_text) other_file_path = os.path.join(path.dirname(cwd_path), 'b1', changed_file) with open(other_file_path) as other_file: other_file_text = other_file.read() print('test_result =', test_result) test_result = test_result and ( other_file_text == patched_file_text) print('test_result =', test_result) if not test_result: print('Differences:') try: diff_process = Process('diff', '-u', file_path, other_file_path) diff_result = diff_process.read() print(diff_result) except Exception as error: print('diff was not executed.') print('Reason:', str(error)) reverse_patch_run = Process('patch', '-R', '-p{}'.format(diff_patch._last_level), cwd=cwd_path) reverse_patch_run.write(patch_text) reverse_patch_run.read() if reverse_patch_run.success(): print('[*] Changes was returned...') else: print('[!] Changes was not returned...') assert test_result