1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00

test(cli): add commands/run/test

This commit is contained in:
Divlo 2021-12-06 17:18:14 +01:00
parent 249bb3a367
commit cffaa9ec40
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
8 changed files with 128 additions and 12 deletions

View File

@ -48,6 +48,11 @@ jobs:
steps:
- uses: 'actions/checkout@v2'
- name: 'Use Docker'
uses: 'actions-hub/docker/cli@master'
env:
SKIP_LOGIN: true
- name: 'Use Node.js'
uses: 'actions/setup-node@v2.5.0'
with:

2
cli/__test__/setup.ts Normal file
View File

@ -0,0 +1,2 @@
const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000
jest.setTimeout(ONE_MINUTE_IN_MILLISECONDS)

View File

@ -101,6 +101,26 @@ Created by [@${githubUser}](https://github.com/${githubUser}) on ${dateString}.
)
})
it('fails with invalid language', async () => {
console.error = jest.fn()
const stream = new PassThrough()
const invalidLanguage = 'invalid'
const inputInvalidLanguage = `--language=${invalidLanguage}`
const exitCode = await cli.run(
[...input, inputChallenge, inputGitHubUser, inputSolution, inputInvalidLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
expect(exitCode).toEqual(1)
expect(console.error).toHaveBeenCalledWith(
chalk.bold.red('Error:') + ' This programming language is not supported yet.'
)
})
it('fails without options', async () => {
const stream = new PassThrough()
const promise = getStream(stream)

View File

@ -0,0 +1,93 @@
import { PassThrough } from 'node:stream'
import chalk from 'chalk'
import { cli } from '../../../cli'
import { Test } from '../../../services/Test'
const input = ['run', 'test']
const challenge = 'hello-world'
const language = 'c'
const solution = 'function'
const inputChallenge = `--challenge=${challenge}`
const inputLanguage = `--language=${language}`
const inputSolution = `--solution=${solution}`
describe('programming-challenges run test', () => {
afterEach(() => {
jest.clearAllMocks()
})
it('succeeds', async () => {
console.log = jest.fn()
const stream = new PassThrough()
const exitCode = await cli.run(
[...input, inputChallenge, inputSolution, inputLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
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)
})
it("fails with solution that doesn't exist", async () => {
console.error = jest.fn()
const stream = new PassThrough()
const invalidSolution = 'invalid'
const inputInvalidSolution = `--solution=${invalidSolution}`
const exitCode = await cli.run(
[...input, inputChallenge, inputInvalidSolution, inputLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
expect(exitCode).toEqual(1)
expect(console.error).toHaveBeenCalledWith(
chalk.bold.red('Error:') + ' The solution was not found.'
)
})
it('fails with invalid language', async () => {
console.error = jest.fn()
const stream = new PassThrough()
const invalidLanguage = 'invalid'
const inputInvalidLanguage = `--language=${invalidLanguage}`
const exitCode = await cli.run(
[...input, inputChallenge, inputSolution, inputInvalidLanguage],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
expect(exitCode).toEqual(1)
expect(console.error).toHaveBeenCalledWith(
chalk.bold.red('Error:') + ' This programming language is not supported yet.'
)
})
it('fails without options', async () => {
console.error = jest.fn()
const stream = new PassThrough()
const exitCode = await cli.run(input, {
stdin: process.stdin,
stdout: stream,
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\`).`
)
})
})

View File

@ -17,7 +17,7 @@ describe('utils/copyDirectory', () => {
'.npmignore': ''
},
'/destination': {}
})
}, { createCwd: false })
let destinationDirectoryContent = await fs.promises.readdir('/destination')
let sourceDirectoryContent = await fs.promises.readdir('/source')
@ -50,7 +50,7 @@ describe('utils/copyDirectory', () => {
'.npmignore': ''
},
'/destination': {}
})
}, { createCwd: false })
let destinationDirectoryContent = await fs.promises.readdir('/destination')
let sourceDirectoryContent = await fs.promises.readdir('/source')

View File

@ -13,19 +13,12 @@ describe('utils/createTemporaryEmptyFolder', () => {
fsMock.restore()
})
it('should create the temporary folder', async () => {
fsMock({})
expect(await isExistingPath(TEMPORARY_PATH)).toBeFalsy()
await createTemporaryEmptyFolder()
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
})
it('should remove and create again the temporary folder', async () => {
fsMock({
[TEMPORARY_PATH]: {
'file.txt': ''
}
})
}, { createCwd: false })
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(1)
await createTemporaryEmptyFolder()

View File

@ -10,14 +10,14 @@ describe('utils/isExistingFile', () => {
it('should return true if the file exists', async () => {
fsMock({
'/file.txt': ''
})
}, { createCwd: false })
expect(await isExistingPath('/file.txt')).toBeTruthy()
})
it("should return false if the file doesn't exists", async () => {
fsMock({
'/file.txt': ''
})
}, { createCwd: false })
expect(await isExistingPath('/randomfile.txt')).toBeFalsy()
})
})

View File

@ -6,5 +6,8 @@
"<rootDir>/commands/run/test.ts",
"<rootDir>/services/Test.ts",
"<rootDir>/node_modules"
],
"setupFiles": [
"<rootDir>/__test__/setup.ts"
]
}