From 2a70daaf994dc641f6dc58dc6ff331d2e33559a1 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 27 Oct 2023 15:53:12 +0100 Subject: [PATCH] feature/71-basic-cli (#104) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Install Commander package - Add basic CLI interface to the package #71 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - I have added unit tests for the CLI as it is so far, no commands have been added yet # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that provde my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/104 Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- package.json | 2 ++ src/cli.ts | 9 ++++++++ tests/cli.test.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++ tests/index.test.ts | 2 +- yarn.lock | 14 +++++------- 5 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 src/cli.ts create mode 100644 tests/cli.test.ts diff --git a/package.json b/package.json index 0eb81d3..e1840cd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "placeholder" ], "dependencies": { + "commander": "^11.1.0", "glob-parent": "^6.0.0", "got-cjs": "^12.5.4", "linqts": "^1.14.4" @@ -23,6 +24,7 @@ "scripts": { "build": "tsc", "start": "ts-node app.ts", + "cli": "ts-node src/cli.ts", "test": "jest", "lint": "eslint .", "release": "np --no-publish" diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..b1311f5 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,9 @@ +import { Command } from "commander"; +const program = new Command(); + +program + .name('random-bunny') + .description('Get a random image url from a subreddit of your choosing') + .version('2.2'); + +program.parse(); \ No newline at end of file diff --git a/tests/cli.test.ts b/tests/cli.test.ts new file mode 100644 index 0000000..efa1b8d --- /dev/null +++ b/tests/cli.test.ts @@ -0,0 +1,54 @@ +import { exec } from "child_process"; +import path from "path"; + +describe('version', () => { + test('GIVEN -V flag is supplied, EXPECT version returned', async () => { + const result = await cli(['-V'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout).toBe('2.2\n'); + }); + + test('GIVEN --version is supplied, EXPECT version returned', async () => { + const result = await cli(['--version'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout).toBe('2.2\n'); + }); +}); + +describe('help', () => { + test('GIVEN -h is supplied, EXPECT help returned', async () => { + const result = await cli(['-h'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); + }); + + test('GIVEN --help is supplied, EXPECT help returned', async () => { + const result = await cli(['--help'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); + }); +}) + +function cli(args: string[], cwd: string): Promise { + return new Promise(resolve => { + exec(`node ${path.resolve('./dist/cli.js')} ${args.join(' ')}`, + { cwd }, + (error, stdout, stderr) => { resolve({ + code: error && error.code ? error.code : 0, + error, + stdout, + stderr }); + }); + }); +} + +interface cliResult { + code: number, + error: any, + stdout: string, + stderr: string, +} \ No newline at end of file diff --git a/tests/index.test.ts b/tests/index.test.ts index 2fb1a14..302fd1d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,7 +1,7 @@ import { ErrorCode } from "../src/constants/ErrorCode"; import ErrorMessages from "../src/constants/ErrorMessages"; import randomBunny from "../src/index"; -import fetch, { CancelableRequest } from "got-cjs"; +import fetch from "got-cjs"; jest.mock('got-cjs'); const fetchMock = jest.mocked(fetch); diff --git a/yarn.lock b/yarn.lock index a6cb3ff..086bf7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -839,14 +839,7 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.14.tgz#74a97a5573980802f32c8e47b663530ab3b6b7d1" integrity sha512-U3PUjAudAdJBeC2pgN8uTIKgxrb4nlDF3SF0++EldXQvQBGkpFZMSnwQiIoDU77tv45VgNkl/L4ouD+rEomujw== -"@types/node@*": - version "20.8.7" - resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.7.tgz#ad23827850843de973096edfc5abc9e922492a25" - integrity sha512-21TKHHh3eUHIi2MloeptJWALuCu5H7HQTdTrWIFReA8ad+aggoX+lRes3ex7/FtpC+sVUpFMQ+QTfYr74mruiQ== - dependencies: - undici-types "~5.25.1" - -"@types/node@^20.0.0": +"@types/node@*", "@types/node@^20.0.0": version "20.8.7" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.7.tgz#ad23827850843de973096edfc5abc9e922492a25" integrity sha512-21TKHHh3eUHIi2MloeptJWALuCu5H7HQTdTrWIFReA8ad+aggoX+lRes3ex7/FtpC+sVUpFMQ+QTfYr74mruiQ== @@ -1586,6 +1579,11 @@ commander-version@^1.1.0: "@bconnorwhite/module" "^2.0.2" commander "^6.1.0" +commander@^11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906" + integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ== + commander@^6.1.0: version "6.2.1" resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"