From 249bb3a36707542dbbe803f11e648f1630fe79f6 Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 6 Dec 2021 16:35:45 +0100 Subject: [PATCH] test(cli): add `commands/generate/solution` --- .../generate/__test__/challenge.test.ts | 10 +- .../generate/__test__/solution.test.ts | 117 ++++++++++++++++++ cli/services/Solution.ts | 4 +- 3 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 cli/commands/generate/__test__/solution.test.ts diff --git a/cli/commands/generate/__test__/challenge.test.ts b/cli/commands/generate/__test__/challenge.test.ts index 654b5d2..6252577 100644 --- a/cli/commands/generate/__test__/challenge.test.ts +++ b/cli/commands/generate/__test__/challenge.test.ts @@ -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}. diff --git a/cli/commands/generate/__test__/solution.test.ts b/cli/commands/generate/__test__/solution.test.ts new file mode 100644 index 0000000..3ff75c1 --- /dev/null +++ b/cli/commands/generate/__test__/solution.test.ts @@ -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') + }) +}) diff --git a/cli/services/Solution.ts b/cli/services/Solution.ts index 53eecad..6f3df69 100644 --- a/cli/services/Solution.ts +++ b/cli/services/Solution.ts @@ -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,