2023-10-23 23:16:24 +02:00
|
|
|
import test from "node:test"
|
|
|
|
import assert from "node:assert/strict"
|
|
|
|
import { PassThrough } from "node:stream"
|
2021-12-06 17:18:14 +01:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import sinon from "sinon"
|
|
|
|
import chalk from "chalk"
|
2021-12-06 17:18:14 +01:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { cli } from "../../../cli.js"
|
|
|
|
import { SolutionTestsResult } from "../../../services/SolutionTestsResult.js"
|
2021-12-06 17:18:14 +01:00
|
|
|
|
2023-10-23 23:16:24 +02: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}`
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
await test("programming-challenges run test", async (t) => {
|
2022-04-23 18:41:14 +02:00
|
|
|
t.afterEach(() => {
|
|
|
|
sinon.restore()
|
2021-12-06 17:18:14 +01:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02: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,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 0)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
consoleLogSpy.calledWith(
|
2023-10-23 23:16:24 +02:00
|
|
|
`${chalk.bold("Name:")} ${challenge}/${language}/${solution}\n`,
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2022-04-23 18:41:14 +02:00
|
|
|
)
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
consoleLogSpy.calledWith(
|
2023-10-23 23:16:24 +02:00
|
|
|
`${chalk.bold("Tests:")} ${chalk.bold.green("3 passed")}, 3 total`,
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2022-04-23 18:41:14 +02:00
|
|
|
)
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
consoleLogSpy.calledWith(SolutionTestsResult.SUCCESS_MESSAGE),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2023-07-20 22:00:11 +02:00
|
|
|
)
|
2021-12-06 17:18:14 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test("fails with solution that doesn't exist", async () => {
|
2023-10-23 23:16:24 +02:00
|
|
|
sinon.stub(console, "error").value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, "error")
|
2021-12-06 17:18:14 +01:00
|
|
|
const stream = new PassThrough()
|
2023-10-23 23:16:24 +02:00
|
|
|
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,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 1)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
consoleErrorSpy.calledWith(
|
2023-10-23 23:16:24 +02:00
|
|
|
chalk.bold.red("Error:") + " The solution was not found.",
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02: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()
|
2023-10-23 23:16:24 +02:00
|
|
|
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,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 1)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
consoleErrorSpy.calledWith(
|
2023-10-23 23:16:24 +02:00
|
|
|
chalk.bold.red("Error:") +
|
|
|
|
" This programming language is not supported yet.",
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02: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,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
2021-12-06 17:18:14 +01:00
|
|
|
})
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 1)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
consoleErrorSpy.calledWith(
|
|
|
|
`${chalk.bold.red(
|
2023-10-23 23:16:24 +02:00
|
|
|
"Error:",
|
|
|
|
)} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`,
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|