1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/cli/commands/run/__test__/test.test.ts

126 lines
3.5 KiB
TypeScript
Raw Normal View History

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