mirror of
				https://github.com/theoludwig/programming-challenges.git
				synced 2025-09-11 23:11:21 +02:00 
			
		
		
		
	test(cli): add commands/run/test
				
					
				
			This commit is contained in:
		
							
								
								
									
										5
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							| @@ -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
									
								
							
							
						
						
									
										2
									
								
								cli/__test__/setup.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000 | ||||
| jest.setTimeout(ONE_MINUTE_IN_MILLISECONDS) | ||||
| @@ -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) | ||||
|   | ||||
							
								
								
									
										93
									
								
								cli/commands/run/__test__/test.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								cli/commands/run/__test__/test.test.ts
									
									
									
									
									
										Normal 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\`).` | ||||
|     ) | ||||
|   }) | ||||
| }) | ||||
| @@ -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') | ||||
|   | ||||
| @@ -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() | ||||
|   | ||||
| @@ -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() | ||||
|   }) | ||||
| }) | ||||
|   | ||||
| @@ -6,5 +6,8 @@ | ||||
|     "<rootDir>/commands/run/test.ts", | ||||
|     "<rootDir>/services/Test.ts", | ||||
|     "<rootDir>/node_modules" | ||||
|   ], | ||||
|   "setupFiles": [ | ||||
|     "<rootDir>/__test__/setup.ts" | ||||
|   ] | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user