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

chore: better Prettier config for easier reviews

This commit is contained in:
2023-10-23 23:16:24 +02:00
parent d7d5f9a5ac
commit 1e2736b8aa
70 changed files with 1476 additions and 1382 deletions

View File

@ -1,38 +1,38 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { PassThrough } from 'node:stream'
import path from 'node:path'
import test from "node:test"
import assert from "node:assert/strict"
import { PassThrough } from "node:stream"
import path from "node:path"
import sinon from 'sinon'
import chalk from 'chalk'
import sinon from "sinon"
import chalk from "chalk"
import { cli } from '../../../cli.js'
import { cli } from "../../../cli.js"
const input = ['run', 'solution']
const challenge = 'hello-world'
const language = 'c'
const solution = 'function'
const input = ["run", "solution"]
const challenge = "hello-world"
const language = "c"
const solution = "function"
const inputPath = path.join(
process.cwd(),
'challenges',
"challenges",
challenge,
'test',
'1',
'input.txt'
"test",
"1",
"input.txt",
)
const inputChallenge = `--challenge=${challenge}`
const inputLanguage = `--language=${language}`
const inputSolution = `--solution=${solution}`
const inputInputPath = `--input-path=${inputPath}`
await test('programming-challenges run solution', async (t) => {
await test("programming-challenges run solution", async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async () => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
await t.test("succeeds", async () => {
sinon.stub(console, "log").value(() => {})
const consoleLogSpy = sinon.spy(console, "log")
const stream = new PassThrough()
const exitCode = await cli.run(
[
@ -41,13 +41,13 @@ await test('programming-challenges run solution', async (t) => {
inputSolution,
inputLanguage,
inputInputPath,
'--output'
"--output",
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 0)
@ -55,10 +55,10 @@ await test('programming-challenges run solution', async (t) => {
})
await t.test("fails with solution that doesn't exist", async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidSolution = 'invalid'
const invalidSolution = "invalid"
const inputInvalidSolution = `--solution=${invalidSolution}`
const exitCode = await cli.run(
[
@ -66,29 +66,29 @@ await test('programming-challenges run solution', async (t) => {
inputChallenge,
inputInvalidSolution,
inputLanguage,
inputInputPath
inputInputPath,
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') + ' The solution was not found.'
chalk.bold.red("Error:") + " The solution was not found.",
),
true
true,
)
})
await t.test('fails with invalid language', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with invalid language", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidLanguage = 'invalid'
const invalidLanguage = "invalid"
const inputInvalidLanguage = `--language=${invalidLanguage}`
const exitCode = await cli.run(
[
@ -96,30 +96,30 @@ await test('programming-challenges run solution', async (t) => {
inputChallenge,
inputSolution,
inputInvalidLanguage,
inputInputPath
inputInputPath,
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
' This programming language is not supported yet.'
chalk.bold.red("Error:") +
" This programming language is not supported yet.",
),
true
true,
)
})
await t.test('fails with invalid `input-path`', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with invalid `input-path`", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidInputPath = 'invalid'
const invalidInputPath = "invalid"
const inputInvalidInputPath = `--input-path=${invalidInputPath}`
const inputPath = path.resolve(process.cwd(), invalidInputPath)
const exitCode = await cli.run(
@ -128,22 +128,22 @@ await test('programming-challenges run solution', async (t) => {
inputChallenge,
inputSolution,
inputLanguage,
inputInvalidInputPath
inputInvalidInputPath,
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
` The \`input-path\` doesn't exist: ${inputPath}.`
chalk.bold.red("Error:") +
` The \`input-path\` doesn't exist: ${inputPath}.`,
),
true
true,
)
})
})

View File

@ -1,125 +1,125 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { PassThrough } from 'node:stream'
import test from "node:test"
import assert from "node:assert/strict"
import { PassThrough } from "node:stream"
import sinon from 'sinon'
import chalk from 'chalk'
import sinon from "sinon"
import chalk from "chalk"
import { cli } from '../../../cli.js'
import { SolutionTestsResult } from '../../../services/SolutionTestsResult.js'
import { cli } from "../../../cli.js"
import { SolutionTestsResult } from "../../../services/SolutionTestsResult.js"
const input = ['run', 'test']
const challenge = 'hello-world'
const language = 'c'
const solution = 'function'
const input = ["run", "test"]
const challenge = "hello-world"
const language = "c"
const solution = "function"
const inputChallenge = `--challenge=${challenge}`
const inputLanguage = `--language=${language}`
const inputSolution = `--solution=${solution}`
await test('programming-challenges run test', async (t) => {
await test("programming-challenges run test", async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async () => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
await t.test("succeeds", async () => {
sinon.stub(console, "log").value(() => {})
const consoleLogSpy = sinon.spy(console, "log")
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, inputChallenge, inputSolution, inputLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 0)
assert.strictEqual(
consoleLogSpy.calledWith(
`${chalk.bold('Name:')} ${challenge}/${language}/${solution}\n`
`${chalk.bold("Name:")} ${challenge}/${language}/${solution}\n`,
),
true
true,
)
assert.strictEqual(
consoleLogSpy.calledWith(
`${chalk.bold('Tests:')} ${chalk.bold.green('3 passed')}, 3 total`
`${chalk.bold("Tests:")} ${chalk.bold.green("3 passed")}, 3 total`,
),
true
true,
)
assert.strictEqual(
consoleLogSpy.calledWith(SolutionTestsResult.SUCCESS_MESSAGE),
true
true,
)
})
await t.test("fails with solution that doesn't exist", async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidSolution = 'invalid'
const invalidSolution = "invalid"
const inputInvalidSolution = `--solution=${invalidSolution}`
const exitCode = await cli.run(
[...input, inputChallenge, inputInvalidSolution, inputLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') + ' The solution was not found.'
chalk.bold.red("Error:") + " The solution was not found.",
),
true
true,
)
})
await t.test('fails with invalid language', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with invalid language", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidLanguage = 'invalid'
const invalidLanguage = "invalid"
const inputInvalidLanguage = `--language=${invalidLanguage}`
const exitCode = await cli.run(
[...input, inputChallenge, inputSolution, inputInvalidLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
' This programming language is not supported yet.'
chalk.bold.red("Error:") +
" This programming language is not supported yet.",
),
true
true,
)
})
await t.test('fails without options', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails without options", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const exitCode = await cli.run(input, {
stdin: process.stdin,
stdout: stream,
stderr: stream
stderr: stream,
})
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
`${chalk.bold.red(
'Error:'
)} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`
"Error:",
)} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`,
),
true
true,
)
})
})

View File

@ -1,48 +1,48 @@
import path from 'node:path'
import fs from 'node:fs'
import path from "node:path"
import fs from "node:fs"
import { Command, Option } from 'clipanion'
import * as typanion from 'typanion'
import chalk from 'chalk'
import { Command, Option } from "clipanion"
import * as typanion from "typanion"
import chalk from "chalk"
import { isExistingPath } from '../../utils/isExistingPath.js'
import { template } from '../../services/Template.js'
import { Solution } from '../../services/Solution.js'
import { TemporaryFolder } from '../../services/TemporaryFolder.js'
import { isExistingPath } from "../../utils/isExistingPath.js"
import { template } from "../../services/Template.js"
import { Solution } from "../../services/Solution.js"
import { TemporaryFolder } from "../../services/TemporaryFolder.js"
export class RunSolutionCommand extends Command {
public static override paths = [['run', 'solution']]
public static override paths = [["run", "solution"]]
public static override usage = {
description: 'Run the solution with the given `input.txt` file.'
description: "Run the solution with the given `input.txt` file.",
}
public programmingLanguage = Option.String('--language', {
description: 'The programming language used to solve the challenge.',
public programmingLanguage = Option.String("--language", {
description: "The programming language used to solve the challenge.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public challenge = Option.String('--challenge', {
description: 'The challenge name where you want to run your solution.',
public challenge = Option.String("--challenge", {
description: "The challenge name where you want to run your solution.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public solutionName = Option.String('--solution', {
description: 'The solution name to run.',
public solutionName = Option.String("--solution", {
description: "The solution name to run.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public inputPathUser = Option.String('--input-path', {
description: 'The input file path to use.',
public inputPathUser = Option.String("--input-path", {
description: "The input file path to use.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public output = Option.Boolean('--output', false, {
description: 'Display the output of the solution.'
public output = Option.Boolean("--output", false, {
description: "Display the output of the solution.",
})
public async execute(): Promise<number> {
@ -50,24 +50,24 @@ export class RunSolutionCommand extends Command {
try {
await TemporaryFolder.cleanAll()
await template.verifySupportedProgrammingLanguage(
this.programmingLanguage
this.programmingLanguage,
)
const solution = await Solution.get({
name: this.solutionName,
challengeName: this.challenge,
programmingLanguageName: this.programmingLanguage
programmingLanguageName: this.programmingLanguage,
})
const inputPath = path.resolve(process.cwd(), this.inputPathUser)
if (!(await isExistingPath(inputPath))) {
throw new Error(`The \`input-path\` doesn't exist: ${inputPath}.`)
}
const input = await fs.promises.readFile(inputPath, { encoding: 'utf-8' })
const input = await fs.promises.readFile(inputPath, { encoding: "utf-8" })
await solution.run(input, this.output)
await TemporaryFolder.cleanAll()
return 0
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
}
await TemporaryFolder.cleanAll()
return 1

View File

@ -1,47 +1,47 @@
import { Command, Option } from 'clipanion'
import * as typanion from 'typanion'
import chalk from 'chalk'
import { Command, Option } from "clipanion"
import * as typanion from "typanion"
import chalk from "chalk"
import { Solution } from '../../services/Solution.js'
import { GitAffected } from '../../services/GitAffected.js'
import { template } from '../../services/Template.js'
import { Test } from '../../services/Test.js'
import { SolutionTestsResult } from '../../services/SolutionTestsResult.js'
import { TemporaryFolder } from '../../services/TemporaryFolder.js'
import { Solution } from "../../services/Solution.js"
import { GitAffected } from "../../services/GitAffected.js"
import { template } from "../../services/Template.js"
import { Test } from "../../services/Test.js"
import { SolutionTestsResult } from "../../services/SolutionTestsResult.js"
import { TemporaryFolder } from "../../services/TemporaryFolder.js"
export class RunTestCommand extends Command {
public static override paths = [['run', 'test']]
public static override paths = [["run", "test"]]
public static override usage = {
description:
'Test if the solution is correct and display where it succeeds and fails.'
"Test if the solution is correct and display where it succeeds and fails.",
}
public programmingLanguage = Option.String('--language', {
description: 'The programming language used to solve the challenge.',
validator: typanion.isString()
public programmingLanguage = Option.String("--language", {
description: "The programming language used to solve the challenge.",
validator: typanion.isString(),
})
public challenge = Option.String('--challenge', {
description: 'The challenge name where you want to test your solution.',
validator: typanion.isString()
public challenge = Option.String("--challenge", {
description: "The challenge name where you want to test your solution.",
validator: typanion.isString(),
})
public solutionName = Option.String('--solution', {
description: 'The solution name to run.',
validator: typanion.isString()
public solutionName = Option.String("--solution", {
description: "The solution name to run.",
validator: typanion.isString(),
})
public affected = Option.Boolean('--affected', false, {
description: 'Only run the tests for the affected files in `git`.'
public affected = Option.Boolean("--affected", false, {
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 all = Option.Boolean("--all", false, {
description: "Run the tests for all the solutions.",
})
public base = Option.String('--base', {
description: 'Base of the current branch (usually master).'
public base = Option.String("--base", {
description: "Base of the current branch (usually master).",
})
public async execute(): Promise<number> {
@ -50,18 +50,20 @@ export class RunTestCommand extends Command {
await TemporaryFolder.cleanAll()
if (this.programmingLanguage != null) {
await template.verifySupportedProgrammingLanguage(
this.programmingLanguage
this.programmingLanguage,
)
}
if (this.all) {
const solutions = await Solution.getManyByProgrammingLanguages(
this.programmingLanguage != null ? [this.programmingLanguage] : undefined
this.programmingLanguage != null
? [this.programmingLanguage]
: undefined,
)
return await Test.runManyWithSolutions(solutions)
}
if (this.affected) {
const gitAffected = new GitAffected({
base: this.base
base: this.base,
})
const solutions = await gitAffected.getAffectedSolutionsFromGit()
return await Test.runManyWithSolutions(solutions)
@ -72,28 +74,28 @@ export class RunTestCommand extends Command {
this.programmingLanguage == null
) {
throw new Error(
'You must specify all the options (`--challenge`, `--solution`, `--language`).'
"You must specify all the options (`--challenge`, `--solution`, `--language`).",
)
}
const solution = await Solution.get({
name: this.solutionName,
challengeName: this.challenge,
programmingLanguageName: this.programmingLanguage
programmingLanguageName: this.programmingLanguage,
})
const result = await solution.test()
result.print({
shouldPrintBenchmark: true,
shouldPrintTableResult: true
shouldPrintTableResult: true,
})
await TemporaryFolder.cleanAll()
if (result.isSuccess) {
if (result.isSuccess) {
console.log(SolutionTestsResult.SUCCESS_MESSAGE)
return 0
}
return 1
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
}
await TemporaryFolder.cleanAll()
return 1