diff --git a/src/sinol_make/helpers/cache.py b/src/sinol_make/helpers/cache.py index dfa0f4ea..2a2d34e3 100644 --- a/src/sinol_make/helpers/cache.py +++ b/src/sinol_make/helpers/cache.py @@ -67,10 +67,7 @@ def save_compiled(file_path: str, exe_path: str, is_checker: bool = False): info.save(file_path) if is_checker: - for solution in os.listdir(paths.get_cache_path('md5sums')): - info = get_cache_file(solution) - info.tests = {} - info.save(solution) + remove_results_cache() def save_to_cache_extra_compilation_files(extra_compilation_files, task_id): @@ -102,21 +99,21 @@ def save_to_cache_extra_compilation_files(extra_compilation_files, task_id): info.save(file_path) +def remove_results_cache(): + """ + Removes all cached test results + """ + for solution in os.listdir(paths.get_cache_path('md5sums')): + info = get_cache_file(solution) + info.tests = {} + info.save(solution) + + def remove_results_if_contest_type_changed(contest_type): """ Checks if contest type has changed and removes all cached test results if it has. :param contest_type: Contest type """ - try: - with open(paths.get_cache_path("contest_type"), 'r') as cache_file: - cache_contest_type = cache_file.read().strip() - if cache_contest_type != contest_type: - for solution in os.listdir(paths.get_cache_path('md5sums')): - info = get_cache_file(solution) - info.tests = {} - info.save(solution) - except FileNotFoundError: - pass - os.makedirs(paths.get_cache_path(), exist_ok=True) - with open(paths.get_cache_path("contest_type"), 'w') as cache_file: - cache_file.write(contest_type) + if package_util.check_if_contest_type_changed(contest_type): + remove_results_cache() + package_util.save_contest_type_to_cache(contest_type) diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 4afd68f9..e4bd8b5e 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -294,3 +294,26 @@ def any_files_matching_pattern(task_id: str, pattern: str) -> bool: :return: True if any file in package matches given pattern. """ return len(get_files_matching_pattern(task_id, pattern)) > 0 + + +def check_if_contest_type_changed(contest_type): + """ + Checks if contest type in cache is different then contest type specified in config.yml. + :param contest_type: Contest type specified in config.yml. + :return: True if contest type in cache is different then contest type specified in config.yml. + """ + if not os.path.isfile(paths.get_cache_path("contest_type")): + return False + with open(paths.get_cache_path("contest_type"), "r") as contest_type_file: + cached_contest_type = contest_type_file.read() + return cached_contest_type != contest_type + + +def save_contest_type_to_cache(contest_type): + """ + Saves contest type to cache. + :param contest_type: Contest type. + """ + os.makedirs(paths.get_cache_path(), exist_ok=True) + with open(paths.get_cache_path("contest_type"), "w") as contest_type_file: + contest_type_file.write(contest_type) diff --git a/tests/commands/run/test_unit.py b/tests/commands/run/test_unit.py index e44cd467..28e770ab 100644 --- a/tests/commands/run/test_unit.py +++ b/tests/commands/run/test_unit.py @@ -96,7 +96,7 @@ def test_validate_expected_scores_success(): } results = command.validate_expected_scores(results) assert results.expected_scores != results.new_expected_scores - assert len(results.changes) == 2 + assert len(results.changes) == 3 # Test with removed solution. command.args = argparse.Namespace(solutions=None, tests=None, print_expected_scores=True)