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
}