diff --git a/cli/commands/run/test.ts b/cli/commands/run/test.ts index 488cda9..d005546 100644 --- a/cli/commands/run/test.ts +++ b/cli/commands/run/test.ts @@ -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) { diff --git a/cli/services/GitAffected.ts b/cli/services/GitAffected.ts index 358288a..31b1adf 100644 --- a/cli/services/GitAffected.ts +++ b/cli/services/GitAffected.ts @@ -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 } } diff --git a/cli/services/Solution.ts b/cli/services/Solution.ts index a025d4a..53eecad 100644 --- a/cli/services/Solution.ts +++ b/cli/services/Solution.ts @@ -103,6 +103,23 @@ export class Solution implements SolutionOptions { return solution } + static async getManyByChallenge (challenge: Challenge): Promise { + 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 { const languages = programmingLanguages ?? await template.getProgrammingLanguages() const challengesPath = path.join( diff --git a/cli/services/Test.ts b/cli/services/Test.ts index 076c269..380635d 100644 --- a/cli/services/Test.ts +++ b/cli/services/Test.ts @@ -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 } diff --git a/package.json b/package.json index cda79ac..bfd29df 100644 --- a/package.json +++ b/package.json @@ -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",