Skip to content

Commit

Permalink
test: migrate UTs to oclif/test v4 (#671)
Browse files Browse the repository at this point in the history
* test: migrate UTs to oclif/test v4

* chore: use latest oclif/test

* test: use sinon's default sandbox

* chore: update dev deps

---------

Co-authored-by: Mike Donnalley <[email protected]>
  • Loading branch information
cristiand391 and mdonnalley authored May 22, 2024
1 parent 470525f commit 3619aab
Show file tree
Hide file tree
Showing 3 changed files with 453 additions and 959 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@commitlint/config-conventional": "^18",
"@commitlint/config-conventional": "^19",
"@oclif/plugin-help": "^6",
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3.2.15",
"@oclif/test": "^4.0.1",
"@types/chai": "^4.3.11",
"@types/lodash": "^4.17.4",
"@types/mocha": "^10.0.6",
"@types/node": "^18",
"@types/sinon": "^17.0.3",
"chai": "^4.4.1",
"commitlint": "^18",
"commitlint": "^19",
"eslint": "^8.57.0",
"eslint-config-oclif": "^5.2.0",
"eslint-config-oclif-typescript": "^3.1.7",
Expand All @@ -29,6 +31,7 @@
"oclif": "^4.10.11",
"prettier": "^3.2.5",
"shx": "^0.3.3",
"sinon": "^18.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
Expand Down
265 changes: 130 additions & 135 deletions test/commands/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Command} from '@oclif/core'
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
import sinon from 'sinon'

import Commands from '../../src/commands/commands.js'

Expand Down Expand Up @@ -75,140 +77,133 @@ const getRegexForCommand = (index: number): RegExp =>
new RegExp(`${commandList[index].id}\\s+${commandList[index].description}`)

