mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { Command, Option } from 'clipanion'
|
|
import * as typanion from 'typanion'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import { Solution } from '../../services/Solution'
|
|
import { GitAffected } from '../../services/GitAffected'
|
|
|
|
const successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
|
|
|
|
export class RunTestCommand extends Command {
|
|
static paths = [['run', 'test']]
|
|
|
|
static usage = {
|
|
description:
|
|
'Test if the solution is correct and display where it succeeds and fails.'
|
|
}
|
|
|
|
public programmingLanguage = Option.String('--language', {
|
|
description: 'The programming language to use to solve the challenge.',
|
|
validator: typanion.isString()
|
|
})
|
|
|
|
public challenge = Option.String('--challenge', {
|
|
description: 'The challenge name where you want to test your solution.',
|
|
validator: typanion.isString()
|
|
})
|
|
|
|
public solutionName = Option.String('--solution', {
|
|
description: 'solution',
|
|
validator: typanion.isString()
|
|
})
|
|
|
|
public affected = Option.Boolean('--affected', false, {
|
|
description: 'Only run the tests for the affected files in `git`.'
|
|
})
|
|
|
|
public isContinuousIntegration = Option.Boolean('--ci', false, {
|
|
description: 'Run the tests for the Continuous Integration (CI).'
|
|
})
|
|
|
|
async execute (): Promise<number> {
|
|
console.log()
|
|
try {
|
|
if (this.affected) {
|
|
const gitAffected = new GitAffected({
|
|
isContinuousIntegration: this.isContinuousIntegration
|
|
})
|
|
const solutions = await gitAffected.getAffectedSolutions()
|
|
for (const solution of solutions) {
|
|
await solution.test()
|
|
console.log('\n------------------------------\n')
|
|
}
|
|
console.log(successMessage)
|
|
return 0
|
|
}
|
|
if (
|
|
this.solutionName == null ||
|
|
this.challenge == null ||
|
|
this.programmingLanguage == null
|
|
) {
|
|
throw new Error(
|
|
'You must specify all the options (`--challenge`, `--solution`, `--language`).'
|
|
)
|
|
}
|
|
const solution = await Solution.get({
|
|
name: this.solutionName,
|
|
challengeName: this.challenge,
|
|
programmingLanguageName: this.programmingLanguage
|
|
})
|
|
await solution.test()
|
|
console.log(successMessage)
|
|
return 0
|
|
} catch (error) {
|
|
console.error(`\n${chalk.bold.red('Error:')} ${error.message as string}`)
|
|
return 1
|
|
}
|
|
}
|
|
}
|