1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

fix(cli): changes to input/output should run all affected tests

This commit is contained in:
Divlo 2021-11-10 18:57:10 +01:00
parent 17efe8a113
commit d18ca22470
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
5 changed files with 47 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import chalk from 'chalk'
import { Solution } from '../../services/Solution'
import { GitAffected } from '../../services/GitAffected'
import { template } from '../../services/Template'
import { Test, successMessage } from '../../services/Test'
import { Test } from '../../services/Test'
export class RunTestCommand extends Command {
static paths = [['run', 'test']]
@ -78,7 +78,7 @@ export class RunTestCommand extends Command {
programmingLanguageName: this.programmingLanguage
})
await solution.test()
console.log(successMessage)
console.log(Test.successMessage)
return 0
} catch (error) {
if (error instanceof Error) {

View File

@ -1,5 +1,6 @@
import execa from 'execa'
import { Challenge } from './Challenge'
import { Solution } from './Solution'
const solutionsRegex = new RegExp(
@ -10,6 +11,10 @@ const dockerRegex = new RegExp(
/templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/Dockerfile/
)
const inputOutputRegex = new RegExp(
/challenges\/[\s\S]*\/test\/[0-9]\/(input.txt|output.txt)/
)
export interface GitAffectedOptions {
isContinuousIntegration: boolean
base?: string
@ -81,6 +86,13 @@ export class GitAffected implements GitAffectedOptions {
const [,, programmingLanguageName] = filePath.replaceAll('\\', '/').split('/')
return programmingLanguageName
})
const affectedInputOutput = files.filter((filePath) => {
return inputOutputRegex.test(filePath)
})
const affectedChallenges = 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
@ -89,6 +101,19 @@ export class GitAffected implements GitAffectedOptions {
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)
}
}
return solutions
}
}

View File

@ -103,6 +103,23 @@ export class Solution implements SolutionOptions {
return solution
}
static async getManyByChallenge (challenge: Challenge): Promise<Solution[]> {
const solutionsPath = path.join(challenge.path, 'solutions')
const languagesSolution = (await fs.promises.readdir(solutionsPath)).filter(
(name) => {
return name !== '.gitkeep'
}
)
const paths: string[] = []
for (const language of languagesSolution) {
const solutionPath = (await fs.promises.readdir(path.join(solutionsPath, language))).map((solutionName) => {
return `challenges/${challenge.name}/solutions/${language}/${solutionName}`
})
paths.push(...solutionPath)
}
return await Solution.getManyByPaths(paths)
}
static async getManyByProgrammingLanguages (programmingLanguages?: string[]): Promise<Solution[]> {
const languages = programmingLanguages ?? await template.getProgrammingLanguages()
const challengesPath = path.join(

View File

@ -29,8 +29,6 @@ export interface TestOptions {
elapsedTimeMilliseconds: number
}
export const successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
export class Test implements TestOptions {
public index: number
public path: string
@ -39,6 +37,7 @@ export class Test implements TestOptions {
public output: string
public stdout: string
public elapsedTimeMilliseconds: number
static successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
constructor (options: TestOptions) {
this.index = options.index
@ -139,7 +138,7 @@ export class Test implements TestOptions {
await solution.test()
console.log('\n------------------------------\n')
}
console.log(successMessage)
console.log(Test.successMessage)
return 0
}

View File

@ -5,7 +5,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/Divlo/programming-challenges"
"url": "https://github.com/Divlo/programming-challenges"
},
"engines": {
"node": ">=16.0.0",