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:
@ -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,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -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,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user