Skip to content

Commit b373149

Browse files
committed
Submission: Refactor execution of tests
With these changes, it is much easier to execute a run command of a submission non-interactively, too. This could be a future extension for RfCs.
1 parent c0a110f commit b373149

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

app/models/submission.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def calculate_score(requesting_user)
152152

153153
# We sort the test files, so that the linter checks are run first. This prevents a modification of the test file
154154
file_scores = assessments.sort_by {|file| file.teacher_defined_linter? ? 0 : 1 }.map.with_index(1) do |file, index|
155-
output = run_test_file file, runner, waiting_duration
155+
output = run_file :test_command, file, runner, waiting_duration
156156
# If the previous execution failed and there is at least one more test, we request a new runner.
157157
swap_runner(runner) if output[:status] == :timeout && index < assessment_number
158158
score_file(output, file, requesting_user)
@@ -181,23 +181,9 @@ def run(file, &block)
181181
durations
182182
end
183183

184-
# @raise [Runner::Error] if the file could not be tested due to a failure with the runner.
185-
# See the specific type and message for more details.
186184
def test(file, requesting_user)
187-
prepared_runner do |runner, waiting_duration|
188-
output = run_test_file file, runner, waiting_duration
189-
score_file output, file, requesting_user
190-
rescue Runner::Error => e
191-
e.waiting_duration = waiting_duration
192-
raise
193-
end
194-
end
195-
196-
def run_test_file(file, runner, waiting_duration)
197-
test_command = command_for execution_environment.test_command, file.filepath
198-
result = {file_role: file.role, waiting_for_container_time: waiting_duration}
199-
output = runner.execute_command(test_command, raise_exception: false, exclusive: false)
200-
result.merge(output)
185+
output = execute :test_command, file
186+
score_file output, file, requesting_user
201187
end
202188

203189
def self.ransackable_attributes(_auth_object = nil)
@@ -277,6 +263,24 @@ def command_substitutions(filename)
277263
}
278264
end
279265

266+
# @raise [Runner::Error] if the file could not be tested due to a failure with the runner.
267+
# See the specific type and message for more details.
268+
def execute(action, file)
269+
prepared_runner do |runner, waiting_duration|
270+
run_file action, file, runner, waiting_duration
271+
rescue Runner::Error => e
272+
e.waiting_duration = waiting_duration
273+
raise
274+
end
275+
end
276+
277+
def run_file(action, file, runner, waiting_duration)
278+
command = command_for execution_environment.public_send(action), file.filepath
279+
result = {file_role: file.role, waiting_for_container_time: waiting_duration}
280+
output = runner.execute_command(command, raise_exception: false, exclusive: false)
281+
result.merge(output)
282+
end
283+
280284
def score_file(output, file, requesting_user)
281285
assessor = Assessor.new(execution_environment:)
282286
assessment = assessor.assess(output)

0 commit comments

Comments
 (0)