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

test(cli): add commands/generate/challenge

This commit is contained in:
Divlo
2021-11-30 21:42:43 +01:00
parent 88acd8cfef
commit d14fd0b62a
7 changed files with 1243 additions and 1145 deletions

View File

@ -1,13 +1,13 @@
import execa from 'execa'
import ora from 'ora'
const CONTAINER_TAG = 'programming-challenges'
class Docker {
static CONTAINER_TAG = 'programming-challenges'
public async build (): Promise<void> {
const loader = ora('Building the Docker image').start()
try {
await execa.command(`docker build --tag=${CONTAINER_TAG} ./`)
await execa.command(`docker build --tag=${Docker.CONTAINER_TAG} ./`)
loader.stop()
} catch (error) {
loader.fail()
@ -17,7 +17,7 @@ class Docker {
public async run (input: string): Promise<string> {
const subprocess = execa.command(
`docker run --interactive --rm ${CONTAINER_TAG}`,
`docker run --interactive --rm ${Docker.CONTAINER_TAG}`,
{
input
}

View File

@ -3,17 +3,11 @@ import execa from 'execa'
import { Challenge } from './Challenge'
import { Solution } from './Solution'
const solutionsRegex = new RegExp(
/challenges\/[\s\S]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\s\S]*\/(.*).(c|cpp|cs|dart|java|js|py|rs|ts)/
)
const solutionsRegex = /challenges\/[\s\S]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\s\S]*\/(.*).(c|cpp|cs|dart|java|js|py|rs|ts)/
const dockerRegex = new RegExp(
/templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/Dockerfile/
)
const dockerRegex = /templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/Dockerfile/
const inputOutputRegex = new RegExp(
/challenges\/[\s\S]*\/test\/(.*)\/(input.txt|output.txt)/
)
const inputOutputRegex = /challenges\/[\s\S]*\/test\/(.*)\/(input.txt|output.txt)/
export interface GitAffectedOptions {
isContinuousIntegration: boolean
@ -89,30 +83,16 @@ export class GitAffected implements GitAffectedOptions {
const affectedInputOutput = files.filter((filePath) => {
return inputOutputRegex.test(filePath)
})
const affectedChallenges = affectedInputOutput.map((filePath) => {
const affectedChallengesFromInputOutput = affectedInputOutput.map((filePath) => {
const [, challengeName] = filePath.replaceAll('\\', '/').split('/')
return new Challenge({ name: challengeName })
})
const solutionsChallenges = await Solution.getManyByPaths(affectedSolutionsPaths)
const solutionsDocker = await Solution.getManyByProgrammingLanguages(affectedLanguages)
const solutions: Solution[] = solutionsDocker
for (const solution of solutionsChallenges) {
if (!affectedLanguages.includes(solution.programmingLanguageName)) {
solutions.push(solution)
}
}
for (const challenge of affectedChallenges) {
let isSolutionIncluded = false
for (const solution of solutions) {
if (solution.challenge.name === challenge.name) {
isSolutionIncluded = true
break
}
}
if (!isSolutionIncluded) {
const solutionsByChallenge = await Solution.getManyByChallenge(challenge)
solutions.push(...solutionsByChallenge)
}
const solutions: Solution[] = [...solutionsDocker, ...solutionsChallenges]
for (const challenge of affectedChallengesFromInputOutput) {
const solutionsByChallenge = await Solution.getManyByChallenge(challenge)
solutions.push(...solutionsByChallenge)
}
const solutionsUnique: Solution[] = []
for (const solution of solutions) {

View File

@ -37,7 +37,7 @@ export class Test implements TestOptions {
public output: string
public stdout: string
public elapsedTimeMilliseconds: number
static successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
static SUCCESS_MESSAGE = `${chalk.bold.green('Success:')} Tests passed! 🎉`
constructor (options: TestOptions) {
this.index = options.index
@ -99,15 +99,15 @@ export class Test implements TestOptions {
const name = `${solution.challenge.name}/${solution.programmingLanguageName}/${solution.name}`
const testsPath = path.join(solution.challenge.path, 'test')
const testsFolders = await fs.promises.readdir(testsPath)
const testsNumbers = testsFolders.map((test) => Number(test)).sort((a, b) => a - b)
const tests: Test[] = []
console.log(`${chalk.bold('Name:')} ${name}\n`)
for (let index = 0; index < testsFolders.length; index++) {
const currentTestIndex = index + 1
const loader = ora(`Test n°${currentTestIndex}`).start()
for (const testNumber of testsNumbers) {
const loader = ora(`Test n°${testNumber}`).start()
try {
const test = await Test.run({
path: path.join(testsPath, testsFolders[index]),
index: currentTestIndex
path: path.join(testsPath, testNumber.toString()),
index: testNumber
})
tests.push(test)
if (test.isSuccess) {
@ -138,7 +138,7 @@ export class Test implements TestOptions {
await solution.test()
console.log('\n------------------------------\n')
}
console.log(Test.successMessage)
console.log(Test.SUCCESS_MESSAGE)
return 0
}