diff --git a/packages/cli/package.json b/packages/cli/package.json index c926fad75e..c429cccc5b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -113,6 +113,7 @@ "qqjs": "0.3.11", "read-pkg": "^4.0.1", "sinon": "^7.2.4", + "stdout-stderr": "^0.1.13", "ts-node": "^10.9.1", "typescript": "4.8.4" }, diff --git a/packages/cli/test/helpers/runCommand.ts b/packages/cli/test/helpers/runCommand.ts new file mode 100644 index 0000000000..0d0524d5e9 --- /dev/null +++ b/packages/cli/test/helpers/runCommand.ts @@ -0,0 +1,37 @@ +import {getConfig} from './testInstances' +import {Command} from '@heroku-cli/command-v9' +import {stdout, stderr} from 'stdout-stderr' + +type CmdConstructorParams = ConstructorParameters +export type GenericCmd = new (...args: CmdConstructorParams) => Command + +const stopMock = () => { + stdout.stop() + stderr.stop() +} + +const runCommand = async (Cmd: GenericCmd, args: string[] = [], printStd = false) => { + const conf = await getConfig() + const instance = new Cmd(args, conf) + if (printStd) { + stdout.print = true + stderr.print = true + } + + stdout.start() + stderr.start() + + try { + instance + .run() + .then(args => { + stopMock() + return args + }) + } catch (error: any) { + stopMock() + throw error + } +} + +export default runCommand diff --git a/packages/cli/test/helpers/testInstances.ts b/packages/cli/test/helpers/testInstances.ts new file mode 100644 index 0000000000..0b20b12dc0 --- /dev/null +++ b/packages/cli/test/helpers/testInstances.ts @@ -0,0 +1,14 @@ +import {APIClient} from '@heroku-cli/command-v9' +import {Config} from '@oclif/core-v1' + +export const getConfig = async () => { + const pjsonPath = require.resolve('../../package.json') + const conf = new Config({root: pjsonPath}) + await conf.load() + return conf +} + +export const getHerokuAPI = async () => { + const conf = await getConfig() + return new APIClient(conf) +} diff --git a/packages/cli/test/integration/run.integration.test.ts b/packages/cli/test/integration/run.integration.test.ts index d0b88cc59c..506237e536 100644 --- a/packages/cli/test/integration/run.integration.test.ts +++ b/packages/cli/test/integration/run.integration.test.ts @@ -1,44 +1,67 @@ -import {expect, test} from '@oclif/test' +import {expect} from 'chai' +import {stdout} from 'stdout-stderr' +import Cmd from '../../src/commands/run/index' +import runCommand from '../helpers/runCommand' -const testFactory = () => { - return test - .stdout() - .do(() => { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - process.stdout.isTTY = false - }) -} +// const testFactory = () => { +// return test +// .stdout() +// .do(() => { +// // eslint-disable-next-line @typescript-eslint/ban-ts-comment +// // @ts-ignore +// process.stdout.isTTY = false +// }) +// } describe('run', () => { - testFactory() - .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'echo 1 2 3']) - .it('runs a command', async ctx => { - expect(ctx.stdout).to.include('1 2 3') - }) + it('runs a command', async () => { + await runCommand(Cmd, ['--app=heroku-cli-ci-smoke-test-app', 'echo 1 2 3']) + expect(stdout).to.include('1 2 3') + }) + // testFactory() + // .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'echo 1 2 3']) + // .it('runs a command', async ctx => { + // expect(ctx.stdout).to.include('1 2 3') + // }) - testFactory() - .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo": "bar"} " ']) - .it('runs a command with spaces', ctx => { - expect(ctx.stdout).to.contain('{foo: bar}') - }) + it('runs a command with spaces', async () => { + await runCommand(Cmd, ['--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo": "bar"} " ']) + expect(stdout).to.contain('{foo:bar}') + }) + // testFactory() + // .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo": "bar"} " ']) + // .it('runs a command with spaces', ctx => { + // expect(ctx.stdout).to.contain('{foo: bar}') + // }) - testFactory() - .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo":"bar"}"']) - .it('runs a command with quotes', ctx => { - expect(ctx.stdout).to.contain('{foo:bar}') - }) + it('runs a command with quotes', async () => { + await runCommand(Cmd, ['--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo":"bar"}"']) + expect(stdout).to.contain('{foo:bar}') + }) + // testFactory() + // .command(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo":"bar"}"']) + // .it('runs a command with quotes', ctx => { + // expect(ctx.stdout).to.contain('{foo:bar}') + // }) - testFactory() - .command(['run', '--app=heroku-cli-ci-smoke-test-app', '-e FOO=bar', 'env']) - .it('runs a command with env vars', ctx => { - expect(ctx.stdout).to.include('FOO=bar') - }) + it('runs a command with env vars', async () => { + await runCommand(Cmd, ['--app=heroku-cli-ci-smoke-test-app', '-e FOO=bar', 'env']) + expect(stdout).to.include('FOO=bar') + }) + // testFactory() + // .command(['run', '--app=heroku-cli-ci-smoke-test-app', '-e FOO=bar', 'env']) + // .it('runs a command with env vars', ctx => { + // expect(ctx.stdout).to.include('FOO=bar') + // }) - testFactory() - .command(['run', '--app=heroku-cli-ci-smoke-test-app', '--exit-code', 'invalid-command']) - .exit(127) - .it('gets 127 status for invalid command', ctx => { - expect(ctx.stdout).to.include('invalid-command: command not found') - }) + it('prints an error for an invalid command', async () => { + await runCommand(Cmd, ['--app=heroku-cli-ci-smoke-test-app', '--exit-code', 'invalid-command']) + expect(stdout).to.include('invalid-command: command not found') + }) + // testFactory() + // .command(['run', '--app=heroku-cli-ci-smoke-test-app', '--exit-code', 'invalid-command']) + // .exit(127) + // .it('gets 127 status for invalid command', ctx => { + // expect(ctx.stdout).to.include('invalid-command: command not found') + // }) }) diff --git a/yarn.lock b/yarn.lock index 34ef0ebf10..0fb6305239 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11297,6 +11297,7 @@ __metadata: shell-escape: ^0.2.0 shell-quote: ^1.8.1 sinon: ^7.2.4 + stdout-stderr: ^0.1.13 tmp: ^0.0.33 true-myth: 2.2.3 ts-node: ^10.9.1 @@ -17986,6 +17987,16 @@ __metadata: languageName: node linkType: hard +"stdout-stderr@npm:^0.1.13": + version: 0.1.13 + resolution: "stdout-stderr@npm:0.1.13" + dependencies: + debug: ^4.1.1 + strip-ansi: ^6.0.0 + checksum: 3955a0afa9a483188929796fd9fa9417f845c3bfcd92f132b3ae7d2a862f91a846312e4244f07eb4dd4c5bd2fe95035db5db01679e7d3795d3ea0b772fc7a6f7 + languageName: node + linkType: hard + "stdout-stderr@npm:^0.1.9": version: 0.1.9 resolution: "stdout-stderr@npm:0.1.9"