2021-12-06 16:35:45 +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 tap from 'tap'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
import fsMock from 'mock-fs'
|
2021-12-06 16:35:45 +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-12-06 16:35:45 +01:00
|
|
|
|
|
|
|
const input = ['generate', 'solution']
|
2023-07-02 17:28:54 +02:00
|
|
|
const githubUser = 'theoludwig'
|
2021-12-06 16:35:45 +01:00
|
|
|
const challenge = 'hello-world'
|
|
|
|
const language = 'c'
|
|
|
|
const solution = 'new-solution'
|
|
|
|
const inputChallenge = `--challenge=${challenge}`
|
|
|
|
const inputGitHubUser = `--github-user=${githubUser}`
|
|
|
|
const inputLanguage = `--language=${language}`
|
|
|
|
const inputSolution = `--solution=${solution}`
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await tap.test('programming-challenges generate solution', async (t) => {
|
|
|
|
t.beforeEach(() => {
|
2021-12-06 16:35:45 +01:00
|
|
|
fsMock(
|
|
|
|
{
|
|
|
|
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
|
|
|
|
},
|
|
|
|
{ createCwd: false }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test('succeeds and generate the new solution', async (t) => {
|
|
|
|
sinon.stub(console, 'log').value(() => {})
|
|
|
|
const consoleLogSpy = sinon.spy(console, 'log')
|
2021-12-06 16:35:45 +01:00
|
|
|
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
|
|
|
const stream = new PassThrough()
|
|
|
|
const exitCode = await cli.run(
|
|
|
|
[...input, inputChallenge, inputGitHubUser, inputLanguage, inputSolution],
|
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(exitCode, 0)
|
|
|
|
const solutionPath = path.join(
|
|
|
|
process.cwd(),
|
|
|
|
'challenges',
|
|
|
|
challenge,
|
|
|
|
'solutions',
|
|
|
|
language,
|
|
|
|
solution
|
|
|
|
)
|
2021-12-06 16:35:45 +01:00
|
|
|
const readmePath = path.join(solutionPath, '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 solution at ${solutionPath}.`
|
|
|
|
t.equal(consoleLogSpy.calledWith(successMessage), true)
|
|
|
|
t.equal(await isExistingPath(solutionPath), true)
|
|
|
|
t.equal(
|
|
|
|
readmeContent,
|
|
|
|
`# ${challenge}/${language}/${solution}
|
2021-12-06 16:35:45 +01:00
|
|
|
|
|
|
|
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
2022-04-23 18:41:14 +02:00
|
|
|
`
|
|
|
|
)
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test("fails with challenges that doesn't exist", async (t) => {
|
|
|
|
sinon.stub(console, 'error').value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, 'error')
|
2021-12-06 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const invalidChallenge = 'aaa-jest-challenge'
|
|
|
|
const inputInvalidChallenge = `--challenge=${invalidChallenge}`
|
|
|
|
const exitCode = await cli.run(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputInvalidChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputLanguage,
|
|
|
|
inputSolution
|
|
|
|
],
|
2021-12-06 16:35:45 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(exitCode, 1)
|
|
|
|
t.equal(
|
|
|
|
consoleErrorSpy.calledWith(
|
|
|
|
chalk.bold.red('Error:') +
|
|
|
|
` The challenge doesn't exist yet: ${invalidChallenge}.`
|
|
|
|
),
|
|
|
|
true
|
2021-12-06 16:35:45 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test('fails with solution that already exist', async (t) => {
|
|
|
|
sinon.stub(console, 'error').value(() => {})
|
|
|
|
const consoleErrorSpy = sinon.spy(console, 'error')
|
2021-12-06 16:35:45 +01:00
|
|
|
const stream = new PassThrough()
|
|
|
|
const invalidSolution = 'function'
|
|
|
|
const inputInvalidSolution = `--solution=${invalidSolution}`
|
|
|
|
const exitCode = await cli.run(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputLanguage,
|
|
|
|
inputInvalidSolution
|
|
|
|
],
|
2021-12-06 16:35:45 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(exitCode, 1)
|
|
|
|
t.equal(
|
|
|
|
consoleErrorSpy.calledWith(
|
|
|
|
chalk.bold.red('Error:') +
|
|
|
|
` The solution already exists: ${invalidSolution}.`
|
|
|
|
),
|
|
|
|
true
|
2021-12-06 16:35:45 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test('fails with invalid language', async (t) => {
|
|
|
|
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'
|
|
|
|
const inputInvalidLanguage = `--language=${invalidLanguage}`
|
|
|
|
const exitCode = await cli.run(
|
2022-04-23 18:41:14 +02:00
|
|
|
[
|
|
|
|
...input,
|
|
|
|
inputChallenge,
|
|
|
|
inputGitHubUser,
|
|
|
|
inputSolution,
|
|
|
|
inputInvalidLanguage
|
|
|
|
],
|
2021-12-06 17:18:14 +01:00
|
|
|
{
|
|
|
|
stdin: process.stdin,
|
|
|
|
stdout: stream,
|
|
|
|
stderr: stream
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stream.end()
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(exitCode, 1)
|
|
|
|
t.equal(
|
|
|
|
consoleErrorSpy.calledWith(
|
|
|
|
chalk.bold.red('Error:') +
|
|
|
|
' This programming language is not supported yet.'
|
|
|
|
),
|
|
|
|
true
|
2021-12-06 17:18:14 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +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,
|
|
|
|
stderr: stream
|
|
|
|
})
|
|
|
|
stream.end()
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(exitCode, 1)
|
2021-12-06 16:35:45 +01:00
|
|
|
const output = await promise
|
2022-04-23 18:41:14 +02:00
|
|
|
t.match(output, 'Unknown Syntax Error')
|
2021-12-06 16:35:45 +01:00
|
|
|
})
|
|
|
|
})
|