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,34 +1,34 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { PassThrough } from 'node:stream'
import path from 'node:path'
import fs from 'node:fs'
import test from "node:test"
import assert from "node:assert/strict"
import { PassThrough } from "node:stream"
import path from "node:path"
import fs from "node:fs"
import sinon from 'sinon'
import fsMock from 'mock-fs'
import chalk from 'chalk'
import getStream from 'get-stream'
import date from 'date-and-time'
import sinon from "sinon"
import fsMock from "mock-fs"
import chalk from "chalk"
import getStream from "get-stream"
import date from "date-and-time"
import { cli } from '../../../cli.js'
import { isExistingPath } from '../../../utils/isExistingPath.js'
import { cli } from "../../../cli.js"
import { isExistingPath } from "../../../utils/isExistingPath.js"
const input = ['generate', 'challenge']
const githubUser = 'theoludwig'
const challenge = 'aaaa-test-jest'
const input = ["generate", "challenge"]
const githubUser = "theoludwig"
const challenge = "aaaa-test-jest"
const inputChallenge = `--challenge=${challenge}`
const inputGitHubUser = `--github-user=${githubUser}`
await test('programming-challenges generate challenge', async (t) => {
await test("programming-challenges generate challenge", async (t) => {
t.beforeEach(() => {
fsMock(
{
[process.cwd()]: fsMock.load(process.cwd(), {
recursive: true,
lazy: true
})
lazy: true,
}),
},
{ createCwd: false }
{ createCwd: false },
)
})
@ -37,28 +37,28 @@ await test('programming-challenges generate challenge', async (t) => {
sinon.restore()
})
await t.test('succeeds and generate the new challenge', async () => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
const dateString = date.format(new Date(), 'D MMMM Y', true)
await t.test("succeeds and generate the new challenge", async () => {
sinon.stub(console, "log").value(() => {})
const consoleLogSpy = sinon.spy(console, "log")
const dateString = date.format(new Date(), "D MMMM Y", true)
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, inputChallenge, inputGitHubUser],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 0)
const challengePath = path.join(process.cwd(), 'challenges', challenge)
const readmePath = path.join(challengePath, 'README.md')
const challengePath = path.join(process.cwd(), "challenges", challenge)
const readmePath = path.join(challengePath, "README.md")
const readmeContent = await fs.promises.readFile(readmePath, {
encoding: 'utf-8'
encoding: "utf-8",
})
const successMessage = `${chalk.bold.green(
'Success:'
"Success:",
)} created the new challenge at ${challengePath}.`
assert.strictEqual(consoleLogSpy.calledWith(successMessage), true)
assert.strictEqual(await isExistingPath(challengePath), true)
@ -75,17 +75,17 @@ Description of the challenge...
## Examples
See the \`test\` folder for examples of input/output.
`
`,
)
})
await t.test('fails without options', async () => {
await t.test("fails without options", async () => {
const stream = new PassThrough()
const promise = getStream(stream)
const exitCode = await cli.run(input, {
stdin: process.stdin,
stdout: stream,
stderr: stream
stderr: stream,
})
stream.end()
assert.strictEqual(exitCode, 1)
@ -93,47 +93,49 @@ See the \`test\` folder for examples of input/output.
assert.match(output, /Unknown Syntax Error/)
})
await t.test('fails with already existing challenge', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with already existing challenge", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, '--challenge=hello-world', inputGitHubUser],
[...input, "--challenge=hello-world", inputGitHubUser],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
`${chalk.bold.red('Error:')} The challenge already exists: hello-world.`
`${chalk.bold.red(
"Error:",
)} The challenge already exists: hello-world.`,
),
true
true,
)
})
await t.test('fails with invalid challenge name', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with invalid challenge name", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, '--challenge=hEllO-world', inputGitHubUser],
[...input, "--challenge=hEllO-world", inputGitHubUser],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
`${chalk.bold.red('Error:')} Invalid challenge name.`
`${chalk.bold.red("Error:")} Invalid challenge name.`,
),
true
true,
)
})
})

View File

@ -1,35 +1,35 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { PassThrough } from 'node:stream'
import path from 'node:path'
import fs from 'node:fs'
import test from "node:test"
import assert from "node:assert/strict"
import { PassThrough } from "node:stream"
import path from "node:path"
import fs from "node:fs"
import sinon from 'sinon'
import fsMock from 'mock-fs'
import chalk from 'chalk'
import getStream from 'get-stream'
import date from 'date-and-time'
import sinon from "sinon"
import fsMock from "mock-fs"
import chalk from "chalk"
import getStream from "get-stream"
import date from "date-and-time"
import { cli } from '../../../cli.js'
import { isExistingPath } from '../../../utils/isExistingPath.js'
import { cli } from "../../../cli.js"
import { isExistingPath } from "../../../utils/isExistingPath.js"
const input = ['generate', 'solution']
const githubUser = 'theoludwig'
const challenge = 'hello-world'
const language = 'c'
const solution = 'new-solution'
const input = ["generate", "solution"]
const githubUser = "theoludwig"
const challenge = "hello-world"
const language = "c"
const solution = "new-solution"
const inputChallenge = `--challenge=${challenge}`
const inputGitHubUser = `--github-user=${githubUser}`
const inputLanguage = `--language=${language}`
const inputSolution = `--solution=${solution}`
await test('programming-challenges generate solution', async (t) => {
await test("programming-challenges generate solution", async (t) => {
t.beforeEach(() => {
fsMock(
{
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true }),
},
{ createCwd: false }
{ createCwd: false },
)
})
@ -38,35 +38,35 @@ await test('programming-challenges generate solution', async (t) => {
sinon.restore()
})
await t.test('succeeds and generate the new solution', async () => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
const dateString = date.format(new Date(), 'D MMMM Y', true)
await t.test("succeeds and generate the new solution", async () => {
sinon.stub(console, "log").value(() => {})
const consoleLogSpy = sinon.spy(console, "log")
const dateString = date.format(new Date(), "D MMMM Y", true)
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, inputChallenge, inputGitHubUser, inputLanguage, inputSolution],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 0)
const solutionPath = path.join(
process.cwd(),
'challenges',
"challenges",
challenge,
'solutions',
"solutions",
language,
solution
solution,
)
const readmePath = path.join(solutionPath, 'README.md')
const readmePath = path.join(solutionPath, "README.md")
const readmeContent = await fs.promises.readFile(readmePath, {
encoding: 'utf-8'
encoding: "utf-8",
})
const successMessage = `${chalk.bold.green(
'Success:'
"Success:",
)} created the new solution at ${solutionPath}.`
assert.strictEqual(consoleLogSpy.calledWith(successMessage), true)
assert.strictEqual(await isExistingPath(solutionPath), true)
@ -75,15 +75,15 @@ await test('programming-challenges generate solution', async (t) => {
`# ${challenge}/${language}/${solution}
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
`
`,
)
})
await t.test("fails with challenges 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 invalidChallenge = 'aaa-jest-challenge'
const invalidChallenge = "aaa-jest-challenge"
const inputInvalidChallenge = `--challenge=${invalidChallenge}`
const exitCode = await cli.run(
[
@ -91,30 +91,30 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
inputInvalidChallenge,
inputGitHubUser,
inputLanguage,
inputSolution
inputSolution,
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
` The challenge doesn't exist yet: ${invalidChallenge}.`
chalk.bold.red("Error:") +
` The challenge doesn't exist yet: ${invalidChallenge}.`,
),
true
true,
)
})
await t.test('fails with solution that already exist', async () => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
await t.test("fails with solution that already exist", async () => {
sinon.stub(console, "error").value(() => {})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const invalidSolution = 'function'
const invalidSolution = "function"
const inputInvalidSolution = `--solution=${invalidSolution}`
const exitCode = await cli.run(
[
@ -122,30 +122,30 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
inputChallenge,
inputGitHubUser,
inputLanguage,
inputInvalidSolution
inputInvalidSolution,
],
{
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 already exists: ${invalidSolution}.`
chalk.bold.red("Error:") +
` The solution already exists: ${invalidSolution}.`,
),
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(
[
@ -153,32 +153,32 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
inputChallenge,
inputGitHubUser,
inputSolution,
inputInvalidLanguage
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 () => {
await t.test("fails without options", async () => {
const stream = new PassThrough()
const promise = getStream(stream)
const exitCode = await cli.run(input, {
stdin: process.stdin,
stdout: stream,
stderr: stream
stderr: stream,
})
stream.end()
assert.strictEqual(exitCode, 1)

View File

@ -1,43 +1,43 @@
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 { Challenge } from '../../services/Challenge.js'
import { Challenge } from "../../services/Challenge.js"
export class GenerateChallengeCommand extends Command {
public static override paths = [['generate', 'challenge']]
public static override paths = [["generate", "challenge"]]
public static override usage = {
description: 'Create the basic files needed for a new challenge.'
description: "Create the basic files needed for a new challenge.",
}
public challenge = Option.String('--challenge', {
description: 'The new challenge name to generate.',
public challenge = Option.String("--challenge", {
description: "The new challenge name to generate.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public githubUser = Option.String('--github-user', {
description: 'Your GitHub user.',
public githubUser = Option.String("--github-user", {
description: "Your GitHub user.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public async execute(): Promise<number> {
try {
const challenge = await Challenge.generate({
name: this.challenge,
githubUser: this.githubUser
githubUser: this.githubUser,
})
console.log(
`${chalk.bold.green('Success:')} created the new challenge at ${
`${chalk.bold.green("Success:")} created the new challenge at ${
challenge.path
}.`
}.`,
)
return 0
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
}
return 1
}

View File

@ -1,38 +1,38 @@
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 { Solution } from "../../services/Solution.js"
export class GenerateSolutionCommand extends Command {
public static override paths = [['generate', 'solution']]
public static override paths = [["generate", "solution"]]
public static override usage = {
description: 'Create the basic files needed for a new solution.'
description: "Create the basic files needed for a new solution.",
}
public challenge = Option.String('--challenge', {
description: 'The challenge name you want to generate a solution for.',
public challenge = Option.String("--challenge", {
description: "The challenge name you want to generate a solution for.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public githubUser = Option.String('--github-user', {
description: 'Your GitHub user.',
public githubUser = Option.String("--github-user", {
description: "Your GitHub user.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public solutionName = Option.String('--solution', {
description: 'The new solution name to generate.',
public solutionName = Option.String("--solution", {
description: "The new solution name to generate.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public programmingLanguage = Option.String('--language', {
description: 'The programming language to use to solve the challenge.',
public programmingLanguage = Option.String("--language", {
description: "The programming language to use to solve the challenge.",
required: true,
validator: typanion.isString()
validator: typanion.isString(),
})
public async execute(): Promise<number> {
@ -41,17 +41,17 @@ export class GenerateSolutionCommand extends Command {
name: this.solutionName,
githubUser: this.githubUser,
challengeName: this.challenge,
programmingLanguageName: this.programmingLanguage
programmingLanguageName: this.programmingLanguage,
})
console.log(
`${chalk.bold.green('Success:')} created the new solution at ${
`${chalk.bold.green("Success:")} created the new solution at ${
solution.path
}.`
}.`,
)
return 0
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
}
return 1
}

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

View File

@ -1,40 +1,40 @@
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 { template } from '../../services/Template.js'
import { Challenge } from '../../services/Challenge.js'
import { template } from "../../services/Template.js"
import { Challenge } from "../../services/Challenge.js"
export class SearchCommand extends Command {
public static override paths = [['search']]
public static override paths = [["search"]]
public static override usage = {
description: 'Search challenges in the programming language specified.'
description: "Search challenges in the programming language specified.",
}
public solved = Option.Boolean('--solved', false, {
public solved = Option.Boolean("--solved", false, {
description:
'Challenges which have already been solved (at least with one solution).'
"Challenges which have already been solved (at least with one solution).",
})
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 async execute(): Promise<number> {
try {
await template.verifySupportedProgrammingLanguage(
this.programmingLanguage
this.programmingLanguage,
)
const challenges = await Challenge.getChallenges()
const challengesResult: Challenge[] = []
for (const challenge of challenges) {
const solutionsPath = path.join(challenge.path, 'solutions')
const solutionsPath = path.join(challenge.path, "solutions")
const solutions = await fs.promises.readdir(solutionsPath)
if (
(!this.solved && !solutions.includes(this.programmingLanguage)) ||
@ -44,8 +44,8 @@ export class SearchCommand extends Command {
}
}
const message = this.solved
? 'Challenges already solved'
: 'Challenges not yet solved'
? "Challenges already solved"
: "Challenges not yet solved"
console.log(`${message} in ${chalk.bold(this.programmingLanguage)}:`)
for (const challenge of challengesResult) {
console.log(` - ${challenge.name}`)
@ -53,7 +53,7 @@ export class SearchCommand extends Command {
return 0
} catch (error) {
if (error instanceof Error) {
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
}
return 1
}