diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3817a4..ceb38cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/cli/__test__/setup.ts b/cli/__test__/setup.ts new file mode 100644 index 0000000..9831908 --- /dev/null +++ b/cli/__test__/setup.ts @@ -0,0 +1,2 @@ +const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000 +jest.setTimeout(ONE_MINUTE_IN_MILLISECONDS) diff --git a/cli/commands/generate/__test__/solution.test.ts b/cli/commands/generate/__test__/solution.test.ts index 3ff75c1..441af6b 100644 --- a/cli/commands/generate/__test__/solution.test.ts +++ b/cli/commands/generate/__test__/solution.test.ts @@ -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) diff --git a/cli/commands/run/__test__/test.test.ts b/cli/commands/run/__test__/test.test.ts new file mode 100644 index 0000000..e7035be --- /dev/null +++ b/cli/commands/run/__test__/test.test.ts @@ -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\`).` + ) + }) +}) diff --git a/cli/utils/__test__/copyDirectory.test.ts b/cli/utils/__test__/copyDirectory.test.ts index 89d37b4..e4713bc 100644 --- a/cli/utils/__test__/copyDirectory.test.ts +++ b/cli/utils/__test__/copyDirectory.test.ts @@ -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') diff --git a/cli/utils/__test__/createTemporaryEmptyFolder.test.ts b/cli/utils/__test__/createTemporaryEmptyFolder.test.ts index 638072d..e50d663 100644 --- a/cli/utils/__test__/createTemporaryEmptyFolder.test.ts +++ b/cli/utils/__test__/createTemporaryEmptyFolder.test.ts @@ -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() diff --git a/cli/utils/__test__/isExistingPath.test.ts b/cli/utils/__test__/isExistingPath.test.ts index c5f6e25..432c779 100644 --- a/cli/utils/__test__/isExistingPath.test.ts +++ b/cli/utils/__test__/isExistingPath.test.ts @@ -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() }) }) diff --git a/jest.config.json b/jest.config.json index bf6bd96..9c25447 100644 --- a/jest.config.json +++ b/jest.config.json @@ -6,5 +6,8 @@ "/commands/run/test.ts", "/services/Test.ts", "/node_modules" + ], + "setupFiles": [ + "/__test__/setup.ts" ] }