2023-07-20 22:00:11 +02:00
|
|
|
import test from 'node:test'
|
|
|
|
import assert from 'node:assert/strict'
|
2021-11-30 21:42:43 +01:00
|
|
|
import { PassThrough } from 'node:stream'
|
|
|
|
import path from 'node:path'
|
|
|
|
import fs from 'node:fs'
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
import sinon from 'sinon'
|
|
|
|
import fsMock from 'mock-fs'
|
2021-11-30 21:42:43 +01:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import getStream from 'get-stream'
|
|
|
|
import date from 'date-and-time'
|
|
|
|
|
2022-02-19 18:30:29 +01:00
|
|
|
import { cli } from '../../../cli.js'
|
|
|
|
import { isExistingPath } from '../../../utils/isExistingPath.js'
|
2021-11-30 21:42:43 +01:00
|
|
|
|
|
|
|
const input = ['generate', 'challenge']
|
2023-07-02 17:28:54 +02:00
|
|
|
const githubUser = 'theoludwig'
|
2021-12-06 16:35:45 +01:00
|
|
|
const challenge = 'aaaa-test-jest'
|
|
|
|
const inputChallenge = `--challenge=${challenge}`
|
2021-11-30 21:42:43 +01:00
|
|
|
const inputGitHubUser = `--github-user=${githubUser}`
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await test('programming-challenges generate challenge', async (t) => {
|
2022-04-23 18:41:14 +02:00
|
|
|
t.beforeEach(() => {
|
2021-11-30 21:42:43 +01:00
|
|
|
fsMock(
|
|
|
|
{
|
2023-07-02 17:28:54 +02:00
|
|
|
[process.cwd()]: fsMock.load(process.cwd(), {
|
|
|
|
recursive: true,
|
|
|
|
lazy: true
|
|
|
|
})
|
2021-11-30 21:42:43 +01:00
|
|
|
},
|
|
|
|
{ createCwd: false }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
t.afterEach(() => {
|
2021-11-30 21:42:43 +01:00
|
|
|
fsMock.restore()
|
2022-04-23 18:41:14 +02:00
|
|
|
sinon.restore()
|
2021-11-30 21:42:43 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('succeeds and generate the new challenge', async () => {
|
2022-04-23 18:41:14 +02:00
|
|
|
sinon.stub(console, 'log').value(() => {})
|
|
|
|
const consoleLogSpy = sinon.spy(console, 'log')
|
2021-11-30 21:42:43 +01:00
|
|
|
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
2021-12-06 16:35:45 +01:00
|
|
|
[...input, inputChallenge, inputGitHubUser],
|
2021-11-30 21:42:43 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 0)
|
2021-12-06 16:35:45 +01:00
|
|
|
const challengePath = path.join(process.cwd(), 'challenges', challenge)
|
2021-11-30 21:42:43 +01:00
|
|
|
const readmePath = path.join(challengePath, 'README.md')
|
2022-04-23 18:41:14 +02:00
|
|
|
const readmeContent = await fs.promises.readFile(readmePath, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
|
|
|
const successMessage = `${chalk.bold.green(
|
|
|
|
'Success:'
|
|
|
|
)} created the new challenge at ${challengePath}.`
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(consoleLogSpy.calledWith(successMessage), true)
|
|
|
|
assert.strictEqual(await isExistingPath(challengePath), true)
|
|
|
|
assert.strictEqual(
|
2022-04-23 18:41:14 +02:00
|
|
|
readmeContent,
|
|
|
|
`# ${challenge}
|
2021-11-30 21:42:43 +01:00
|
|
|
|
|
|
|
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
|
|
|
Description of the challenge...
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
See the \`test\` folder for examples of input/output.
|
2022-04-23 18:41:14 +02:00
|
|
|
`
|
|
|
|
)
|
2021-11-30 21:42:43 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('fails without options', async () => {
|
2021-11-30 21:42:43 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const promise = getStream(stream)
|
|
|
|
const exitCode = await cli.run(input, {
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
})
|
|
|
|
stream.end()
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(exitCode, 1)
|
2021-11-30 21:42:43 +01:00
|
|
|
const output = await promise
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.match(output, /Unknown Syntax Error/)
|
2021-11-30 21:42:43 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('fails with already existing challenge', async () => {
|
2022-04-23 18:41:14 +02:00
|
|
|
sinon.stub(console, 'error').value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, 'error')
|
2021-11-30 21:42:43 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, '--challenge=hello-world', inputGitHubUser],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
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('Error:')} The challenge already exists: hello-world.`
|
|
|
|
),
|
|
|
|
true
|
|
|
|
)
|
2021-11-30 21:42:43 +01:00
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('fails with invalid challenge name', async () => {
|
2022-04-23 18:41:14 +02:00
|
|
|
sinon.stub(console, 'error').value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, 'error')
|
2021-11-30 21:42:43 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, '--challenge=hEllO-world', inputGitHubUser],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
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('Error:')} Invalid challenge name.`
|
|
|
|
),
|
|
|
|
true
|
2021-11-30 21:42:43 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|