1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00
programming-challenges/cli/commands/run/__test__/solution.test.ts

150 lines
3.8 KiB
TypeScript
Raw Normal View History

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