forked from manytask/checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp.py
188 lines (172 loc) · 6.49 KB
/
cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from ..exceptions import (
BuildFailedError,
ExecutionFailedError,
StylecheckFailedError,
TestsFailedError,
TimeoutExpiredError,
)
from ..utils.files import check_files_contains_regexp, copy_files
from ..utils.print import print_info
from .tester import Tester
class CppTester(Tester):
@dataclass
class TaskTestConfig(Tester.TaskTestConfig):
allow_change: list[str] = field(default_factory=list)
forbidden_regexp: list[str] = field(default_factory=list)
copy_to_build: list[str] = field(default_factory=list)
linter: bool = True
build_type: str = 'Asan'
is_crash_me: bool = False
tests: list[str] = field(default_factory=list)
input_file: dict[str, str] = field(default_factory=dict)
args: dict[str, list[str]] = field(default_factory=dict)
timeout: float = 60.
capture_output: bool = True
def __post_init__(
self,
) -> None:
assert self.tests
assert self.allow_change
def _gen_build( # type: ignore[override]
self,
test_config: TaskTestConfig,
build_dir: Path,
source_dir: Path,
public_tests_dir: Path | None,
private_tests_dir: Path | None,
sandbox: bool = True,
verbose: bool = False,
normalize_output: bool = False,
) -> None:
check_files_contains_regexp(
source_dir,
regexps=test_config.forbidden_regexp,
patterns=test_config.allow_change,
raise_on_found=True,
)
reference_root = public_tests_dir.parent # type: ignore
task_name = source_dir.name
task_dir = reference_root / task_name
self._executor(
copy_files,
source=source_dir,
target=task_dir,
patterns=test_config.allow_change,
verbose=verbose,
)
self._executor(
copy_files,
source=task_dir,
target=build_dir,
patterns=test_config.copy_to_build,
verbose=verbose,
)
try:
print_info('Running cmake...', color='orange')
self._executor(
['cmake', '-G', 'Ninja', str(reference_root),
'-DGRADER=YES', '-DENABLE_PRIVATE_TESTS=YES',
f'-DCMAKE_BUILD_TYPE={test_config.build_type}'],
cwd=build_dir,
verbose=verbose,
)
except ExecutionFailedError:
print_info('ERROR', color='red')
raise BuildFailedError('cmake execution failed')
for test_binary in test_config.tests:
try:
print_info(f'Building {test_binary}...', color='orange')
self._executor(
['ninja', '-v', test_binary],
cwd=build_dir,
verbose=verbose,
)
except ExecutionFailedError:
print_info('ERROR', color='red')
raise BuildFailedError(f'Can\'t build {test_binary}')
if not test_config.linter:
return
try:
print_info('Running clang format...', color='orange')
format_path = reference_root / 'run-clang-format.py'
self._executor(
[str(format_path), '-r', str(task_dir)],
cwd=build_dir,
verbose=verbose,
)
print_info('[No issues]')
print_info('OK', color='green')
except ExecutionFailedError:
print_info('ERROR', color='red')
raise StylecheckFailedError('Style error (clang format)')
try:
print_info('Running clang tidy...', color='orange')
files = [str(file) for file in task_dir.rglob('*.cpp')]
self._executor(
['clang-tidy', '-p', '.', *files],
cwd=build_dir,
verbose=verbose,
)
print_info('[No issues]')
print_info('OK', color='green')
except ExecutionFailedError:
print_info('ERROR', color='red')
raise StylecheckFailedError('Style error (clang tidy)')
def _clean_build( # type: ignore[override]
self,
test_config: TaskTestConfig,
build_dir: Path,
verbose: bool = False,
) -> None:
self._executor(
['rm', '-rf', str(build_dir)],
check=False,
verbose=verbose,
)
def _run_tests( # type: ignore[override]
self,
test_config: TaskTestConfig,
build_dir: Path,
sandbox: bool = False,
verbose: bool = False,
normalize_output: bool = False,
) -> float:
for test_binary in test_config.tests:
stdin = None
try:
print_info(f'Running {test_binary}...', color='orange')
args = test_config.args.get(test_binary, [])
if test_binary in test_config.input_file:
stdin = open(build_dir / test_config.input_file[test_binary], 'r')
self._executor(
[str(build_dir / test_binary), *args],
sandbox=True,
cwd=build_dir,
verbose=verbose,
capture_output=test_config.capture_output,
timeout=test_config.timeout,
stdin=stdin
)
if test_config.is_crash_me:
print_info('ERROR', color='red')
raise TestsFailedError('Program has not crashed')
print_info('OK', color='green')
except TimeoutExpiredError:
print_info('ERROR', color='red')
message = f'Your solution exceeded time limit: {test_config.timeout} seconds'
raise TestsFailedError(message)
except ExecutionFailedError:
if not test_config.is_crash_me:
print_info('ERROR', color='red')
raise TestsFailedError("Test failed (wrong answer or sanitizer error)")
finally:
if stdin is not None:
stdin.close()
if test_config.is_crash_me:
print_info('Program has crashed', color='green')
else:
print_info('All tests passed', color='green')
return 1.