Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
Add test for readme-generation with custom help
Browse files Browse the repository at this point in the history
Co-authored-by: Chad Carbert <chadcarbert@me.com>
  • Loading branch information
Jamie White and chadian committed Sep 7, 2020
1 parent 190b6ef commit b05b6bc
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/_test-help.ts
Original file line number Diff line number Diff line change
@@ -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}`
}
}
19 changes: 19 additions & 0 deletions test/fixtures/cli-with-custom-help/README.md
Original file line number Diff line number Diff line change
@@ -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.

<!-- toc -->
<!-- tocstop -->

# Usage

<!-- usage -->
<!-- usagestop -->

# Commands

<!-- commands -->
<!-- commandsstop -->
11 changes: 11 additions & 0 deletions test/fixtures/cli-with-custom-help/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "cli-with-custom-help",
"files": [
"/lib"
],
"oclif": {
"commands": "./lib/commands",
"bin": "cli-with-custom-help",
"helpClass": "./lib/help"
}
}
31 changes: 31 additions & 0 deletions test/fixtures/cli-with-custom-help/src/commands/hello.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/cli-with-custom-help/src/help.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
}
1 change: 1 addition & 0 deletions test/fixtures/cli-with-custom-help/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {run} from '@oclif/command'
14 changes: 14 additions & 0 deletions test/fixtures/cli-with-custom-help/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"strict": true,
"target": "es2017"
},
"include": [
"src/**/*"
]
}
18 changes: 18 additions & 0 deletions test/readme.test.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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')
})
})
})

0 comments on commit b05b6bc

Please sign in to comment.