-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidate.ts
More file actions
69 lines (61 loc) · 2.2 KB
/
Copy pathvalidate.ts
File metadata and controls
69 lines (61 loc) · 2.2 KB
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
import type { Issue } from '../data/types'
import type { RunResult } from './compile'
export type CheckOutcome = {
passed: boolean
/** Short verdict line shown in the console. */
reason: string
/** Optional extra detail (e.g. expected vs. actual output). */
detail?: string
}
/** Trim trailing whitespace per line and collapse trailing blank lines. */
export function normalizeOutput(s: string): string {
return s
.replace(/\r\n/g, '\n')
.split('\n')
.map((l) => l.replace(/\s+$/, ''))
.join('\n')
.replace(/\n+$/, '')
.trim()
}
/** Decide whether a compiled+executed submission solves the issue. */
export function checkSolution(
issue: Issue,
result: RunResult,
source: string,
): CheckOutcome {
if (result.error) {
return result.error === 'aborted'
? { passed: false, reason: 'Run cancelled.' }
: { passed: false, reason: `Could not reach the compiler: ${result.error}` }
}
if (!result.compiled) {
return { passed: false, reason: 'Compilation failed — check the errors above.' }
}
if (result.timedOut) {
return { passed: false, reason: 'Execution timed out — does your program terminate?' }
}
if (!result.executed || result.exitCode !== 0) {
const code = result.exitCode === null ? 'unknown' : String(result.exitCode)
return { passed: false, reason: `Program exited abnormally (exit code ${code}).` }
}
if (issue.validation.kind === 'run') {
const want = normalizeOutput(issue.validation.expectedStdout)
const got = normalizeOutput(result.stdout)
if (got === want) {
return { passed: true, reason: 'Compiled, ran, and output matches. Ticket resolved!' }
}
return {
passed: false,
reason: 'It compiles and runs, but the output is not what we expect.',
detail: `Expected:\n${want}\n\nGot:\n${got || '(no output)'}`,
}
}
// kind === 'source': compiled + ran cleanly, now require the idiom in the code.
const { patterns, flags, message } = issue.validation
for (const p of patterns) {
if (!new RegExp(p, flags).test(source)) {
return { passed: false, reason: message }
}
}
return { passed: true, reason: 'Compiled, ran, and the required change is in place. Resolved!' }
}