2023-10-23 23:16:24 +02:00
|
|
|
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"
|
2021-12-06 16:35:45 +01:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import sinon from "sinon"
|
|
|
|
import fsMock from "mock-fs"
|
|
|
|
import chalk from "chalk"
|
|
|
|
import getStream from "get-stream"
|
|
|
|
import date from "date-and-time"
|
2021-12-06 16:35:45 +01:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { cli } from "../../../cli.js"
|
|
|
|
import { isExistingPath } from "../../../utils/isExistingPath.js"
|
2021-12-06 16:35:45 +01:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
const input = ["generate", "solution"]
|
|
|
|
const githubUser = "theoludwig"
|
|
|
|
const challenge = "hello-world"
|
|
|
|
const language = "c"
|
|
|
|
const solution = "new-solution"
|
2021-12-06 16:35:45 +01:00
|
|
|
const inputChallenge = `--challenge=${challenge}`
|
|
|
|
const inputGitHubUser = `--github-user=${githubUser}`
|
|
|
|
const inputLanguage = `--language=${language}`
|
|
|
|
const inputSolution = `--solution=${solution}`
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
await test("programming-challenges generate solution", async (t) => {
|
2022-04-23 18:41:14 +02:00
|
|
|
t.beforeEach(() => {
|
2021-12-06 16:35:45 +01:00
|
|
|
fsMock(
|
|
|
|
{
|
2023-10-23 23:16:24 +02:00
|
|
|
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true }),
|
2021-12-06 16:35:45 +01:00
|
|
|
},
|
2023-10-23 23:16:24 +02:00
|
|
|
{ createCwd: false },
|
2021-12-06 16:35:45 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
t.afterEach(() => {
|
2021-12-06 16:35:45 +01:00
|
|
|
fsMock.restore()
|
2022-04-23 18:41:14 +02:00
|
|
|
sinon.restore()
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
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)
|
2021-12-06 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, inputChallenge, inputGitHubUser, inputLanguage, inputSolution],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 16:35:45 +01:00
|
|
|
)
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 0)
|
2022-04-23 18:41:14 +02:00
|
|
|
const solutionPath = path.join(
|
|
|
|
process.cwd(),
|
2023-10-23 23:16:24 +02:00
|
|
|
"challenges",
|
2022-04-23 18:41:14 +02:00
|
|
|
challenge,
|
2023-10-23 23:16:24 +02:00
|
|
|
"solutions",
|
2022-04-23 18:41:14 +02:00
|
|
|
language,
|
2023-10-23 23:16:24 +02:00
|
|
|
solution,
|
2022-04-23 18:41:14 +02:00
|
|
|
)
|
2023-10-23 23:16:24 +02:00
|
|
|
const readmePath = path.join(solutionPath, "README.md")
|
2022-04-23 18:41:14 +02:00
|
|
|
const readmeContent = await fs.promises.readFile(readmePath, {
|
2023-10-23 23:16:24 +02:00
|
|
|
encoding: "utf-8",
|
2022-04-23 18:41:14 +02:00
|
|
|
})
|
|
|
|
const successMessage = `${chalk.bold.green(
|
2023-10-23 23:16:24 +02:00
|
|
|
"Success:",
|
2022-04-23 18:41:14 +02:00
|
|
|
)} created the new solution at ${solutionPath}.`
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(consoleLogSpy.calledWith(successMessage), true)
|
|
|
|
assert.strictEqual(await isExistingPath(solutionPath), true)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
readmeContent,
|
|
|
|
`# ${challenge}/${language}/${solution}
|
2021-12-06 16:35:45 +01:00
|
|
|
|
|
|
|
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
2023-10-23 23:16:24 +02:00
|
|
|
`,
|
2022-04-23 18:41:14 +02:00
|
|
|
)
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test("fails with challenges 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 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
2023-10-23 23:16:24 +02:00
|
|
|
const invalidChallenge = "aaa-jest-challenge"
|
2021-12-06 16:35:45 +01:00
|
|
|
const inputInvalidChallenge = `--challenge=${invalidChallenge}`
|
|
|
|
const exitCode = await cli.run(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputInvalidChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputLanguage,
|
2023-10-23 23:16:24 +02:00
|
|
|
inputSolution,
|
2022-04-23 18:41:14 +02:00
|
|
|
],
|
2021-12-06 16:35:45 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 16:35:45 +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 challenge doesn't exist yet: ${invalidChallenge}.`,
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2021-12-06 16:35:45 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
await t.test("fails with solution that already exist", async () => {
|
|
|
|
sinon.stub(console, "error").value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, "error")
|
2021-12-06 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
2023-10-23 23:16:24 +02:00
|
|
|
const invalidSolution = "function"
|
2021-12-06 16:35:45 +01:00
|
|
|
const inputInvalidSolution = `--solution=${invalidSolution}`
|
|
|
|
const exitCode = await cli.run(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputLanguage,
|
2023-10-23 23:16:24 +02:00
|
|
|
inputInvalidSolution,
|
2022-04-23 18:41:14 +02:00
|
|
|
],
|
2021-12-06 16:35:45 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
|
|
|
},
|
2021-12-06 16:35:45 +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 already exists: ${invalidSolution}.`,
|
2022-04-23 18:41:14 +02:00
|
|
|
),
|
2023-10-23 23:16:24 +02:00
|
|
|
true,
|
2021-12-06 16:35:45 +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(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputSolution,
|
2023-10-23 23:16:24 +02:00
|
|
|
inputInvalidLanguage,
|
2022-04-23 18:41:14 +02:00
|
|
|
],
|
2021-12-06 17:18:14 +01:00
|
|
|
{
|
|
|
|
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 () => {
|
2021-12-06 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const promise = getStream(stream)
|
|
|
|
const exitCode = await cli.run(input, {
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
2023-10-23 23:16:24 +02:00
|
|
|
stderr: stream,
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 1)
|
2021-12-06 16:35:45 +01:00
|
|
|
const output = await promise
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.match(output, /Unknown Syntax Error/)
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
})
|