1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00
programming-challenges/cli/commands/run/test.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Command, Option } from 'clipanion'
import * as typanion from 'typanion'
import chalk from 'chalk'
2022-02-19 18:30:29 +01:00
import { Solution } from '../../services/Solution.js'
import { GitAffected } from '../../services/GitAffected.js'
import { template } from '../../services/Template.js'
import { Test } from '../../services/Test.js'
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 used 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', {
2022-08-30 15:48:07 +02:00
description: 'The solution name to run.',
validator: typanion.isString()
})
public affected = Option.Boolean('--affected', false, {
description: 'Only run the tests for the affected files in `git`.'
})
public all = Option.Boolean('--all', false, {
description: 'Run the tests for all the solutions.'
})
public isContinuousIntegration = Option.Boolean('--ci', false, {
description: 'Run the tests for the Continuous Integration (CI).'
})
public base = Option.String('--base', {
description: 'Base of the current branch (usually master)'
})
async execute(): Promise<number> {
console.log()
try {
if (this.programmingLanguage != null) {
await template.verifySupportedProgrammingLanguage(
this.programmingLanguage
)
}
if (this.all) {
return await Test.runAllTests(this.programmingLanguage)
}
if (this.affected) {
const gitAffected = new GitAffected({
isContinuousIntegration: this.isContinuousIntegration,
base: this.base
})
const solutions = await gitAffected.getAffectedSolutionsFromGit()
return await Test.runManyWithSolutions(solutions)
}
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(Test.SUCCESS_MESSAGE)
return 0
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
}
return 1
}
}
}