2021-06-09 20:31:45 +02:00
|
|
|
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'
|
2022-09-22 16:16:21 +02:00
|
|
|
import { SolutionTestsResult } from '../../services/SolutionTestsResult.js'
|
|
|
|
import { TemporaryFolder } from '../../services/TemporaryFolder.js'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
|
|
|
export class RunTestCommand extends Command {
|
2023-01-10 23:15:36 +01:00
|
|
|
public static override paths = [['run', 'test']]
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public static override usage = {
|
2021-06-09 20:31:45 +02:00
|
|
|
description:
|
|
|
|
'Test if the solution is correct and display where it succeeds and fails.'
|
|
|
|
}
|
|
|
|
|
|
|
|
public programmingLanguage = Option.String('--language', {
|
2021-10-13 21:43:45 +02:00
|
|
|
description: 'The programming language used to solve the challenge.',
|
2021-06-09 20:31:45 +02:00
|
|
|
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.',
|
2021-06-09 20:31:45 +02:00
|
|
|
validator: typanion.isString()
|
|
|
|
})
|
|
|
|
|
|
|
|
public affected = Option.Boolean('--affected', false, {
|
|
|
|
description: 'Only run the tests for the affected files in `git`.'
|
|
|
|
})
|
|
|
|
|
2021-10-13 21:43:45 +02:00
|
|
|
public all = Option.Boolean('--all', false, {
|
|
|
|
description: 'Run the tests for all the solutions.'
|
|
|
|
})
|
|
|
|
|
2021-06-30 15:23:58 +02:00
|
|
|
public base = Option.String('--base', {
|
2022-12-30 00:55:21 +01:00
|
|
|
description: 'Base of the current branch (usually master).'
|
2021-06-30 15:23:58 +02:00
|
|
|
})
|
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public async execute(): Promise<number> {
|
2021-06-09 20:31:45 +02:00
|
|
|
console.log()
|
|
|
|
try {
|
2022-09-22 16:16:21 +02:00
|
|
|
await TemporaryFolder.cleanAll()
|
2021-10-13 21:43:45 +02:00
|
|
|
if (this.programmingLanguage != null) {
|
2022-04-24 20:27:51 +02:00
|
|
|
await template.verifySupportedProgrammingLanguage(
|
|
|
|
this.programmingLanguage
|
|
|
|
)
|
2021-10-13 21:43:45 +02:00
|
|
|
}
|
|
|
|
if (this.all) {
|
2022-09-22 16:16:21 +02:00
|
|
|
const solutions = await Solution.getManyByProgrammingLanguages(
|
|
|
|
this.programmingLanguage != null ? [this.programmingLanguage] : undefined
|
|
|
|
)
|
|
|
|
return await Test.runManyWithSolutions(solutions)
|
2021-10-13 21:43:45 +02:00
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
if (this.affected) {
|
2021-06-25 12:46:01 +02:00
|
|
|
const gitAffected = new GitAffected({
|
2021-06-30 15:23:58 +02:00
|
|
|
base: this.base
|
2021-06-25 12:46:01 +02:00
|
|
|
})
|
2021-12-07 12:11:54 +01:00
|
|
|
const solutions = await gitAffected.getAffectedSolutionsFromGit()
|
2021-11-09 16:45:42 +01:00
|
|
|
return await Test.runManyWithSolutions(solutions)
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
2022-09-22 16:16:21 +02:00
|
|
|
const result = await solution.test()
|
|
|
|
result.print({
|
|
|
|
shouldPrintBenchmark: true,
|
|
|
|
shouldPrintTableResult: true
|
|
|
|
})
|
|
|
|
await TemporaryFolder.cleanAll()
|
|
|
|
if (result.isSuccess) {
|
|
|
|
console.log(SolutionTestsResult.SUCCESS_MESSAGE)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2021-06-09 20:31:45 +02:00
|
|
|
} catch (error) {
|
2021-08-27 13:03:41 +02:00
|
|
|
if (error instanceof Error) {
|
|
|
|
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
|
|
|
|
}
|
2022-09-22 16:16:21 +02:00
|
|
|
await TemporaryFolder.cleanAll()
|
2021-06-09 20:31:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|