mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-10-29 22:17:23 +01:00
test(cli): add commands/generate/solution
This commit is contained in:
parent
ee6733c760
commit
249bb3a367
@ -12,8 +12,8 @@ import { isExistingPath } from '../../../utils/isExistingPath'
|
||||
|
||||
const input = ['generate', 'challenge']
|
||||
const githubUser = 'Divlo'
|
||||
const challengeName = 'aaaa-test-jest'
|
||||
const inputChallengeName = `--challenge=${challengeName}`
|
||||
const challenge = 'aaaa-test-jest'
|
||||
const inputChallenge = `--challenge=${challenge}`
|
||||
const inputGitHubUser = `--github-user=${githubUser}`
|
||||
|
||||
describe('programming-challenges generate challenge', () => {
|
||||
@ -36,7 +36,7 @@ describe('programming-challenges generate challenge', () => {
|
||||
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallengeName, inputGitHubUser],
|
||||
[...input, inputChallenge, inputGitHubUser],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
@ -45,13 +45,13 @@ describe('programming-challenges generate challenge', () => {
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(0)
|
||||
const challengePath = path.join(process.cwd(), 'challenges', challengeName)
|
||||
const challengePath = path.join(process.cwd(), 'challenges', challenge)
|
||||
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()
|
||||
expect(readmeContent).toMatch(`# ${challengeName}
|
||||
expect(readmeContent).toMatch(`# ${challenge}
|
||||
|
||||
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
|
||||
|
117
cli/commands/generate/__test__/solution.test.ts
Normal file
117
cli/commands/generate/__test__/solution.test.ts
Normal file
@ -0,0 +1,117 @@
|
||||
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'
|
||||
|
||||
import { cli } from '../../../cli'
|
||||
import { isExistingPath } from '../../../utils/isExistingPath'
|
||||
|
||||
const input = ['generate', 'solution']
|
||||
const githubUser = 'Divlo'
|
||||
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}`
|
||||
|
||||
describe('programming-challenges generate solution', () => {
|
||||
beforeEach(() => {
|
||||
fsMock(
|
||||
{
|
||||
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
|
||||
},
|
||||
{ createCwd: false }
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fsMock.restore()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('succeeds and generate the new solution', async () => {
|
||||
console.log = jest.fn()
|
||||
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()
|
||||
expect(exitCode).toEqual(0)
|
||||
const solutionPath = path.join(process.cwd(), 'challenges', challenge, 'solutions', language, solution)
|
||||
const readmePath = path.join(solutionPath, 'README.md')
|
||||
const readmeContent = await fs.promises.readFile(readmePath, { encoding: 'utf-8' })
|
||||
const successMessage = `${chalk.bold.green('Success:')} created the new solution at ${solutionPath}.`
|
||||
expect(console.log).toHaveBeenCalledWith(successMessage)
|
||||
expect(await isExistingPath(solutionPath)).toBeTruthy()
|
||||
expect(readmeContent).toMatch(`# ${challenge}/${language}/${solution}
|
||||
|
||||
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
`)
|
||||
})
|
||||
|
||||
it("fails with challenges that doesn't exist", async () => {
|
||||
console.error = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const invalidChallenge = 'aaa-jest-challenge'
|
||||
const inputInvalidChallenge = `--challenge=${invalidChallenge}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputInvalidChallenge, inputGitHubUser, inputLanguage, inputSolution],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ` The challenge doesn't exist yet: ${invalidChallenge}.`
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with solution that already exist', async () => {
|
||||
console.error = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const invalidSolution = 'function'
|
||||
const inputInvalidSolution = `--solution=${invalidSolution}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputGitHubUser, inputLanguage, inputInvalidSolution],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ` The solution already exists: ${invalidSolution}.`
|
||||
)
|
||||
})
|
||||
|
||||
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')
|
||||
})
|
||||
})
|
@ -67,7 +67,7 @@ export class Solution implements SolutionOptions {
|
||||
const { name, challengeName, programmingLanguageName, githubUser } = options
|
||||
const challenge = new Challenge({ name: challengeName })
|
||||
if (!(await isExistingPath(challenge.path))) {
|
||||
throw new Error(`The challenge doesn't exist yet: ${name}.`)
|
||||
throw new Error(`The challenge doesn't exist yet: ${challenge.name}.`)
|
||||
}
|
||||
const solution = new Solution({
|
||||
name,
|
||||
@ -75,7 +75,7 @@ export class Solution implements SolutionOptions {
|
||||
programmingLanguageName
|
||||
})
|
||||
if (await isExistingPath(solution.path)) {
|
||||
throw new Error('The solution already exists.')
|
||||
throw new Error(`The solution already exists: ${name}.`)
|
||||
}
|
||||
await template.solution({
|
||||
challengeName: challenge.name,
|
||||
|
Loading…
Reference in New Issue
Block a user