describe('commands', () => {
test
.loadConfig({root: process.cwd()})
.stdout()
.command(['commands'])
.it('runs commands', (ctx) => {
expect(ctx.stdout).to.include('commands list all the commands')
expect(ctx.stdout).to.include('help Display help for oclif-example.')
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--hidden'])
.it('runs commands --hidden', (ctx) => {
expect(ctx.stdout).to.match(getRegexForCommand(0))
expect(ctx.stdout).to.match(getRegexForCommand(1))
expect(ctx.stdout).to.not.match(getRegexForCommand(2))
expect(ctx.stdout).to.match(getRegexForCommand(3))
expect(ctx.stdout).to.match(getRegexForCommand(4))
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--filter=Command=^topic'])
.it('runs commands --filter="Command=^topic"', (ctx) => {
expect(ctx.stdout).to.match(getRegexForCommand(0))
expect(ctx.stdout).not.to.match(getRegexForCommand(1))
expect(ctx.stdout).to.not.match(getRegexForCommand(2))
expect(ctx.stdout).to.match(getRegexForCommand(3))
expect(ctx.stdout).to.not.match(getRegexForCommand(4))
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--filter=Plugin=anothertest'])
.it('runs commands --filter="Plugin=anothertest"', (ctx) => {
expect(ctx.stdout).to.not.match(getRegexForCommand(0))
expect(ctx.stdout).to.match(getRegexForCommand(1))
expect(ctx.stdout).to.not.match(getRegexForCommand(2))
expect(ctx.stdout).to.not.match(getRegexForCommand(3))
expect(ctx.stdout).to.not.match(getRegexForCommand(4))
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--filter=Command=anothertopic:subtopic:command'])
.it('runs commands --filter="Command=anothertopic:subtopic:command"', (ctx) => {
expect(ctx.stdout).to.not.match(getRegexForCommand(0))
expect(ctx.stdout).to.match(getRegexForCommand(1))
expect(ctx.stdout).to.not.match(getRegexForCommand(2))
expect(ctx.stdout).to.not.match(getRegexForCommand(3))
expect(ctx.stdout).to.not.match(getRegexForCommand(4))
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--columns=Command', '--no-header'])
.it('runs commands --filter=Command=', (ctx) => {
expect(ctx.stdout).to.match(/ anothertopic:subtopic:command\s+/)
expect(ctx.stdout).to.match(/ topic:subtopic:command\s+/)
expect(ctx.stdout).to.match(/ topic:subtopic:dep\s+/)
expect(ctx.stdout).to.not.include('topic:subtopic:deprecated')
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--filter=Command=subtopic:command'])
.it('runs commands --filter"=Command=subtopic:command"', (ctx) => {
expect(ctx.stdout).to.equal(
' Command Summary \n' +
' ───────────────────────────── ─────────────────────────────── \n' +
' anothertopic:subtopic:command another super good test command \n' +
' topic:subtopic:command super good test command \n',
)
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--filter=Command=^topic:subtopic:command'])
.it('runs commands --filter"=Command=^topic:subtopic:command"', (ctx) => {
expect(ctx.stdout).to.equal(
' Command Summary \n' +
' ────────────────────── ─────────────────────── \n' +
' topic:subtopic:command super good test command \n',
)
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--json'])
.it('runs commands --json', (ctx: {stdout: string}) => {
const commands = JSON.parse(ctx.stdout)
expect(commands[0].id).to.equal('anothertopic:subtopic:command')
expect(commands[0].testCustomProperty).to.equal('test')
expect(commands[0].anotherCustomProperty[0]).to.equal(5)
expect(commands[0].anotherCustomProperty[1]).to.deep.equal({foo: 'bar'})
expect(commands[0]).to.not.have.property('circularObj')
expect(commands[1].id).to.equal('topic:subtopic:command')
expect(commands[1].testCustomProperty).to.equal('test')
expect(commands[1].anotherCustomProperty).to.equal(undefined)
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands'])
.it('hides deprecated commands', (ctx: {stdout: string}) => {
expect(ctx.stdout).to.not.include('topic:subtopic:deprecated')
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands'])
.it('hides deprecated aliases', (ctx: {stdout: string}) => {
expect(ctx.stdout).to.not.include('topic:subtopic:deprecated-alias')
})
afterEach(() => {
sinon.restore()
})

test
.stub(Commands.prototype, 'getCommands', (stub) => stub.returns(commandList))
.stdout()
.command(['commands', '--deprecated'])
.it('shows deprecated commands when asked', (ctx: {stdout: string}) => {
expect(ctx.stdout).to.match(getRegexForCommand(0))
expect(ctx.stdout).to.match(getRegexForCommand(1))
expect(ctx.stdout).to.match(getRegexForCommand(2))
expect(ctx.stdout).to.match(getRegexForCommand(3))
expect(ctx.stdout).to.not.match(getRegexForCommand(4))
it('prints commands', async () => {
const {stdout} = await runCommand(['commands'], {
root: process.cwd(),
})
expect(stdout).to.include('commands list all the commands')
expect(stdout).to.include('help Display help for oclif-example.')
})

it('runs commands --hidden', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--hidden'])
expect(stdout).to.match(getRegexForCommand(0))
expect(stdout).to.match(getRegexForCommand(1))
expect(stdout).to.not.match(getRegexForCommand(2))
expect(stdout).to.match(getRegexForCommand(3))
expect(stdout).to.match(getRegexForCommand(4))
})

it('runs commands --filter="Command=^topic"', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--filter=Command=^topic'])
expect(stdout).to.match(getRegexForCommand(0))
expect(stdout).not.to.match(getRegexForCommand(1))
expect(stdout).to.not.match(getRegexForCommand(2))
expect(stdout).to.match(getRegexForCommand(3))
expect(stdout).to.not.match(getRegexForCommand(4))
})

it('runs commands --filter="Plugin=anothertest"', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--filter=Plugin=anothertest'])
expect(stdout).to.not.match(getRegexForCommand(0))
expect(stdout).to.match(getRegexForCommand(1))
expect(stdout).to.not.match(getRegexForCommand(2))
expect(stdout).to.not.match(getRegexForCommand(3))
expect(stdout).to.not.match(getRegexForCommand(4))
})

it('runs commands --filter="Command=anothertopic:subtopic:command"', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--filter=Command=anothertopic:subtopic:command'])
expect(stdout).to.not.match(getRegexForCommand(0))
expect(stdout).to.match(getRegexForCommand(1))
expect(stdout).to.not.match(getRegexForCommand(2))
expect(stdout).to.not.match(getRegexForCommand(3))
expect(stdout).to.not.match(getRegexForCommand(4))
})

it('runs commands --filter=Command=', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--columns=Command', '--no-header'])
expect(stdout).to.match(/ anothertopic:subtopic:command\s+/)
expect(stdout).to.match(/ topic:subtopic:command\s+/)
expect(stdout).to.match(/ topic:subtopic:dep\s+/)
expect(stdout).to.not.include('topic:subtopic:deprecated')
})

it('runs commands --filter"=Command=subtopic:command"', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--filter=Command=subtopic:command'])
expect(stdout).to.equal(
' Command Summary \n' +
' ───────────────────────────── ─────────────────────────────── \n' +
' anothertopic:subtopic:command another super good test command \n' +
' topic:subtopic:command super good test command \n',
)
})

it('runs commands --filter"=Command=^topic:subtopic:command"', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--filter=Command=^topic:subtopic:command'])
expect(stdout).to.equal(
' Command Summary \n' +
' ────────────────────── ─────────────────────── \n' +
' topic:subtopic:command super good test command \n',
)
})

it('runs commands --json', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--json'])
const commands = JSON.parse(stdout)
expect(commands[0].id).to.equal('anothertopic:subtopic:command')
expect(commands[0].testCustomProperty).to.equal('test')
expect(commands[0].anotherCustomProperty[0]).to.equal(5)
expect(commands[0].anotherCustomProperty[1]).to.deep.equal({foo: 'bar'})
expect(commands[0]).to.not.have.property('circularObj')
expect(commands[1].id).to.equal('topic:subtopic:command')
expect(commands[1].testCustomProperty).to.equal('test')
expect(commands[1].anotherCustomProperty).to.equal(undefined)
})

it('hides deprecated commands', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands'])
expect(stdout).to.not.include('topic:subtopic:deprecated')
})

it('hides deprecated aliases', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands'])
expect(stdout).to.not.include('topic:subtopic:deprecated-alias')
})

it('shows deprecated commands when asked', async () => {
// @ts-expect-error type mismatch
sinon.stub(Commands.prototype, 'getCommands').returns(commandList)
const {stdout} = await runCommand(['commands', '--deprecated'])
expect(stdout).to.not.include('topic:subtopic:deprecated-alias')
expect(stdout).to.match(getRegexForCommand(0))
expect(stdout).to.match(getRegexForCommand(1))
expect(stdout).to.match(getRegexForCommand(2))
expect(stdout).to.match(getRegexForCommand(3))
expect(stdout).to.not.match(getRegexForCommand(4))
})
})
Loading

0 comments on commit 3619aab

Please sign in to comment.