From b05b6bccefd3263fb01e1e7d4bad6d23c733ebdd Mon Sep 17 00:00:00 2001 From: Jamie White Date: Mon, 7 Sep 2020 19:04:36 +0200 Subject: [PATCH] Add test for readme-generation with custom help Co-authored-by: Chad Carbert --- src/_test-help.ts | 12 +++++++ test/fixtures/cli-with-custom-help/README.md | 19 ++++++++++++ .../cli-with-custom-help/package.json | 11 +++++++ .../src/commands/hello.ts | 31 +++++++++++++++++++ .../fixtures/cli-with-custom-help/src/help.ts | 12 +++++++ .../cli-with-custom-help/src/index.ts | 1 + .../cli-with-custom-help/tsconfig.json | 14 +++++++++ test/readme.test.ts | 18 +++++++++++ 8 files changed, 118 insertions(+) create mode 100644 src/_test-help.ts create mode 100644 test/fixtures/cli-with-custom-help/README.md create mode 100644 test/fixtures/cli-with-custom-help/package.json create mode 100644 test/fixtures/cli-with-custom-help/src/commands/hello.ts create mode 100644 test/fixtures/cli-with-custom-help/src/help.ts create mode 100644 test/fixtures/cli-with-custom-help/src/index.ts create mode 100644 test/fixtures/cli-with-custom-help/tsconfig.json diff --git a/src/_test-help.ts b/src/_test-help.ts new file mode 100644 index 00000000..8811eb13 --- /dev/null +++ b/src/_test-help.ts @@ -0,0 +1,12 @@ +// This a test help class for the purposes of dev-cli and readme generation. +// It is used for testing purposes only as the dev-cli generates a readme for itself in tests +// (see readme tests). For more info on help plugins check out @oclif/plugin-help + +export default class TestHelp { + [x: string]: any + + getCommandHelpForReadme(command: any) { + const flags = Object.keys(command.flags).join(', ') + return `Test help generated by test plugin for: ${command.id}\nIt has the following flags: ${flags}` + } +} diff --git a/test/fixtures/cli-with-custom-help/README.md b/test/fixtures/cli-with-custom-help/README.md new file mode 100644 index 00000000..95e7bed2 --- /dev/null +++ b/test/fixtures/cli-with-custom-help/README.md @@ -0,0 +1,19 @@ +# cli-with-custom-help + +This file is a test for running `oclif-dev readme` in the presence of +a custom help class. It should use the custom help class to generate +the command documentation below. The test suite resets this file after +each test. + + + + +# Usage + + + + +# Commands + + + diff --git a/test/fixtures/cli-with-custom-help/package.json b/test/fixtures/cli-with-custom-help/package.json new file mode 100644 index 00000000..6f88b76a --- /dev/null +++ b/test/fixtures/cli-with-custom-help/package.json @@ -0,0 +1,11 @@ +{ + "name": "cli-with-custom-help", + "files": [ + "/lib" + ], + "oclif": { + "commands": "./lib/commands", + "bin": "cli-with-custom-help", + "helpClass": "./lib/help" + } +} diff --git a/test/fixtures/cli-with-custom-help/src/commands/hello.ts b/test/fixtures/cli-with-custom-help/src/commands/hello.ts new file mode 100644 index 00000000..14bdb8a0 --- /dev/null +++ b/test/fixtures/cli-with-custom-help/src/commands/hello.ts @@ -0,0 +1,31 @@ +import {Command, flags} from '@oclif/command' + +export default class Hello extends Command { + static description = 'describe the command here' + + static examples = [ + `$ cli-with-custom-help hello +hello world from ./src/hello.ts! +`, + ] + + static flags = { + help: flags.help({char: 'h'}), + // flag with a value (-n, --name=VALUE) + name: flags.string({char: 'n', description: 'name to print'}), + // flag with no value (-f, --force) + force: flags.boolean({char: 'f'}), + } + + static args = [{name: 'file'}] + + async run() { + const {args, flags} = this.parse(Hello) + + const name = flags.name ?? 'world' + this.log(`hello ${name} from ./src/commands/hello.ts`) + if (args.file && flags.force) { + this.log(`you input --force and --file: ${args.file}`) + } + } +} diff --git a/test/fixtures/cli-with-custom-help/src/help.ts b/test/fixtures/cli-with-custom-help/src/help.ts new file mode 100644 index 00000000..10038015 --- /dev/null +++ b/test/fixtures/cli-with-custom-help/src/help.ts @@ -0,0 +1,12 @@ +import {HelpBase} from '@oclif/plugin-help' +import {Command} from '@oclif/config' + +export default class CustomHelp extends HelpBase { + showHelp() { + console.log('TODO: showHelp') + } + + showCommandHelp(command: Command) { + console.log(`Custom help for ${command.id}`) + } +} diff --git a/test/fixtures/cli-with-custom-help/src/index.ts b/test/fixtures/cli-with-custom-help/src/index.ts new file mode 100644 index 00000000..4caa481e --- /dev/null +++ b/test/fixtures/cli-with-custom-help/src/index.ts @@ -0,0 +1 @@ +export {run} from '@oclif/command' diff --git a/test/fixtures/cli-with-custom-help/tsconfig.json b/test/fixtures/cli-with-custom-help/tsconfig.json new file mode 100644 index 00000000..b4c5d768 --- /dev/null +++ b/test/fixtures/cli-with-custom-help/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "declaration": true, + "importHelpers": true, + "module": "commonjs", + "outDir": "lib", + "rootDir": "src", + "strict": true, + "target": "es2017" + }, + "include": [ + "src/**/*" + ] +} diff --git a/test/readme.test.ts b/test/readme.test.ts index a33b29f2..69ae88e6 100644 --- a/test/readme.test.ts +++ b/test/readme.test.ts @@ -1,5 +1,6 @@ import {expect, test} from '@oclif/test' import * as fs from 'fs-extra' +import * as path from 'path' const readme = fs.readFileSync('README.md', 'utf8') @@ -20,4 +21,21 @@ describe('readme', () => { .it('runs readme --multi', () => { expect(fs.readFileSync('README.md', 'utf8')).to.contain('manifest') }) + + describe('with custom help', () => { + const rootPath = path.join(__dirname, 'fixtures/cli-with-custom-help') + const readmePath = path.join(rootPath, 'README.md') + const originalReadme = fs.readFileSync(readmePath, 'utf8') + + test + .stdout() + .finally(() => fs.writeFileSync(readmePath, originalReadme)) + .stub(process, 'cwd', () => rootPath) + .command(['readme']) + .it('prints custom help to the readme', () => { + const newReadme = fs.readFileSync(readmePath, 'utf8') + + expect(newReadme).to.contain('Custom help for hello') + }) + }) })