parent
464a0dd1ae
commit
a37a6b1cde
@ -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)
|
||||
|
@ -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
|
@ -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
|
Loading…
Reference in new issue