import pytest from calculate.templates.format.diff_format import DiffFormat from calculate.utils.files import Process from os import path import os @pytest.mark.diff class TestExecuteMethods: def test_if_diff_patch_used_for_patching_of_several_files__it_changes_patched_file_correctly(self): test_result = True root_path = path.join(os.getcwd(), 'tests/format/testfiles/') with open(path.join(root_path, 'diff_1.patch')) as patch_file: patch_text = patch_file.read() diff_patch = DiffFormat(patch_text) print('Path:', root_path) output = diff_patch.execute_format(root_path=root_path) if output: print('Changed files:') for changed_file in diff_patch._changed_files_list: print(changed_file, ':', sep='') with open(path.join(diff_patch._root_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._root_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=root_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 root_path = path.join(os.getcwd(), 'tests/format/testfiles/a1') patch_path = path.join(os.getcwd(), 'tests/format/testfiles/diff_2.patch') with open(path.join(patch_path)) as patch_file: patch_text = patch_file.read() diff_patch = DiffFormat(patch_text) print('Path:', root_path) output = diff_patch.execute_format(root_path=root_path) print('Output:') print(output) if output: print('Changed files:') for changed_file in diff_patch._changed_files_list: print(changed_file, ':', sep='') file_path = path.join(diff_patch._root_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(root_path), 'b1', changed_file) with open(other_file_path) as other_file: other_file_text = other_file.read() test_result = test_result and (other_file_text == patched_file_text) 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=root_path) reverse_patch_run.write(patch_text) output = reverse_patch_run.read() if reverse_patch_run.success(): print('[*] Changes was returned...') else: print('[!] Changes was not returned...') else: test_result = False assert test_result