1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00
programming-challenges/cli/commands/run/__test__/solution.test.ts

150 lines
3.8 KiB
TypeScript
Raw Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
2022-08-30 15:48:07 +02:00
import { PassThrough } from 'node:stream'
import path from 'node:path'
import sinon from 'sinon'
import chalk from 'chalk'
import { cli } from '../../../cli.js'
const input = ['run', 'solution']
const challenge = 'hello-world'
const language = 'c'
const solution = 'function'
const inputPath = path.join(
process.cwd(),
'challenges',
challenge,
'test',
'1',
'input.txt'
)
const inputChallenge = `--challenge=${challenge}`
const inputLanguage = `--language=${language}`
const inputSolution = `--solution=${solution}`
const inputInputPath = `--input-path=${inputPath}`
await test('programming-challenges run solution', async (t) => {
2022-08-30 15:48:07 +02:00
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async () => {
2022-08-30 15:48:07 +02:00
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
const stream = new PassThrough()
const exitCode = await cli.run(
[
...input,
inputChallenge,
inputSolution,
inputLanguage,
inputInputPath,
'--output'
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
assert.strictEqual(exitCode, 0)
assert.strictEqual(consoleLogSpy.calledWith(`Hello, world!`), true)
2022-08-30 15:48:07 +02:00
})
await t.test("fails with solution that doesn't exist", async () => {
2022-08-30 15:48:07 +02:00
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
const stream = new PassThrough()
const invalidSolution = 'invalid'
const inputInvalidSolution = `--solution=${invalidSolution}`
const exitCode = await cli.run(
[
...input,
inputChallenge,
inputInvalidSolution,
inputLanguage,
inputInputPath
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
2022-08-30 15:48:07 +02:00
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') + ' The solution was not found.'
),
true
)
})
await t.test('fails with invalid language', async () => {
2022-08-30 15:48:07 +02:00
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,
inputSolution,
inputInvalidLanguage,
inputInputPath
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
2022-08-30 15:48:07 +02:00
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
' This programming language is not supported yet.'
),
true
)
})
await t.test('fails with invalid `input-path`', async () => {
2022-08-30 15:48:07 +02:00
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
const stream = new PassThrough()
const invalidInputPath = 'invalid'
const inputInvalidInputPath = `--input-path=${invalidInputPath}`
const inputPath = path.resolve(process.cwd(), invalidInputPath)
const exitCode = await cli.run(
[
...input,
inputChallenge,
inputSolution,
inputLanguage,
inputInvalidInputPath
],
{
stdin: process.stdin,
stdout: stream,
stderr: stream
}
)
stream.end()
assert.strictEqual(exitCode, 1)
assert.strictEqual(
2022-08-30 15:48:07 +02:00
consoleErrorSpy.calledWith(
chalk.bold.red('Error:') +
` The \`input-path\` doesn't exist: ${inputPath}.`
),
true
)
})
})