Skip to content

Commit

Permalink
feat: use oclif/test v4 (#1420)
Browse files Browse the repository at this point in the history
* test: use oclif/test v4

* chore: clean up

* test: use new major of oclif/test

* chore: bump commitlint

* feat: generate CLI with oclif/test v4

* test: dont use createSandbox
  • Loading branch information
mdonnalley committed May 22, 2024
1 parent aa6f704 commit 1f8dbd9
Show file tree
Hide file tree
Showing 19 changed files with 446 additions and 740 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
"@commitlint/config-conventional": "^18",
"@commitlint/config-conventional": "^19",
"@oclif/plugin-legacy": "^2.0.8",
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3.2.11",
"@oclif/test": "^4",
"@types/async-retry": "^1.4.5",
"@types/chai": "^4.3.4",
"@types/cli-progress": "^3.11.0",
Expand All @@ -49,9 +49,10 @@
"@types/node": "^18",
"@types/semver": "^7.5.8",
"@types/shelljs": "^0.8.11",
"@types/sinon": "^17.0.3",
"@types/validate-npm-package-name": "^4.0.2",
"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 @@ -64,7 +65,7 @@
"prettier": "^3.2.5",
"shelljs": "^0.8.5",
"shx": "^0.3.4",
"sinon": "^17.0.2",
"sinon": "^18.0.0",
"ts-node": "^10.7.0",
"typescript": "^5"
},
Expand Down
4 changes: 3 additions & 1 deletion src/commands/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Manifest extends Command {
}),
}

