1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat(cli): add --all option to run test command

This commit is contained in:
Divlo
2021-10-13 21:43:45 +02:00
parent aeff95e691
commit 0a27b9e1a1
47 changed files with 1657 additions and 1773 deletions

View File

@ -1,9 +1,13 @@
import fs from 'node:fs'
import path from 'node:path'
import { Command, Option } from 'clipanion'
import * as typanion from 'typanion'
import chalk from 'chalk'
import { Solution } from '../../services/Solution'
import { GitAffected } from '../../services/GitAffected'
import { template } from '../../services/Template'
const successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
@ -16,7 +20,7 @@ export class RunTestCommand extends Command {
}
public programmingLanguage = Option.String('--language', {
description: 'The programming language to use to solve the challenge.',
description: 'The programming language used to solve the challenge.',
validator: typanion.isString()
})
@ -34,6 +38,10 @@ export class RunTestCommand extends Command {
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).'
})
@ -42,21 +50,59 @@ export class RunTestCommand extends Command {
description: 'Base of the current branch (usually master)'
})
async runTests (solutions: Solution[]): Promise<number> {
for (const solution of solutions) {
await solution.test()
console.log('\n------------------------------\n')
}
console.log(successMessage)
return 0
}
async execute (): Promise<number> {
console.log()
try {
if (this.programmingLanguage != null) {
await template.verifySupportedProgrammingLanguage(this.programmingLanguage)
}
if (this.all) {
const challengesPath = path.join(
__dirname,
'..',
'..',
'..',
'challenges'
)
const challenges = await fs.promises.readdir(challengesPath)
const paths: string[] = []
for (const challenge of challenges) {
const solutionsPath = path.join(challengesPath, challenge, 'solutions')
const languagesSolution = (await fs.promises.readdir(solutionsPath)).filter(
(name) => {
if (this.programmingLanguage != null) {
return name === this.programmingLanguage
}
return name !== '.gitkeep'
}
)
for (const language of languagesSolution) {
const solutionPath = (await fs.promises.readdir(path.join(solutionsPath, language))).map((solutionName) => {
return `challenges/${challenge}/solutions/${language}/${solutionName}`
})
paths.push(...solutionPath)
}
}
const solutions = await Solution.getManyByPaths(paths)
await this.runTests(solutions)
return 0
}
if (this.affected) {
const gitAffected = new GitAffected({
isContinuousIntegration: this.isContinuousIntegration,
base: this.base
})
const solutions = await gitAffected.getAffectedSolutions()
for (const solution of solutions) {
await solution.test()
console.log('\n------------------------------\n')
}
console.log(successMessage)
return 0
return await this.runTests(solutions)
}
if (
this.solutionName == null ||