Test Adapters: Always use the last output for score runs

* This prevents learners from cheating by printing the required status line themselves
This commit is contained in:
Sebastian Serth
2021-11-02 15:52:49 +01:00
parent d7e515f1c6
commit 3d3478d859
8 changed files with 33 additions and 33 deletions

View File

@ -14,10 +14,10 @@ class CppCatch2Adapter < TestingFrameworkAdapter
if ALL_PASSED_REGEXP.match(output[:stdout]) if ALL_PASSED_REGEXP.match(output[:stdout])
{count: Regexp.last_match(1).to_i, passed: Regexp.last_match(1).to_i} {count: Regexp.last_match(1).to_i, passed: Regexp.last_match(1).to_i}
else else
count = COUNT_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 count = output[:stdout].scan(COUNT_REGEXP).try(:last).try(:first).try(:to_i) || 0
failed = FAILURES_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 failed = output[:stdout].scan(FAILURES_REGEXP).try(:last).try(:first).try(:to_i) || 0
error_matches = ASSERTION_ERROR_REGEXP.match(output[:stdout]).try(:captures) || [] error_matches = output[:stdout].scan(ASSERTION_ERROR_REGEXP) || []
{count: count, failed: failed, error_messages: error_matches} {count: count, failed: failed, error_messages: error_matches.flatten.reject(&:blank?)}
end end
end end
end end

View File

@ -10,13 +10,13 @@ class Junit5Adapter < TestingFrameworkAdapter
end end
def parse_output(output) def parse_output(output)
count = COUNT_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 count = output[:stdout].scan(COUNT_REGEXP).try(:last).try(:first).try(:to_i) || 0
failed = FAILURES_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 failed = output[:stdout].scan(FAILURES_REGEXP).try(:last).try(:first).try(:to_i) || 0
if failed.zero? if failed.zero?
{count: count, passed: count} {count: count, passed: count}
else else
error_matches = ASSERTION_ERROR_REGEXP.match(output[:stdout]).try(:captures) || [] error_matches = output[:stdout].scan(ASSERTION_ERROR_REGEXP) || []
{count: count, failed: failed, error_messages: error_matches} {count: count, failed: failed, error_messages: error_matches.flatten.reject(&:blank?)}
end end
end end
end end

View File

@ -3,7 +3,7 @@
class JunitAdapter < TestingFrameworkAdapter class JunitAdapter < TestingFrameworkAdapter
COUNT_REGEXP = /Tests run: (\d+)/.freeze COUNT_REGEXP = /Tests run: (\d+)/.freeze
FAILURES_REGEXP = /Failures: (\d+)/.freeze FAILURES_REGEXP = /Failures: (\d+)/.freeze
SUCCESS_REGEXP = /OK \((\d+) tests?\)/.freeze SUCCESS_REGEXP = /OK \((\d+) tests?\)\s*(?:\x1B\]0;)?\z/.freeze
ASSERTION_ERROR_REGEXP = /java\.lang\.AssertionError:?\s(.*?)\tat org.junit|org\.junit\.ComparisonFailure:\s(.*?)\tat org.junit/m.freeze ASSERTION_ERROR_REGEXP = /java\.lang\.AssertionError:?\s(.*?)\tat org.junit|org\.junit\.ComparisonFailure:\s(.*?)\tat org.junit/m.freeze
def self.framework_name def self.framework_name
@ -14,10 +14,10 @@ class JunitAdapter < TestingFrameworkAdapter
if SUCCESS_REGEXP.match(output[:stdout]) if SUCCESS_REGEXP.match(output[:stdout])
{count: Regexp.last_match(1).to_i, passed: Regexp.last_match(1).to_i} {count: Regexp.last_match(1).to_i, passed: Regexp.last_match(1).to_i}
else else
count = COUNT_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 count = output[:stdout].scan(COUNT_REGEXP).try(:last).try(:first).try(:to_i) || 0
failed = FAILURES_REGEXP.match(output[:stdout]).try(:captures).try(:first).try(:to_i) || 0 failed = output[:stdout].scan(FAILURES_REGEXP).try(:last).try(:first).try(:to_i) || 0
error_matches = ASSERTION_ERROR_REGEXP.match(output[:stdout]).try(:captures) || [] error_matches = output[:stdout].scan(ASSERTION_ERROR_REGEXP) || []
{count: count, failed: failed, error_messages: error_matches} {count: count, failed: failed, error_messages: error_matches.flatten.reject(&:blank?)}
end end
end end
end end

View File

@ -9,10 +9,8 @@ class MochaAdapter < TestingFrameworkAdapter
end end
def parse_output(output) def parse_output(output)
matches_success = SUCCESS_REGEXP.match(output[:stdout]) success = output[:stdout].scan(SUCCESS_REGEXP).try(:last).try(:first).try(:to_i) || 0
matches_failed = FAILURES_REGEXP.match(output[:stdout]) failed = output[:stdout].scan(FAILURES_REGEXP).try(:last).try(:first).try(:to_i) || 0
failed = matches_failed ? matches_failed.captures.first.to_i : 0
success = matches_success ? matches_success.captures.first.to_i : 0
{count: success + failed, failed: failed} {count: success + failed, failed: failed}
end end
end end

