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:
@ -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 ||
|
||||
|
@ -1,8 +1,6 @@
|
||||
import execa from 'execa'
|
||||
|
||||
import { Challenge } from './Challenge'
|
||||
import { Solution } from './Solution'
|
||||
import { isExistingPath } from '../utils/isExistingPath'
|
||||
|
||||
const solutionsRegex = new RegExp(
|
||||
/challenges\/[\s\S]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\s\S]*\/(solution|Solution).(c|cpp|cs|dart|java|js|py|rs|ts)/
|
||||
@ -72,22 +70,6 @@ export class GitAffected implements GitAffectedOptions {
|
||||
const affectedSolutionsPaths = files.filter((filePath) => {
|
||||
return solutionsRegex.test(filePath)
|
||||
})
|
||||
const solutions: string[] = []
|
||||
for (const path of affectedSolutionsPaths) {
|
||||
if (await isExistingPath(path)) {
|
||||
solutions.push(path)
|
||||
}
|
||||
}
|
||||
return solutions.map((solution) => {
|
||||
const [, challengeName, , programmingLanguageName, solutionName] =
|
||||
solution.split('/')
|
||||
return new Solution({
|
||||
challenge: new Challenge({
|
||||
name: challengeName
|
||||
}),
|
||||
name: solutionName,
|
||||
programmingLanguageName
|
||||
})
|
||||
})
|
||||
return await Solution.getManyByPaths(affectedSolutionsPaths)
|
||||
}
|
||||
}
|
||||
|
@ -101,4 +101,29 @@ export class Solution implements SolutionOptions {
|
||||
}
|
||||
return solution
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Solutions by relative paths.
|
||||
* @param paths relative to `challenges` (e.g: `challenges/hello-world/solutions/c/function`)
|
||||
* @returns
|
||||
*/
|
||||
static async getManyByPaths (paths: string[]): Promise<Solution[]> {
|
||||
const solutions: string[] = []
|
||||
for (const path of paths) {
|
||||
if (await isExistingPath(path)) {
|
||||
solutions.push(path)
|
||||
}
|
||||
}
|
||||
return solutions.map((solution) => {
|
||||
const [, challengeName, , programmingLanguageName, solutionName] =
|
||||
solution.split('/')
|
||||
return new Solution({
|
||||
challenge: new Challenge({
|
||||
name: challengeName
|
||||
}),
|
||||
name: solutionName,
|
||||
programmingLanguageName
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -73,9 +73,7 @@ class Template {
|
||||
public async solution (options: TemplateSolutionOptions): Promise<void> {
|
||||
const { destination, githubUser, name, challengeName, programmingLanguageName } = options
|
||||
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, programmingLanguageName)
|
||||
if (!(await isExistingPath(templateLanguagePath))) {
|
||||
throw new Error('This programming language is not supported yet.')
|
||||
}
|
||||
await this.verifySupportedProgrammingLanguage(programmingLanguageName)
|
||||
await fs.promises.mkdir(destination, { recursive: true })
|
||||
await copyDirectory(templateLanguagePath, destination)
|
||||
await copyDirectory(TEMPLATE_SOLUTION_BASE_PATH, destination)
|
||||
@ -95,6 +93,13 @@ class Template {
|
||||
destination
|
||||
})
|
||||
}
|
||||
|
||||
public async verifySupportedProgrammingLanguage (language: string): Promise<void> {
|
||||
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, language)
|
||||
if (!(await isExistingPath(templateLanguagePath))) {
|
||||
throw new Error('This programming language is not supported yet.')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const template = new Template()
|
||||
|
Reference in New Issue
Block a user