public async run(): Promise<void> {
public async run(): Promise<Interfaces.Manifest> {
const {flags} = await this.parse(Manifest)
try {
unlinkSync('oclif.manifest.json')
Expand Down Expand Up @@ -105,6 +105,8 @@ export default class Manifest extends Command {
writeFileSync(file, JSON.stringify(plugin.manifest, null, 2))

this.log(`wrote manifest to ${file}`)

return plugin.manifest
}

private async executeCommand(command: string, options?: ExecOptions): Promise<{stderr: string; stdout: string}> {
Expand Down
13 changes: 11 additions & 2 deletions src/commands/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.
default: true,
description: 'Include aliases in the command list.',
}),
'dry-run': Flags.boolean({
description: 'Prints the generated README without modifying the file.',
}),
multi: Flags.boolean({
description: 'Create a different markdown page for each topic.',
}),
Expand Down Expand Up @@ -55,7 +58,7 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.

private flags!: Interfaces.InferredFlags<typeof Readme.flags>

async run(): Promise<void> {
async run(): Promise<string> {
const {flags} = await this.parse(Readme)
this.flags = flags
this.flags['plugin-directory'] ??= process.cwd()
Expand Down Expand Up @@ -89,6 +92,7 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.

const generator = new ReadmeGenerator(config, {
aliases: this.flags.aliases,
dryRun: this.flags['dry-run'],
multi: this.flags.multi,
nestedTopicsDepth: this.flags['nested-topics-depth'],
outputDir: this.flags['output-dir'],
Expand All @@ -98,6 +102,11 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.
version: this.flags.version,
})

await generator.generate()
const readme = await generator.generate()
if (this.flags['dry-run']) {
this.log(readme)
}

return readme
}
}
2 changes: 1 addition & 1 deletion src/readme-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ USAGE
}

protected async write(file: string, content: string): Promise<void> {
await fs.outputFile(file, content)
if (!this.options.dryRun) await fs.outputFile(file, content)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/shared/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3",
"@oclif/test": "^4",
"@types/chai": "^4",
"@types/mocha": "^10",
"@types/node": "^18",
Expand Down
11 changes: 5 additions & 6 deletions templates/cli/shared/test/commands/hello/index.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'

describe('hello', () => {
test
.stdout()
.command(['hello', 'friend', '--from=oclif'])
.it('runs hello cmd', ctx => {
expect(ctx.stdout).to.contain('hello friend from oclif!')
it('runs hello', async () => {
const {stdout} = await runCommand('hello friend --from oclif')
expect(stdout).to.contain('hello friend from oclif!')
})
})
11 changes: 5 additions & 6 deletions templates/cli/shared/test/commands/hello/world.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'

describe('hello world', () => {
test
.stdout()
.command(['hello:world'])
.it('runs hello world cmd', ctx => {
expect(ctx.stdout).to.contain('hello world!')
it('runs hello world cmd', async () => {
const {stdout} = await runCommand('hello world')
expect(stdout).to.contain('hello world!')
})
})
19 changes: 8 additions & 11 deletions templates/test/command.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'

describe('<%- name %>', () => {
test
.stdout()
.command(['<%- name %>'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
it('runs <%- name %> cmd', async () => {
const {stdout} = await runCommand('<%- name %>')
expect(stdout).to.contain('hello world')
})

test
.stdout()
.command(['<%- name %>', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
it('runs <%- name %> --name oclif', async () => {
const {stdout} = await runCommand('<%- name %> --name oclif')
expect(stdout).to.contain('hello oclif')
})
})
12 changes: 6 additions & 6 deletions templates/test/hook.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {expect, test} from '@oclif/test'
import {runHook} from '@oclif/test'
import {expect} from 'chai'

describe('hooks', () => {
test
.stdout()
.hook('init', {id: 'mycommand'})
.do(output => expect(output.stdout).to.contain('example hook running mycommand'))
.it('shows a message')
it('shows a message', async () => {
const {stdout} = await runHook('init', {id: 'mycommand'})
expect(stdout).to.contain('example hook running mycommand')
})
})
2 changes: 1 addition & 1 deletion test/helpers/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from '@oclif/test'
import {expect} from 'chai'

import {deleteFolder} from './helper'

Expand Down
2 changes: 1 addition & 1 deletion test/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DeleteObjectsRequest, ObjectIdentifier} from '@aws-sdk/client-s3'
import {expect} from '@oclif/test'
import {expect} from 'chai'
import * as fs from 'node:fs'
import * as shelljs from 'shelljs'

Expand Down
44 changes: 23 additions & 21 deletions test/integration/deb.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
import * as fs from 'fs-extra'
import _ from 'lodash'
import {exec as execSync} from 'node:child_process'
Expand All @@ -14,7 +15,7 @@ const pjsonPath = require.resolve('../../package.json')
const originalPJSON = _.cloneDeep(pjson)
const target = [process.platform, process.arch].join('-')

const onlyLinux = process.platform === 'linux' ? test : test.skip()
const onlyLinux = process.platform === 'linux' ? it : it.skip
const testRun = `test-${Math.random().toString().split('.')[1].slice(0, 4)}`

describe('publish:deb', () => {
Expand All @@ -32,6 +33,7 @@ describe('publish:deb', () => {
await fs.writeJSON(pjsonPath, pjson, {spaces: 2})
await fs.emptyDir(root)
})

afterEach(async () => {
if (!process.env.PRESERVE_ARTIFACTS) {
await deleteFolder(bucket, `${basePrefix}/versions/${pjson.version}/`)
Expand All @@ -40,23 +42,23 @@ describe('publish:deb', () => {
await fs.writeJSON(pjsonPath, originalPJSON, {spaces: 2})
})

onlyLinux
.command(['pack:deb'])
.command(['upload:deb'])
.it('publishes valid releases', async () => {
const sha = await gitSha(process.cwd(), {short: true})
await exec('cat test/release.key | sudo apt-key add -')
await exec(
`sudo sh -c 'echo "deb https://${developerSalesforceCom}/${basePrefix}/versions/${pjson.version}/${sha}/apt/ /" > /etc/apt/sources.list.d/oclif.list'`,
)
await exec('sudo apt-get update')
await exec('sudo apt-get install -y oclif')
await exec('oclif --version')
// test the binAliases section
const {stdout: oclif2} = await exec('oclif2 --version')
expect(oclif2).to.contain(`oclif/${pjson.version} ${target} node-v${pjson.oclif.update.node.version}`)

const {stdout: oclif} = await exec('oclif --version')
expect(oclif).to.contain(`oclif/${pjson.version} ${target} node-v${pjson.oclif.update.node.version}`)
})
onlyLinux('publishes valid releases', async () => {
await runCommand('pack deb')
await runCommand('upload deb')

const sha = await gitSha(process.cwd(), {short: true})
await exec('cat test/release.key | sudo apt-key add -')
await exec(
`sudo sh -c 'echo "deb https://${developerSalesforceCom}/${basePrefix}/versions/${pjson.version}/${sha}/apt/ /" > /etc/apt/sources.list.d/oclif.list'`,
)
await exec('sudo apt-get update')
await exec('sudo apt-get install -y oclif')
await exec('oclif --version')
// test the binAliases section
const {stdout: oclif2} = await exec('oclif2 --version')
expect(oclif2).to.contain(`oclif/${pjson.version} ${target} node-v${pjson.oclif.update.node.version}`)

const {stdout: oclif} = await exec('oclif --version')
expect(oclif).to.contain(`oclif/${pjson.version} ${target} node-v${pjson.oclif.update.node.version}`)
})
})
41 changes: 21 additions & 20 deletions test/integration/macos.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
import {emptyDir, writeJSON} from 'fs-extra'
import _ from 'lodash'
import {createWriteStream} from 'node:fs'
Expand All @@ -12,7 +13,7 @@ const pjson = require('../../package.json')
const pjsonPath = require.resolve('../../package.json')
const originalPJSON = _.cloneDeep(pjson)

const onlyMacos = process.platform === 'darwin' ? test : test.skip()
const onlyMacos = process.platform === 'darwin' ? it : it.skip
const testRun = `test-${Math.random().toString().split('.')[1].slice(0, 4)}`

describe('publish:macos', () => {
Expand Down Expand Up @@ -41,22 +42,22 @@ describe('publish:macos', () => {
await writeJSON(pjsonPath, originalPJSON, {spaces: 2})
})

onlyMacos
.command(['pack:macos'])
.do(async () => {
// install the intel silicon pkg
;[pkg, sha] = await findDistFileSha(cwd, 'macos', (f) => f.endsWith('x64.pkg'))
await exec(`sudo installer -pkg ${path.join(cwd, 'dist', 'macos', pkg)} -target /`)
expect(exec('oclif --version').stdout).to.contain(`oclif/${pjson.version}`)
// tests binAlias
expect(exec('oclif2 --version').stdout).to.contain(`oclif/${pjson.version}`)
})
.command(['upload:macos'])
.it('publishes valid releases', async () => {
const {default: got} = await import('got')
await pipeline(
got.stream(`https://${developerSalesforceCom}/${basePrefix}/versions/${pjson.version}/${sha}/${pkg}`),
createWriteStream(pkg),
)
})
onlyMacos('publishes valid releases', async () => {
await runCommand('pack macos')

// install the intel silicon pkg
;[pkg, sha] = await findDistFileSha(cwd, 'macos', (f) => f.endsWith('x64.pkg'))
exec(`sudo installer -pkg ${path.join(cwd, 'dist', 'macos', pkg)} -target /`)
expect(exec('oclif --version').stdout).to.contain(`oclif/${pjson.version}`)
// tests binAlias
expect(exec('oclif2 --version').stdout).to.contain(`oclif/${pjson.version}`)

await runCommand('upload macos')

const {default: got} = await import('got')
await pipeline(
got.stream(`https://${developerSalesforceCom}/${basePrefix}/versions/${pjson.version}/${sha}/${pkg}`),
createWriteStream(pkg),
)
})
})
20 changes: 10 additions & 10 deletions test/integration/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Interfaces} from '@oclif/core'
import {expect, test} from '@oclif/test'
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
import {emptyDir, writeJSON} from 'fs-extra'
import _ from 'lodash'
import {exec as execSync} from 'node:child_process'
Expand All @@ -19,7 +20,7 @@ const pjson = require('../../package.json')
const pjsonPath = require.resolve('../../package.json')
const originalPJSON = _.cloneDeep(pjson)
const target = [process.platform, process.arch].join('-')
const skipIfWindows = process.platform === 'win32' ? test.skip() : test
const skipIfWindows = process.platform === 'win32' ? it.skip : it
const testRun = `test-${Math.random().toString().split('.')[1].slice(0, 4)}`
const cwd = process.cwd()
pjson.version = `${pjson.version}-${testRun}`
Expand Down Expand Up @@ -82,12 +83,11 @@ describe('upload tarballs', async () => {
await writeJSON(pjsonPath, originalPJSON, {spaces: 2})
})

skipIfWindows
.command(['pack:tarballs', '--parallel', '--xz'])
.command(['upload:tarballs', '--xz'])
.command(['promote', '--channel', pjson.version, '--sha', sha, '--version', pjson.version])
.it('checks uploads for version and channel', async () => {
await manifest(`versions/${pjson.version}/${sha}`, pjson.oclif.update.node.version)
await manifest(`channels/${pjson.version}`, pjson.oclif.update.node.version)
})
skipIfWindows('checks uploads for version and channel', async () => {
await runCommand('pack tarballs --parallel --xz')
await runCommand('upload tarballs --xz')
await runCommand(`promote --channel ${pjson.version} --sha ${sha} --version ${pjson.version}`)
await manifest(`versions/${pjson.version}/${sha}`, pjson.oclif.update.node.version)
await manifest(`channels/${pjson.version}`, pjson.oclif.update.node.version)
})
})
Loading

0 comments on commit 1f8dbd9

Please sign in to comment.