2021-11-30 21:42:43 +01:00
|
|
|
import { PassThrough } from 'node:stream'
|
|
|
|
import path from 'node:path'
|
|
|
|
import fs from 'node:fs'
|
|
|
|
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import getStream from 'get-stream'
|
|
|
|
import fsMock from 'mock-fs'
|
|
|
|
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']
|
|
|
|
const githubUser = 'Divlo'
|
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}`
|
|
|
|
|
|
|
|
describe('programming-challenges generate challenge', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fsMock(
|
|
|
|
{
|
|
|
|
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
|
|
|
|
},
|
|
|
|
{ createCwd: false }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
fsMock.restore()
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('succeeds and generate the new challenge', async () => {
|
|
|
|
console.log = jest.fn()
|
|
|
|
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()
|
|
|
|
expect(exitCode).toEqual(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')
|
|
|
|
const readmeContent = await fs.promises.readFile(readmePath, { encoding: 'utf-8' })
|
|
|
|
const successMessage = `${chalk.bold.green('Success:')} created the new challenge at ${challengePath}.`
|
|
|
|
expect(console.log).toHaveBeenCalledWith(successMessage)
|
|
|
|
expect(await isExistingPath(challengePath)).toBeTruthy()
|
2021-12-06 16:35:45 +01:00
|
|
|
expect(readmeContent).toMatch(`# ${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.
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails without options', async () => {
|
|
|
|
const stream = new PassThrough()
|
|
|
|
const promise = getStream(stream)
|
|
|
|
const exitCode = await cli.run(input, {
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
})
|
|
|
|
stream.end()
|
|
|
|
expect(exitCode).toEqual(1)
|
|
|
|
const output = await promise
|
|
|
|
expect(output).toContain('Unknown Syntax Error')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails with already existing challenge', async () => {
|
|
|
|
console.error = jest.fn()
|
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, '--challenge=hello-world', inputGitHubUser],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(console.error).toHaveBeenCalledWith(
|
|
|
|
`${chalk.bold.red('Error:')} The challenge already exists: hello-world.`
|
|
|
|
)
|
|
|
|
stream.end()
|
|
|
|
expect(exitCode).toEqual(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails with invalid challenge name', async () => {
|
|
|
|
console.error = jest.fn()
|
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, '--challenge=hEllO-world', inputGitHubUser],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
|
|
|
expect(exitCode).toEqual(1)
|
|
|
|
expect(console.error).toHaveBeenCalledWith(
|
|
|
|
`${chalk.bold.red('Error:')} Invalid challenge name.`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|