Skip to content

Commit

Permalink
Merge pull request #5837 from drew2a/fix/5829
Browse files Browse the repository at this point in the history
Add value check
  • Loading branch information
drew2a committed Dec 11, 2020
2 parents a50468f + f875e41 commit ca9a886
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/tribler-core/tribler_core/upgrade/config_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ def convert_state_file_to_conf_74(filename, refactoring_tool=None):
Converts .pstate file (pre-7.4.0) to .conf file.
:param filename: .pstate file
:param refactoring_tool: RefactoringTool instance if using Python3
:return: None
:return: fixed config
"""
def _fix_state_config(config):
for section, option in [('state', 'metainfo'), ('state', 'engineresumedata')]:
value = config.get(section, option, literal_eval=False)
if not value or not refactoring_tool:
continue

try:
value = str(refactoring_tool.refactor_string(value + '\n', option + '_2to3'))
ungarbled_dict = recursive_ungarble_metainfo(ast.literal_eval(value))
Expand Down Expand Up @@ -88,6 +91,8 @@ def _fix_state_config(config):
logger.error("Could not parse %s file on upgrade so removing it", filename)
os.remove(filename)

return fixed_config


def convert_config_to_tribler75(state_dir):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
from tribler_core.upgrade import config_converter
from tribler_core.upgrade.config_converter import convert_state_file_to_conf_74


CONFIG_PATH = TESTS_DATA_DIR / "config_files"


def test_convert_state_file_to_conf_74(tmpdir):
# pylint: disable=import-outside-toplevel, unused-argument

@pytest.fixture(name='refactoring_tool')
def fixture_refactoring_tool():
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
return RefactoringTool(fixer_names=get_fixers_from_package('lib2to3.fixes'))


def test_convert_state_file_to_conf_74(tmpdir, refactoring_tool):
"""
Tests conversion of the pstate files (pre-7.4.0) files to .conf files. Tests for two different pstate files,
one corrupted with incorrect metainfo data, and the other one with correct working metadata.
"""
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
refactoring_tool = RefactoringTool(fixer_names=get_fixers_from_package('lib2to3.fixes'))

os.makedirs(Path(tmpdir) / STATEDIR_CHECKPOINT_DIR)

# Copy a good working Ubuntu pstate file
Expand Down Expand Up @@ -51,6 +55,24 @@ def test_convert_state_file_to_conf_74(tmpdir):
assert not os.path.exists(dest_path)


def test_no_refactoring_tool(tmpdir):
state_conf = "missed_state.conf"

config_path = Path(tmpdir, state_conf)
shutil.copy(Path(CONFIG_PATH, state_conf), config_path)

assert convert_state_file_to_conf_74(config_path, None)


def test_missed_state(tmpdir, refactoring_tool):
state_conf = "missed_state.conf"

config_path = Path(tmpdir, state_conf)
shutil.copy(Path(CONFIG_PATH, state_conf), config_path)

assert convert_state_file_to_conf_74(config_path, refactoring_tool)


@pytest.fixture(name='faulty_config_parser')
async def fixture_faulty_config_parser():
def load_config_with_parse_error(filename):
Expand All @@ -62,15 +84,12 @@ def load_config_with_parse_error(filename):
config_converter.load_config = original_load_config


def test_convert_state_file_to_conf74_with_parse_error(tmpdir, faulty_config_parser):
def test_convert_state_file_to_conf74_with_parse_error(tmpdir, faulty_config_parser, refactoring_tool):
"""
Tests conversion of the state files (pre-7.4.0) files to .conf files (7.4) when there is parsing error.
ParseError happens for some users for some unknown reason. We simply remove the files that we cannot
parse. This test tests that such a corrupted file is actual deleted.
"""
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
refactoring_tool = RefactoringTool(fixer_names=get_fixers_from_package('lib2to3.fixes'))

state_dir = Path(tmpdir)
os.makedirs(state_dir / STATEDIR_CHECKPOINT_DIR)

Expand All @@ -88,4 +107,3 @@ def test_convert_state_file_to_conf74_with_parse_error(tmpdir, faulty_config_par
# We expect ParseError and the file to be deleted.
assert not os.path.exists(converted_file_path)
assert not os.path.exists(dest_path)

0 comments on commit ca9a886

Please sign in to comment.