View File

@ -9,12 +9,12 @@ class PyLintAdapter < TestingFrameworkAdapter
end end
def parse_output(output) def parse_output(output)
regex_match = REGEXP.match(output[:stdout]) regex_match = output[:stdout].scan(REGEXP).try(:last)
if regex_match.blank? if regex_match.blank?
count = 0 count = 0
failed = 0 failed = 0
else else
captures = regex_match.captures.map(&:to_f) captures = regex_match.map(&:to_f)
count = captures.second count = captures.second
passed = captures.first >= 0 ? captures.first : 0 passed = captures.first >= 0 ? captures.first : 0
failed = count - passed failed = count - passed
@ -39,9 +39,13 @@ class PyLintAdapter < TestingFrameworkAdapter
Sentry.capture_message({stdout: output[:stdout], regex: ASSERTION_ERROR_REGEXP}.to_json) Sentry.capture_message({stdout: output[:stdout], regex: ASSERTION_ERROR_REGEXP}.to_json)
assertion_error_matches = [] assertion_error_matches = []
end end
concatenated_errors = assertion_error_matches.map {|result| "#{result[:name]}: #{result[:result]}" }.flatten concatenated_errors = assertion_error_matches.map {|result| "#{result[:name]}: #{result[:result]}" }
{count: count, failed: failed, error_messages: concatenated_errors, {
detailed_linter_results: assertion_error_matches} count: count,
failed: failed,
error_messages: concatenated_errors.flatten.reject(&:blank?),
detailed_linter_results: assertion_error_matches.flatten.reject(&:blank?)
}
end end
def self.translate_linter(assessment, locale) def self.translate_linter(assessment, locale)

View File

@ -12,11 +12,9 @@ class PyUnitAdapter < TestingFrameworkAdapter
def parse_output(output) def parse_output(output)
# PyUnit is expected to print test results on Stderr! # PyUnit is expected to print test results on Stderr!
count = COUNT_REGEXP.match(output[:stderr]).captures.first.to_i count = output[:stderr].scan(COUNT_REGEXP).try(:last).try(:first).try(:to_i) || 0
failures_matches = FAILURES_REGEXP.match(output[:stderr]) failed = output[:stderr].scan(FAILURES_REGEXP).try(:last).try(:first).try(:to_i) || 0
failed = failures_matches ? failures_matches.captures.try(:first).to_i : 0 errors = output[:stderr].scan(ERRORS_REGEXP).try(:last).try(:first).try(:to_i) || 0
error_matches = ERRORS_REGEXP.match(output[:stderr])
errors = error_matches ? error_matches.captures.try(:first).to_i : 0
begin begin
assertion_error_matches = Timeout.timeout(2.seconds) do assertion_error_matches = Timeout.timeout(2.seconds) do
output[:stderr].scan(ASSERTION_ERROR_REGEXP).map do |match| output[:stderr].scan(ASSERTION_ERROR_REGEXP).map do |match|
@ -28,12 +26,12 @@ class PyUnitAdapter < TestingFrameworkAdapter
else else
"#{testname}: #{error}" "#{testname}: #{error}"
end end
end.flatten || [] end || []
end end
rescue Timeout::Error rescue Timeout::Error
Sentry.capture_message({stderr: output[:stderr], regex: ASSERTION_ERROR_REGEXP}.to_json) Sentry.capture_message({stderr: output[:stderr], regex: ASSERTION_ERROR_REGEXP}.to_json)
assertion_error_matches = [] assertion_error_matches = []
end end
{count: count, failed: failed + errors, error_messages: assertion_error_matches} {count: count, failed: failed + errors, error_messages: assertion_error_matches.flatten.reject(&:blank?)}
end end
end end

View File

@ -9,11 +9,11 @@ class RScriptAdapter < TestingFrameworkAdapter
end end
def parse_output(output) def parse_output(output)
captures = REGEXP.match(output[:stdout]).captures.map(&:to_i) captures = output[:stdout].scan(REGEXP).try(:last).map(&:to_i)
count = captures.first count = captures.first
passed = captures.second passed = captures.second
failed = count - passed failed = count - passed
assertion_error_matches = output[:stdout].scan(ASSERTION_ERROR_REGEXP).flatten || [] assertion_error_matches = output[:stdout].scan(ASSERTION_ERROR_REGEXP) || []
{count: count, failed: failed, error_messages: assertion_error_matches} {count: count, failed: failed, error_messages: assertion_error_matches.flatten.reject(&:blank?)}
end end
end end

View File

@ -8,7 +8,7 @@ class RspecAdapter < TestingFrameworkAdapter
end end
def parse_output(output) def parse_output(output)
captures = REGEXP.match(output[:stdout]).captures.map(&:to_i) captures = output[:stdout].scan(REGEXP).try(:last).map(&:to_i)
count = captures.first count = captures.first
failed = captures.second failed = captures.second
{count: count, failed: failed} {count: count, failed: failed}