|
| 1 | +import {runCommand} from '@oclif/test' |
| 2 | +import {expect} from 'chai' |
| 3 | +import nock from 'nock' |
| 4 | + |
| 5 | +import {unwrap} from '../helpers/utils/unwrap.js' |
| 6 | + |
| 7 | +function setupRunNocks(command: string, skipAppCheck = false) { |
| 8 | + const api = nock('https://api.heroku.com') |
| 9 | + .get('/account') |
| 10 | + .reply(200, {email: 'test@example.com'}) |
| 11 | + |
| 12 | + if (!skipAppCheck) { |
| 13 | + api |
| 14 | + .get('/apps/heroku-cli-ci-smoke-test-app') |
| 15 | + .reply(200, {name: 'heroku-cli-ci-smoke-test-app', stack: {name: 'heroku-22'}}) |
| 16 | + } |
| 17 | + |
| 18 | + api |
| 19 | + .post('/apps/heroku-cli-ci-smoke-test-app/dynos') |
| 20 | + .reply(201, { |
| 21 | + attach_url: 'rendezvous://rendezvous.runtime.heroku.com:5000', |
| 22 | + command, |
| 23 | + created_at: '2020-01-01T00:00:00Z', |
| 24 | + id: '12345678-1234-1234-1234-123456789012', |
| 25 | + name: 'run.1234', |
| 26 | + size: 'basic', |
| 27 | + state: 'starting', |
| 28 | + type: 'run', |
| 29 | + updated_at: '2020-01-01T00:00:00Z', |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +function buildProcessArgv(commandParts: string[], flags: string[] = []): string[] { |
| 34 | + return ['node', 'heroku', 'run', ...flags, '--app=heroku-cli-ci-smoke-test-app', ...commandParts] |
| 35 | +} |
| 36 | + |
| 37 | +describe('run', function () { |
| 38 | + let originalArgv: string[] |
| 39 | + |
| 40 | + beforeEach(function () { |
| 41 | + originalArgv = process.argv |
| 42 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 43 | + // @ts-ignore |
| 44 | + process.stdout.isTTY = false |
| 45 | + }) |
| 46 | + |
| 47 | + afterEach(function () { |
| 48 | + process.argv = originalArgv |
| 49 | + nock.cleanAll() |
| 50 | + }) |
| 51 | + |
| 52 | + it('runs a command', async function () { |
| 53 | + // Set up process.argv to match what revertSortedArgs expects |
| 54 | + process.argv = buildProcessArgv(['echo', '1', '2', '3']) |
| 55 | + setupRunNocks('echo 1 2 3') |
| 56 | + |
| 57 | + const {error} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', 'echo', '1', '2', '3']) |
| 58 | + |
| 59 | + // Expected to fail when trying to connect to rendezvous |
| 60 | + // This verifies the command flow works correctly up to the connection point |
| 61 | + expect(error).to.exist |
| 62 | + }) |
| 63 | + |
| 64 | + it('respects --no-launcher', async function () { |
| 65 | + // Note: when --no-launcher is set, shouldPrependLauncher returns early without checking the app |
| 66 | + process.argv = buildProcessArgv(['echo', '1', '2', '3'], ['--no-launcher']) |
| 67 | + setupRunNocks('echo 1 2 3', true) |
| 68 | + |
| 69 | + const {error} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', '--no-launcher', 'echo', '1', '2', '3']) |
| 70 | + |
| 71 | + // Expected to fail when trying to connect to rendezvous |
| 72 | + expect(error).to.exist |
| 73 | + }) |
| 74 | + |
| 75 | + it.skip('runs a command with spaces', async function () { |
| 76 | + const {stdout} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo": "bar"} " ']) |
| 77 | + |
| 78 | + expect(unwrap(stdout)).to.contain('{foo: bar}') |
| 79 | + }) |
| 80 | + |
| 81 | + it.skip('runs a command with quotes', async function () { |
| 82 | + const {stdout} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', 'ruby -e "puts ARGV[0]" "{"foo":"bar"}"']) |
| 83 | + |
| 84 | + expect(stdout).to.contain('{foo:bar}') |
| 85 | + }) |
| 86 | + |
| 87 | + it('runs a command with env vars', async function () { |
| 88 | + process.argv = buildProcessArgv(['env'], ['-e', 'FOO=bar']) |
| 89 | + setupRunNocks('env') |
| 90 | + |
| 91 | + const {error} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', '-e', 'FOO=bar', 'env']) |
| 92 | + |
| 93 | + // Expected to fail when trying to connect to rendezvous |
| 94 | + expect(error).to.exist |
| 95 | + }) |
| 96 | + |
| 97 | + it.skip('gets 127 status for invalid command', async function () { |
| 98 | + // Exit code handling is better tested in unit tests |
| 99 | + const {stdout} = await runCommand(['run', '--app=heroku-cli-ci-smoke-test-app', '--exit-code', 'invalid-command']) |
| 100 | + |
| 101 | + expect(unwrap(stdout)).to.include('invalid-command: command not found') |
| 102 | + }) |
| 103 | +}) |
0 commit comments