diff --git a/calculate/templates/format/patch_format.py b/calculate/templates/format/patch_format.py index de70843..be62063 100644 --- a/calculate/templates/format/patch_format.py +++ b/calculate/templates/format/patch_format.py @@ -58,6 +58,7 @@ class PatchFormat(BaseFormat): if patch_dry_run.success(): return '' else: + print('Exception') raise FormatError('correction failed') self._last_level = level @@ -66,6 +67,7 @@ class PatchFormat(BaseFormat): patch_run.write(self._patch_text) if patch_run.success(): + print('SUCCESS') for line in patch_run.read_lines(): if line.startswith('patching file'): changed_file_path = path.join(self._cwd_path, @@ -76,9 +78,6 @@ class PatchFormat(BaseFormat): else: self.changed_files.update({changed_file_path: 'remove'}) - print('Changings: ') - for file_path, change_type in self.changed_files.items(): - print('{}: {}'.format(file_path, change_type)) return patch_run.read() else: return '' diff --git a/tests/templates/format/test_patch.py b/tests/templates/format/test_patch.py index 7a21a15..f84a104 100644 --- a/tests/templates/format/test_patch.py +++ b/tests/templates/format/test_patch.py @@ -1,102 +1,82 @@ import pytest +import shutil from calculate.templates.format.patch_format import PatchFormat from calculate.utils.files import Process from os import path import os +import re + + +BACKUP_PATH = os.path.join( + os.getcwd(), + 'tests/templates/format/testfiles/patch_testfiles.backup') + +TESTFILES_PATH = os.path.join( + os.getcwd(), + 'tests/templates/format/testfiles/patch_testfiles') @pytest.mark.patch class TestExecuteMethods: + def test_create_testfiles(self): + shutil.copytree(BACKUP_PATH, TESTFILES_PATH) + def test_if_diff_patch_used_for_patching_of_several_files__it_changes_patched_file_correctly(self): + if not os.path.exists(TESTFILES_PATH): + assert False + + path_pattern = re.compile(r'/a1/') test_result = True - cwd_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/') + cwd_path = path.join( + os.getcwd(), + 'tests/templates/format/testfiles/patch_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: + for changed_file, change_type in diff_patch.changed_files.items(): + with open(changed_file, 'r') 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...') + other_file_path = re.sub(path_pattern, '/b/', changed_file) + with open(other_file_path, 'r') as other_file: + other_file_text = other_file.read() + test_result = (test_result and (other_file_text + == patched_file_text)) 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): + if not os.path.exists(TESTFILES_PATH): + assert False + + path_pattern = re.compile(r'/a1/') 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') + cwd_path = path.join( + os.getcwd(), + 'tests/templates/format/testfiles/patch_testfiles/a1') + patch_path = path.join( + os.getcwd(), + 'tests/templates/format/testfiles/patch_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: + for changed_file, change_type in diff_patch.changed_files.items(): + with open(changed_file, 'r') 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) + + other_file_path = re.sub(path_pattern, '/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...') + test_result = (test_result and (other_file_text + == patched_file_text)) assert test_result + + def test_remove_testfiles(self): + shutil.rmtree(TESTFILES_PATH) diff --git a/tests/templates/format/testfiles/b1/dir/file1.txt b/tests/templates/format/testfiles/b1/dir/file1.txt deleted file mode 100644 index 474f6f7..0000000 --- a/tests/templates/format/testfiles/b1/dir/file1.txt +++ /dev/null @@ -1,17 +0,0 @@ -CONFIG_IRQ_WORK=y -CONFIG_BUILDTIME_EXTABLE_SORT=y -CONFIG_THREAD_INFO_IN_TASK=y -# -# General setup -# -CONFIG_INIT_ENV_ARG_LIMIT=16 -# CONFIG_COMPILE_TEST is not set -CONFIG_LOCALVERSION="" -# CONFIG_LOCALVERSION_AUTO is not set -CONFIG_BUILD_SALT="" -CONFIG_HAVE_KERNEL_GZIP=y -CONFIG_HAVE_KERNEL_BZIP2=y -CONFIG_HAVE_KERNEL_LZMA=y -CONFIG_HAVE_KERNEL_XZ=y -CONFIG_HAVE_KERNEL_LZO=y -CONFIG_HAVE_KERNEL_LZ4=y diff --git a/tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file1.txt b/tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file1.txt new file mode 100644 index 0000000..023bd50 --- /dev/null +++ b/tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file1.txt @@ -0,0 +1,10 @@ +CONFIG_IRQ_WORK=y +CONFIG_BUILDTIME_EXTABLE_SORT=y +# +# General setup +# +CONFIG_HAVE_KERNEL_GZIP=y +CONFIG_HAVE_KERNEL_BZIP2=y +CONFIG_HAVE_KERNEL_XZ=y +CONFIG_HAVE_KERNEL_LZO=y +CONFIG_HAVE_KERNEL_LZ4=y diff --git a/tests/templates/format/testfiles/b1/dir/file2.txt b/tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file2.txt similarity index 73% rename from tests/templates/format/testfiles/b1/dir/file2.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file2.txt index acfa977..be7bbc3 100644 --- a/tests/templates/format/testfiles/b1/dir/file2.txt +++ b/tests/templates/format/testfiles/patch_testfiles.backup/a/dir/file2.txt @@ -8,9 +8,3 @@ A needle tears a hole The old familiar sting Try to kill it all away But I remember everything. - -What have I become -My sweetest friend -Everyone I know -Goes away -In the end diff --git a/tests/templates/format/testfiles/a1/dir/file1.txt b/tests/templates/format/testfiles/patch_testfiles.backup/a1/dir/file1.txt similarity index 100% rename from tests/templates/format/testfiles/a1/dir/file1.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/a1/dir/file1.txt diff --git a/tests/templates/format/testfiles/a/dir/file1.txt b/tests/templates/format/testfiles/patch_testfiles.backup/b/dir/file1.txt similarity index 100% rename from tests/templates/format/testfiles/a/dir/file1.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/b/dir/file1.txt diff --git a/tests/templates/format/testfiles/a/dir/file2.txt b/tests/templates/format/testfiles/patch_testfiles.backup/b/dir/file2.txt similarity index 100% rename from tests/templates/format/testfiles/a/dir/file2.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/b/dir/file2.txt diff --git a/tests/templates/format/testfiles/b/dir/file1.txt b/tests/templates/format/testfiles/patch_testfiles.backup/b1/dir/file1.txt similarity index 100% rename from tests/templates/format/testfiles/b/dir/file1.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/b1/dir/file1.txt diff --git a/tests/templates/format/testfiles/b/dir/file2.txt b/tests/templates/format/testfiles/patch_testfiles.backup/b1/dir/file2.txt similarity index 100% rename from tests/templates/format/testfiles/b/dir/file2.txt rename to tests/templates/format/testfiles/patch_testfiles.backup/b1/dir/file2.txt diff --git a/tests/templates/format/testfiles/diff_1.patch b/tests/templates/format/testfiles/patch_testfiles.backup/diff_1.patch similarity index 100% rename from tests/templates/format/testfiles/diff_1.patch rename to tests/templates/format/testfiles/patch_testfiles.backup/diff_1.patch diff --git a/tests/templates/format/testfiles/diff_2.patch b/tests/templates/format/testfiles/patch_testfiles.backup/diff_2.patch similarity index 100% rename from tests/templates/format/testfiles/diff_2.patch rename to tests/templates/format/testfiles/patch_testfiles.backup/diff_2.patch diff --git a/tests/templates/testfiles/test_config_root_2/var/lib/calculate/config b/tests/templates/testfiles/test_config_root_2/var/lib/calculate/config/file similarity index 100% rename from tests/templates/testfiles/test_config_root_2/var/lib/calculate/config rename to tests/templates/testfiles/test_config_root_2/var/lib/calculate/config/file diff --git a/tests/templates/testfiles/test_executor_root/etc.backup/append_join_file_testfiles/file_16/file b/tests/templates/testfiles/test_executor_root/etc.backup/append_join_file_testfiles/file_16/file new file mode 100644 index 0000000..e69de29 diff --git a/tests/templates/testfiles/test_executor_root/etc.backup/clear_dir_testfiles/dir_0/file b/tests/templates/testfiles/test_executor_root/etc.backup/clear_dir_testfiles/dir_0/file new file mode 100644 index 0000000..e69de29 diff --git a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS index a2ea5d3..aad0da2 100644 --- a/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS +++ b/tests/templates/testfiles/test_wrapper_root/var/db/pkg/test-category/test-package-1.0/CONTENTS @@ -6,6 +6,6 @@ obj /etc/dir/deleted.json 11e3a79fe51cce828d973dba8702adaa 1591356795 dir /etc/dir/subdir obj /etc/dir/subdir/config.json 506649cf099878124deda2c2452c3693 1591605050 obj /etc/dir/subdir/file 84bcceb2c8e6de79849ea5f3304f2411 1591343236 -obj /etc/dir/file.conf 0b87fea7f5b65cac5012baa2bf647e72 1594324832 +obj /etc/dir/file.conf 0b87fea7f5b65cac5012baa2bf647e72 1591196541 dir /etc/dir/dir_1 -obj /etc/dir/dir_1/config.json 506649cf099878124deda2c2452c3693 1594324832 +obj /etc/dir/dir_1/config.json 506649cf099878124deda2c2452c3693 1591617024