Skip to content

Commit

Permalink
test: try out run integration tests without oclif/test
Browse files Browse the repository at this point in the history
  • Loading branch information
k80bowman committed Apr 24, 2024
1 parent a4b2106 commit 2ed9824
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/test/helpers/runCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {getConfig} from './testInstances'
import {Command} from '@heroku-cli/command-v9'
import {stdout, stderr} from 'stdout-stderr'

type CmdConstructorParams = ConstructorParameters<typeof Command>
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
14 changes: 14 additions & 0 deletions packages/cli/test/helpers/testInstances.ts
Original file line number Diff line number Diff line change
@@ -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)
}
95 changes: 59 additions & 36 deletions packages/cli/test/integration/run.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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')
// })
})
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2ed9824

Please sign in to comment.