Skip to content

Commit 092d907

Browse files
authored
Remove tests cache on correct solution change (#159)
* Remove tests cache when correct solutions changes * Add tests * Refactor
1 parent 40fdbbc commit 092d907

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/sinol_make/commands/gen/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sinol_make import util
99
from sinol_make.commands.gen import gen_util
1010
from sinol_make.structs.gen_structs import OutputGenerationArguments
11-
from sinol_make.helpers import parsers, package_util
11+
from sinol_make.helpers import parsers, package_util, cache
1212
from sinol_make.interfaces.BaseCommand import BaseCommand
1313

1414

@@ -108,6 +108,7 @@ def run(self, args: argparse.Namespace):
108108
self.task_id = package_util.get_task_id()
109109
package_util.validate_test_names(self.task_id)
110110
util.change_stack_size_to_unlimited()
111+
cache.check_correct_solution(self.task_id)
111112
if self.ins:
112113
self.ingen = gen_util.get_ingen(self.task_id, args.ingen_path)
113114
print(util.info(f'Using ingen file {os.path.basename(self.ingen)}'))

src/sinol_make/helpers/cache.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,30 @@ def check_can_access_cache():
131131
except PermissionError:
132132
util.exit_with_error("You don't have permission to access the `.cache/` directory. "
133133
"`sinol-make` needs to be able to write to this directory.")
134+
135+
136+
def has_file_changed(file_path: str) -> bool:
137+
"""
138+
Checks if file has changed since last compilation.
139+
:param file_path: Path to the file
140+
:return: True if file has changed, False otherwise
141+
"""
142+
try:
143+
info = get_cache_file(file_path)
144+
return info.md5sum != util.get_file_md5(file_path)
145+
except FileNotFoundError:
146+
return True
147+
148+
149+
def check_correct_solution(task_id: str):
150+
"""
151+
Checks if correct solution has changed. If it did, removes cache for input files.
152+
:param task_id: Task id
153+
"""
154+
try:
155+
solution = package_util.get_correct_solution(task_id)
156+
except FileNotFoundError:
157+
return
158+
159+
if has_file_changed(solution) and os.path.exists(os.path.join(os.getcwd(), 'in', '.md5sums')):
160+
os.unlink(os.path.join(os.getcwd(), 'in', '.md5sums'))

src/sinol_make/helpers/package_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ def get_solutions(task_id: str, args_solutions: Union[List[str], None] = None) -
146146
return sorted(solutions, key=lambda solution: get_executable_key(solution, task_id))
147147

148148

149+
def get_correct_solution(task_id: str) -> str:
150+
"""
151+
Returns path to correct solution.
152+
:param task_id: Task id.
153+
:return: Path to correct solution.
154+
"""
155+
correct_solution = get_solutions(task_id, [f'{task_id}.*'])
156+
if len(correct_solution) == 0:
157+
raise FileNotFoundError("Correct solution not found.")
158+
return os.path.join(os.getcwd(), "prog", correct_solution[0])
159+
160+
149161
def get_file_name(file_path):
150162
return os.path.split(file_path)[1]
151163

tests/commands/gen/test_integration.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import glob
55

66
from sinol_make import configure_parsers
7+
from sinol_make import util as sm_util
78
from sinol_make.commands.gen import Command
89
from sinol_make.commands.gen import gen_util
9-
from sinol_make.helpers import package_util, paths
10+
from sinol_make.helpers import package_util, paths, cache
1011
from tests.fixtures import *
1112
from tests import util
1213

@@ -47,6 +48,9 @@ def test_correct_inputs(capsys, create_package):
4748
"""
4849
Test `ingen` command with all unchanged inputs.
4950
"""
51+
task_id = package_util.get_task_id()
52+
correct_solution = package_util.get_correct_solution(task_id)
53+
cache.save_compiled(correct_solution, "exe")
5054
simple_run()
5155
md5_sums = get_md5_sums(create_package)
5256

@@ -63,6 +67,9 @@ def test_changed_inputs(capsys, create_package):
6367
"""
6468
Test `ingen` command with changed inputs.
6569
"""
70+
task_id = package_util.get_task_id()
71+
correct_solution = package_util.get_correct_solution(task_id)
72+
cache.save_compiled(correct_solution, "exe")
6673
simple_run()
6774
md5_sums = get_md5_sums(create_package)
6875
correct_md5 = md5_sums.copy()
@@ -152,3 +159,29 @@ def test_missing_output_files(create_package):
152159
assert os.path.exists(outs[0])
153160
shutil.rmtree(paths.get_cache_path())
154161
os.unlink(os.path.join(package_path, "in", ".md5sums"))
162+
163+
164+
@pytest.mark.parametrize("create_package", [util.get_shell_ingen_pack_path(), util.get_simple_package_path()],
165+
indirect=True)
166+
def test_correct_solution_changed(create_package):
167+
"""
168+
Test if `.md5sums` is deleted when correct solution is changed.
169+
"""
170+
package_path = create_package
171+
task_id = package_util.get_task_id()
172+
md5sums = os.path.join(package_path, "in", ".md5sums")
173+
simple_run()
174+
assert os.path.exists(md5sums)
175+
outputs = {}
176+
for output in glob.glob(os.path.join(package_path, "out", "*.out")):
177+
outputs[os.path.basename(output)] = sm_util.get_file_md5(output)
178+
179+
solution = package_util.get_correct_solution(task_id)
180+
with open(os.path.join(solution), "w") as f:
181+
f.write("int main() {}")
182+
cache.check_correct_solution(task_id)
183+
assert not os.path.exists(md5sums)
184+
simple_run()
185+
assert os.path.exists(md5sums)
186+
for output in glob.glob(os.path.join(package_path, "out", "*.out")):
187+
assert outputs[os.path.basename(output)] != sm_util.get_file_md5(output)

0 commit comments

Comments
 (0)