Coverage for sel_tools/file_export/solutions_check.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
1"""Check that solutions are not published by accident."""
3from pathlib import Path
5from sel_tools.file_export.config import EXPORT_BEGIN, EXPORT_END
6from sel_tools.utils.files import FileTree, FileVisitor
9class CheckForSolutionsCodeVisitor(FileVisitor):
10 """Check if file contains solution code."""
12 def __init__(self) -> None:
13 self.__has_solutions_code = False
15 def visit_file(self, file: Path) -> None:
16 content = file.read_text()
17 self.__has_solutions_code = EXPORT_BEGIN in content or EXPORT_END in content
19 @property
20 def has_solutions_code(self) -> bool:
21 return self.__has_solutions_code
24def check_code_for_solutions_code(source_path: Path, publish_solutions: bool) -> None:
25 """Check the code to be published for solutions marker."""
26 if publish_solutions:
27 return
29 source_file_tree = FileTree(source_path)
30 solutions_check_visitor = CheckForSolutionsCodeVisitor()
31 source_file_tree.accept(solutions_check_visitor)
32 if solutions_check_visitor.has_solutions_code:
33 msg = "Solutions code found in the source code"
34 raise ValueError(msg)