|
| 1 | +jest.mock('child_process'); |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { execProgram } from '../../lib/api/cxapp/exec'; |
| 4 | +import { setVerbose } from '../../lib/logging'; |
| 5 | +import { Configuration } from '../../lib/settings'; |
| 6 | +import * as bockfs from '../bockfs'; |
| 7 | +import { testAssembly } from '../util'; |
| 8 | +import { mockSpawn } from '../util/mock-child_process'; |
| 9 | +import { MockSdkProvider } from '../util/mock-sdk'; |
| 10 | + |
| 11 | +setVerbose(true); |
| 12 | + |
| 13 | +let sdkProvider: MockSdkProvider; |
| 14 | +let config: Configuration; |
| 15 | +beforeEach(() => { |
| 16 | + sdkProvider = new MockSdkProvider(); |
| 17 | + config = new Configuration(); |
| 18 | + |
| 19 | + config.settings.set(['output'], 'cdk.out'); |
| 20 | + |
| 21 | + // insert contents in fake filesystem |
| 22 | + bockfs({ |
| 23 | + '/home/project/cloud-executable': 'ARBITRARY', |
| 24 | + '/home/project/windows.js': 'ARBITRARY', |
| 25 | + 'home/project/executable-app.js': 'ARBITRARY' |
| 26 | + }); |
| 27 | + bockfs.workingDirectory('/home/project'); |
| 28 | + bockfs.executable('/home/project/cloud-executable'); |
| 29 | + bockfs.executable('/home/project/executable-app.js'); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + sinon.restore(); |
| 34 | + bockfs.restore(); |
| 35 | +}); |
| 36 | + |
| 37 | +test('validates --app key is present', async () => { |
| 38 | + // GIVEN no config key for `app` |
| 39 | + await expect(execProgram(sdkProvider, config)).rejects.toThrow( |
| 40 | + '--app is required either in command-line, in cdk.json or in ~/.cdk.json' |
| 41 | + ); |
| 42 | + |
| 43 | +}); |
| 44 | + |
| 45 | +test('bypasses synth when app points to a cloud assembly', async () => { |
| 46 | + // GIVEN |
| 47 | + config.settings.set(['app'], 'cdk.out'); |
| 48 | + writeOutputAssembly(); |
| 49 | + |
| 50 | + // WHEN |
| 51 | + const cloudAssembly = await execProgram(sdkProvider, config); |
| 52 | + expect(cloudAssembly.artifacts).toEqual([]); |
| 53 | + expect(cloudAssembly.directory).toEqual('cdk.out'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('the application set in --app is executed', async () => { |
| 57 | + // GIVEN |
| 58 | + config.settings.set(['app'], 'cloud-executable'); |
| 59 | + mockSpawn({ |
| 60 | + commandLine: ['cloud-executable'], |
| 61 | + sideEffect: () => writeOutputAssembly(), |
| 62 | + }); |
| 63 | + |
| 64 | + // WHEN |
| 65 | + await execProgram(sdkProvider, config); |
| 66 | +}); |
| 67 | + |
| 68 | +test('the application set in --app is executed as-is if it contains a filename that does not exist', async () => { |
| 69 | + // GIVEN |
| 70 | + config.settings.set(['app'], 'does-not-exist'); |
| 71 | + mockSpawn({ |
| 72 | + commandLine: ['does-not-exist'], |
| 73 | + sideEffect: () => writeOutputAssembly(), |
| 74 | + }); |
| 75 | + |
| 76 | + // WHEN |
| 77 | + await execProgram(sdkProvider, config); |
| 78 | +}); |
| 79 | + |
| 80 | +test('the application set in --app is executed with arguments', async () => { |
| 81 | + // GIVEN |
| 82 | + config.settings.set(['app'], 'cloud-executable an-arg'); |
| 83 | + mockSpawn({ |
| 84 | + commandLine: ['cloud-executable', 'an-arg'], |
| 85 | + sideEffect: () => writeOutputAssembly(), |
| 86 | + }); |
| 87 | + |
| 88 | + // WHEN |
| 89 | + await execProgram(sdkProvider, config); |
| 90 | +}); |
| 91 | + |
| 92 | +test('application set in --app as `*.js` always uses handler on windows', async () => { |
| 93 | + // GIVEN |
| 94 | + sinon.stub(process, 'platform').value('win32'); |
| 95 | + config.settings.set(['app'], 'windows.js'); |
| 96 | + mockSpawn({ |
| 97 | + commandLine: [process.execPath, 'windows.js'], |
| 98 | + sideEffect: () => writeOutputAssembly(), |
| 99 | + }); |
| 100 | + |
| 101 | + // WHEN |
| 102 | + await execProgram(sdkProvider, config); |
| 103 | +}); |
| 104 | + |
| 105 | +test('application set in --app is `*.js` and executable', async () => { |
| 106 | + // GIVEN |
| 107 | + config.settings.set(['app'], 'executable-app.js'); |
| 108 | + mockSpawn({ |
| 109 | + commandLine: ['executable-app.js'], |
| 110 | + sideEffect: () => writeOutputAssembly(), |
| 111 | + }); |
| 112 | + |
| 113 | + // WHEN |
| 114 | + await execProgram(sdkProvider, config); |
| 115 | +}); |
| 116 | + |
| 117 | +function writeOutputAssembly() { |
| 118 | + const asm = testAssembly({ |
| 119 | + stacks: [] |
| 120 | + }); |
| 121 | + bockfs.write('/home/project/cdk.out/manifest.json', JSON.stringify(asm)); |
| 122 | +} |
0 commit comments