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

fix(cli): changes to templates/docker, should run all affected tests

This commit is contained in:
Divlo
2021-11-09 16:45:42 +01:00
parent 9621751a4d
commit 17efe8a113
10 changed files with 1241 additions and 1209 deletions

View File

@ -6,6 +6,10 @@ 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)/
)
const dockerRegex = new RegExp(
/templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/Dockerfile/
)
export interface GitAffectedOptions {
isContinuousIntegration: boolean
base?: string
@ -70,6 +74,21 @@ export class GitAffected implements GitAffectedOptions {
const affectedSolutionsPaths = files.filter((filePath) => {
return solutionsRegex.test(filePath)
})
return await Solution.getManyByPaths(affectedSolutionsPaths)
const affectedDockerPaths = files.filter((filePath) => {
return dockerRegex.test(filePath)
})
const affectedLanguages = affectedDockerPaths.map((filePath) => {
const [,, programmingLanguageName] = filePath.replaceAll('\\', '/').split('/')
return programmingLanguageName
})
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)
}
}
return solutions
}
}

View File

@ -1,4 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import {
createTemporaryEmptyFolder,
@ -102,6 +103,33 @@ export class Solution implements SolutionOptions {
return solution
}
static async getManyByProgrammingLanguages (programmingLanguages?: string[]): Promise<Solution[]> {
const languages = programmingLanguages ?? await template.getProgrammingLanguages()
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) => {
return name !== '.gitkeep' && languages.includes(name)
}
)
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)
}
}
return await Solution.getManyByPaths(paths)
}
/**
* Get Solutions by relative paths.
* @param paths relative to `challenges` (e.g: `challenges/hello-world/solutions/c/function`)

View File

@ -5,7 +5,6 @@ import { replaceInFile } from 'replace-in-file'
import date from 'date-and-time'
import { copyDirectory } from '../utils/copyDirectory'
import { isExistingPath } from '../utils/isExistingPath'
const TEMPLATE_PATH = path.join(__dirname, '..', '..', 'templates')
const TEMPLATE_DOCKER_PATH = path.join(TEMPLATE_PATH, 'docker')
@ -94,9 +93,14 @@ class Template {
})
}
public async getProgrammingLanguages (): Promise<string[]> {
const languages = await fs.promises.readdir(TEMPLATE_SOLUTION_PATH)
return languages.filter(language => language !== 'base')
}
public async verifySupportedProgrammingLanguage (language: string): Promise<void> {
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, language)
if (!(await isExistingPath(templateLanguagePath))) {
const languages = await this.getProgrammingLanguages()
if (!languages.includes(language)) {
throw new Error('This programming language is not supported yet.')
}
}

View File

@ -29,6 +29,8 @@ 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
@ -132,6 +134,23 @@ export class Test implements TestOptions {
return { input, output }
}
static async runManyWithSolutions (solutions: Solution[]): Promise<number> {
for (const solution of solutions) {
await solution.test()
console.log('\n------------------------------\n')
}
console.log(successMessage)
return 0
}
static async runAllTests (programmingLanguage?: string): Promise<number> {
const solutions = await Solution.getManyByProgrammingLanguages(
programmingLanguage != null ? [programmingLanguage] : undefined
)
await Test.runManyWithSolutions(solutions)
return 0
}
static async run (options: TestRunOptions): Promise<Test> {
const { input, output } = await Test.getInputOutput(options.path)
const start = performance.now()