-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.ts
173 lines (151 loc) · 4.85 KB
/
validate.ts
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
import { join } from 'path'
import { readFile, pathExists, emptyDir } from 'fs-extra'
import { load } from 'js-yaml'
import { getArg } from './utils/args'
import gitP, { SimpleGit } from 'simple-git/promise'
import {
createCommandRunner,
createCherryPick,
createTestRunner
} from './utils/exec'
import { getCommits, CommitLogObject } from './utils/commits'
import { TutorialSkeleton } from '../typings/tutorial'
interface Options {
yaml: string
clean: boolean
}
async function validate (args: string[]) {
// dir - default .
const dir = !args.length || args[0].match(/^-/) ? '.' : args[0]
const localDir = join(process.cwd(), dir)
// -y --yaml - default coderoad-config.yml
const options: Options = {
yaml: getArg(args, { name: 'yaml', alias: 'y' }) || 'coderoad.yaml',
clean: getArg(args, { name: 'clean', alias: 'c' }) !== 'false'
}
const _yaml: string = await readFile(join(localDir, options.yaml), 'utf8')
// parse yaml config
let skeleton
try {
skeleton = load(_yaml) as TutorialSkeleton
if (!skeleton) {
throw new Error('Invalid yaml file contents')
}
} catch (e) {
console.error('Error parsing yaml')
console.error(e.message)
return
}
const codeBranch: string = skeleton.config.repo.branch
// validate commits
const commits: CommitLogObject = await getCommits({ localDir, codeBranch })
// setup tmp dir
const tmpDir = join(localDir, '.tmp')
try {
if (!(await pathExists(tmpDir))) {
await emptyDir(tmpDir)
}
const tempGit: SimpleGit = gitP(tmpDir)
await tempGit.init()
await tempGit
.addRemote('origin', skeleton.config.repo.uri)
.catch(console.warn)
await tempGit.fetch('origin', skeleton.config.repo.branch)
// no js cherry pick implementation
const cherryPick = createCherryPick(tmpDir)
const runCommands = createCommandRunner(tmpDir)
const runTest = createTestRunner(tmpDir, skeleton.config.testRunner)
// setup
console.info('* Setup')
if (commits.INIT) {
// load commits
console.info('-- Loading commits...')
await cherryPick(commits.INIT)
// run commands
if (skeleton.config?.setup?.commands) {
console.info('-- Running commands...')
await runCommands(skeleton.config?.setup?.commands)
}
}
for (const level of skeleton.levels) {
console.info(`* ${level.id}`)
if (level?.setup) {
// load commits
if (commits[`${level.id}`]) {
console.log(`-- Loading commits...`)
await cherryPick(commits[level.id])
}
// run commands
if (level.setup?.commands) {
console.log(`-- Running commands...`)
await runCommands(level.setup.commands)
}
}
// steps
if (level.steps) {
for (const step of level.steps) {
console.info(`** ${step.id}`)
// load commits
const stepSetupCommits = commits[`${step.id}:T`]
if (stepSetupCommits) {
console.info(`--- Loading setup commits...`)
await cherryPick(stepSetupCommits)
}
// run commands
if (step?.setup?.commands) {
console.info(`--- Running setup commands...`)
await runCommands(step.setup.commands)
}
const stepSolutionCommits = commits[`${step.id}:S`]
const hasSolution = step.solution || stepSolutionCommits
// ignore running tests on steps with no solution
if (hasSolution) {
// run test
console.info('--- Running setup test...')
// expect fail
const { stdout, stderr } = await runTest()
if (stdout) {
console.error(
`--- Expected ${step.id} setup tests to fail, but passed`
)
// log tests
console.log(stdout)
}
}
if (stepSolutionCommits) {
console.info(`--- Loading solution commits...`)
await cherryPick(stepSolutionCommits)
}
// run commands
if (step?.solution?.commands) {
console.info(`--- Running solution commands...`)
await runCommands(step.solution.commands)
}
if (hasSolution) {
// run test
console.info('--- Running solution test...')
// expect pass
const { stdout, stderr } = await runTest()
if (stderr) {
console.error(
`--- Expected ${step.id} solution tests to pass, but failed`
)
// log tests
console.log(stderr)
}
}
}
}
}
console.info(`\n✔ Success!`)
} catch (e) {
console.error('\n✘ Fail!')
console.error(e.message)
} finally {
// cleanup
if (options.clean) {
await emptyDir(tmpDir)
}
}
}
export default validate