mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat: usage of ESM modules imports (instead of CommonJS) (#14)
This commit is contained in:
@ -2,9 +2,11 @@ import { PassThrough } from 'node:stream'
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import fsMock from 'mock-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.js'
|
||||
@ -16,8 +18,8 @@ const challenge = 'aaaa-test-jest'
|
||||
const inputChallenge = `--challenge=${challenge}`
|
||||
const inputGitHubUser = `--github-user=${githubUser}`
|
||||
|
||||
describe('programming-challenges generate challenge', () => {
|
||||
beforeEach(() => {
|
||||
await tap.test('programming-challenges generate challenge', async (t) => {
|
||||
t.beforeEach(() => {
|
||||
fsMock(
|
||||
{
|
||||
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
|
||||
@ -26,13 +28,14 @@ describe('programming-challenges generate challenge', () => {
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
t.afterEach(() => {
|
||||
fsMock.restore()
|
||||
jest.clearAllMocks()
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('succeeds and generate the new challenge', async () => {
|
||||
console.log = jest.fn()
|
||||
await t.test('succeeds and generate the new challenge', async (t) => {
|
||||
sinon.stub(console, 'log').value(() => {})
|
||||
const consoleLogSpy = sinon.spy(console, 'log')
|
||||
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
@ -44,14 +47,20 @@ describe('programming-challenges generate challenge', () => {
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(0)
|
||||
t.equal(exitCode, 0)
|
||||
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(`# ${challenge}
|
||||
const readmeContent = await fs.promises.readFile(readmePath, {
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
const successMessage = `${chalk.bold.green(
|
||||
'Success:'
|
||||
)} created the new challenge at ${challengePath}.`
|
||||
t.equal(consoleLogSpy.calledWith(successMessage), true)
|
||||
t.equal(await isExistingPath(challengePath), true)
|
||||
t.equal(
|
||||
readmeContent,
|
||||
`# ${challenge}
|
||||
|
||||
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
|
||||
@ -62,10 +71,11 @@ Description of the challenge...
|
||||
## Examples
|
||||
|
||||
See the \`test\` folder for examples of input/output.
|
||||
`)
|
||||
`
|
||||
)
|
||||
})
|
||||
|
||||
it('fails without options', async () => {
|
||||
await t.test('fails without options', async (t) => {
|
||||
const stream = new PassThrough()
|
||||
const promise = getStream(stream)
|
||||
const exitCode = await cli.run(input, {
|
||||
@ -74,13 +84,14 @@ See the \`test\` folder for examples of input/output.
|
||||
stderr: stream
|
||||
})
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
t.equal(exitCode, 1)
|
||||
const output = await promise
|
||||
expect(output).toContain('Unknown Syntax Error')
|
||||
t.match(output, 'Unknown Syntax Error')
|
||||
})
|
||||
|
||||
it('fails with already existing challenge', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails with already existing challenge', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
[...input, '--challenge=hello-world', inputGitHubUser],
|
||||
@ -90,15 +101,19 @@ See the \`test\` folder for examples of input/output.
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
`${chalk.bold.red('Error:')} The challenge already exists: hello-world.`
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
`${chalk.bold.red('Error:')} The challenge already exists: hello-world.`
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with invalid challenge name', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails with invalid challenge name', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
[...input, '--challenge=hEllO-world', inputGitHubUser],
|
||||
@ -109,9 +124,12 @@ See the \`test\` folder for examples of input/output.
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
`${chalk.bold.red('Error:')} Invalid challenge name.`
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
`${chalk.bold.red('Error:')} Invalid challenge name.`
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -2,9 +2,11 @@ import { PassThrough } from 'node:stream'
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import fsMock from 'mock-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.js'
|
||||
@ -20,8 +22,8 @@ const inputGitHubUser = `--github-user=${githubUser}`
|
||||
const inputLanguage = `--language=${language}`
|
||||
const inputSolution = `--solution=${solution}`
|
||||
|
||||
describe('programming-challenges generate solution', () => {
|
||||
beforeEach(() => {
|
||||
await tap.test('programming-challenges generate solution', async (t) => {
|
||||
t.beforeEach(() => {
|
||||
fsMock(
|
||||
{
|
||||
[process.cwd()]: fsMock.load(process.cwd(), { recursive: true })
|
||||
@ -30,13 +32,14 @@ describe('programming-challenges generate solution', () => {
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
t.afterEach(() => {
|
||||
fsMock.restore()
|
||||
jest.clearAllMocks()
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('succeeds and generate the new solution', async () => {
|
||||
console.log = jest.fn()
|
||||
await t.test('succeeds and generate the new solution', async (t) => {
|
||||
sinon.stub(console, 'log').value(() => {})
|
||||
const consoleLogSpy = sinon.spy(console, 'log')
|
||||
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
@ -48,26 +51,47 @@ describe('programming-challenges generate solution', () => {
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(0)
|
||||
const solutionPath = path.join(process.cwd(), 'challenges', challenge, 'solutions', language, solution)
|
||||
t.equal(exitCode, 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}
|
||||
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}
|
||||
|
||||
Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
`)
|
||||
`
|
||||
)
|
||||
})
|
||||
|
||||
it("fails with challenges that doesn't exist", async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test("fails with challenges that doesn't exist", async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const invalidChallenge = 'aaa-jest-challenge'
|
||||
const inputInvalidChallenge = `--challenge=${invalidChallenge}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputInvalidChallenge, inputGitHubUser, inputLanguage, inputSolution],
|
||||
[
|
||||
...input,
|
||||
inputInvalidChallenge,
|
||||
inputGitHubUser,
|
||||
inputLanguage,
|
||||
inputSolution
|
||||
],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
@ -75,19 +99,30 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ` The challenge doesn't exist yet: ${invalidChallenge}.`
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
chalk.bold.red('Error:') +
|
||||
` The challenge doesn't exist yet: ${invalidChallenge}.`
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with solution that already exist', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails with solution that already exist', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const invalidSolution = 'function'
|
||||
const inputInvalidSolution = `--solution=${invalidSolution}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputGitHubUser, inputLanguage, inputInvalidSolution],
|
||||
[
|
||||
...input,
|
||||
inputChallenge,
|
||||
inputGitHubUser,
|
||||
inputLanguage,
|
||||
inputInvalidSolution
|
||||
],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
@ -95,19 +130,30 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ` The solution already exists: ${invalidSolution}.`
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
chalk.bold.red('Error:') +
|
||||
` The solution already exists: ${invalidSolution}.`
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with invalid language', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails with invalid language', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const invalidLanguage = 'invalid'
|
||||
const inputInvalidLanguage = `--language=${invalidLanguage}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputGitHubUser, inputSolution, inputInvalidLanguage],
|
||||
[
|
||||
...input,
|
||||
inputChallenge,
|
||||
inputGitHubUser,
|
||||
inputSolution,
|
||||
inputInvalidLanguage
|
||||
],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
@ -115,13 +161,17 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ' This programming language is not supported yet.'
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
chalk.bold.red('Error:') +
|
||||
' This programming language is not supported yet.'
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails without options', async () => {
|
||||
await t.test('fails without options', async () => {
|
||||
const stream = new PassThrough()
|
||||
const promise = getStream(stream)
|
||||
const exitCode = await cli.run(input, {
|
||||
@ -130,8 +180,8 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
|
||||
stderr: stream
|
||||
})
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
t.equal(exitCode, 1)
|
||||
const output = await promise
|
||||
expect(output).toContain('Unknown Syntax Error')
|
||||
t.match(output, 'Unknown Syntax Error')
|
||||
})
|
||||
})
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { PassThrough } from 'node:stream'
|
||||
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import chalk from 'chalk'
|
||||
|
||||
import { cli } from '../../../cli.js'
|
||||
@ -13,13 +15,14 @@ const inputChallenge = `--challenge=${challenge}`
|
||||
const inputLanguage = `--language=${language}`
|
||||
const inputSolution = `--solution=${solution}`
|
||||
|
||||
describe('programming-challenges run test', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks()
|
||||
await tap.test('programming-challenges run test', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('succeeds', async () => {
|
||||
console.log = jest.fn()
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(console, 'log').value(() => {})
|
||||
const consoleLogSpy = sinon.spy(console, 'log')
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputSolution, inputLanguage],
|
||||
@ -30,14 +33,25 @@ describe('programming-challenges run test', () => {
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(0)
|
||||
expect(console.log).toHaveBeenNthCalledWith(2, `${chalk.bold('Name:')} ${challenge}/${language}/${solution}\n`)
|
||||
expect(console.log).toHaveBeenNthCalledWith(4, `${chalk.bold('Tests:')} ${chalk.bold.green('3 passed')}, 3 total`)
|
||||
expect(console.log).toHaveBeenNthCalledWith(6, Test.SUCCESS_MESSAGE)
|
||||
t.equal(exitCode, 0)
|
||||
t.equal(
|
||||
consoleLogSpy.calledWith(
|
||||
`${chalk.bold('Name:')} ${challenge}/${language}/${solution}\n`
|
||||
),
|
||||
true
|
||||
)
|
||||
t.equal(
|
||||
consoleLogSpy.calledWith(
|
||||
`${chalk.bold('Tests:')} ${chalk.bold.green('3 passed')}, 3 total`
|
||||
),
|
||||
true
|
||||
)
|
||||
t.equal(consoleLogSpy.calledWith(Test.SUCCESS_MESSAGE), true)
|
||||
})
|
||||
|
||||
it("fails with solution that doesn't exist", async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test("fails with solution that doesn't exist", async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const invalidSolution = 'invalid'
|
||||
const inputInvalidSolution = `--solution=${invalidSolution}`
|
||||
@ -50,14 +64,18 @@ describe('programming-challenges run test', () => {
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ' The solution was not found.'
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
chalk.bold.red('Error:') + ' The solution was not found.'
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with invalid language', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails with invalid language', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const invalidLanguage = 'invalid'
|
||||
const inputInvalidLanguage = `--language=${invalidLanguage}`
|
||||
@ -70,14 +88,19 @@ describe('programming-challenges run test', () => {
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ' This programming language is not supported yet.'
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
chalk.bold.red('Error:') +
|
||||
' This programming language is not supported yet.'
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('fails without options', async () => {
|
||||
console.error = jest.fn()
|
||||
await t.test('fails without options', async (t) => {
|
||||
sinon.stub(console, 'error').value(() => {})
|
||||
const consoleErrorSpy = sinon.spy(console, 'error')
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(input, {
|
||||
stdin: process.stdin,
|
||||
@ -85,9 +108,14 @@ describe('programming-challenges run test', () => {
|
||||
stderr: stream
|
||||
})
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
`${chalk.bold.red('Error:')} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`
|
||||
t.equal(exitCode, 1)
|
||||
t.equal(
|
||||
consoleErrorSpy.calledWith(
|
||||
`${chalk.bold.red(
|
||||
'Error:'
|
||||
)} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`
|
||||
),
|
||||
true
|
||||
)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user