-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdeploy.test.ts
More file actions
147 lines (109 loc) · 5.8 KB
/
deploy.test.ts
File metadata and controls
147 lines (109 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {expect} from 'chai';
import {afterEach, beforeEach} from 'mocha';
import sinon from 'sinon';
import CodeDeploy from '../../../src/commands/code/deploy.js';
import {createIsolatedConfigHooks, createTestCommand} from '../../helpers/test-setup.js';
describe('code deploy', () => {
const hooks = createIsolatedConfigHooks();
beforeEach(hooks.beforeEach);
afterEach(hooks.afterEach);
async function createCommand(flags: Record<string, unknown>, args: Record<string, unknown>) {
return createTestCommand(CodeDeploy, hooks.getConfig(), flags, args);
}
function stubCommon(command: any) {
const instance = {config: {hostname: 'example.com', codeVersion: 'v1'}};
sinon.stub(command, 'requireWebDavCredentials').returns(void 0);
sinon.stub(command, 'hasOAuthCredentials').returns(true);
sinon.stub(command, 'log').returns(void 0);
sinon.stub(command, 'warn').returns(void 0);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com', codeVersion: 'v1'}}));
sinon.stub(command, 'instance').get(() => instance);
return instance;
}
it('runs before hooks and returns early when skipped', async () => {
const command: any = await createCommand({}, {cartridgePath: '.'});
stubCommon(command);
sinon.stub(command, 'runBeforeHooks').resolves({skip: true, skipReason: 'by plugin'});
sinon.stub(command, 'runAfterHooks').rejects(new Error('Unexpected after hooks'));
sinon.stub(command, 'findCartridgesWithProviders').rejects(new Error('Unexpected cartridge discovery'));
const result = await command.run();
expect(result).to.deep.equal({cartridges: [], codeVersion: 'v1', reloaded: false});
});
it('errors when no cartridges are found', async () => {
const command: any = await createCommand({}, {cartridgePath: '.'});
stubCommon(command);
sinon.stub(command, 'runBeforeHooks').resolves({skip: false});
sinon.stub(command, 'findCartridgesWithProviders').resolves([]);
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
try {
await command.run();
expect.fail('Should have thrown');
} catch {
// expected
}
expect(errorStub.calledOnce).to.equal(true);
});
it('calls delete + upload and reload when flags are set', async () => {
const command: any = await createCommand({delete: true, reload: true}, {cartridgePath: '.'});
const instance = stubCommon(command);
sinon.stub(command, 'runBeforeHooks').resolves({skip: false});
const afterHooksStub = sinon.stub(command, 'runAfterHooks').resolves(void 0);
const cartridges = [{name: 'c1', src: '/tmp/c1', dest: 'c1'}];
sinon.stub(command, 'findCartridgesWithProviders').resolves(cartridges);
const deleteStub = sinon.stub().resolves(void 0);
const uploadStub = sinon.stub().resolves(void 0);
const reloadStub = sinon.stub().resolves(void 0);
command.operations = {
...command.operations,
deleteCartridges: deleteStub,
uploadCartridges: uploadStub,
reloadCodeVersion: reloadStub,
};
const result = await command.run();
expect(deleteStub.calledOnceWithExactly(instance, cartridges)).to.equal(true);
expect(uploadStub.calledOnceWithExactly(instance, cartridges)).to.equal(true);
expect(reloadStub.calledOnceWithExactly(instance, 'v1')).to.equal(true);
expect(result).to.deep.include({codeVersion: 'v1', reloaded: true});
expect(afterHooksStub.calledOnce).to.equal(true);
});
it('swallows reload errors and still succeeds', async () => {
const command: any = await createCommand({reload: true}, {cartridgePath: '.'});
stubCommon(command);
sinon.stub(command, 'runBeforeHooks').resolves({skip: false});
sinon.stub(command, 'runAfterHooks').resolves(void 0);
const cartridges = [{name: 'c1', src: '/tmp/c1', dest: 'c1'}];
sinon.stub(command, 'findCartridgesWithProviders').resolves(cartridges);
const uploadStub = sinon.stub().resolves(void 0);
const reloadStub = sinon.stub().rejects(new Error('reload failed'));
command.operations = {...command.operations, uploadCartridges: uploadStub, reloadCodeVersion: reloadStub};
const result = await command.run();
expect(result.reloaded).to.equal(false);
});
it('uses active code version when resolvedConfig is missing codeVersion', async () => {
const command: any = await createCommand({}, {cartridgePath: '.'});
sinon.stub(command, 'requireWebDavCredentials').returns(void 0);
sinon.stub(command, 'hasOAuthCredentials').returns(true);
sinon.stub(command, 'log').returns(void 0);
sinon.stub(command, 'warn').returns(void 0);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com', codeVersion: undefined}}));
const instanceConfig: any = {hostname: 'example.com', codeVersion: undefined};
const instance = {config: instanceConfig};
sinon.stub(command, 'instance').get(() => instance);
sinon.stub(command, 'runBeforeHooks').resolves({skip: false});
sinon.stub(command, 'runAfterHooks').resolves(void 0);
const activeStub = sinon.stub().resolves({id: 'active', active: true});
const cartridges = [{name: 'c1', src: '/tmp/c1', dest: 'c1'}];
sinon.stub(command, 'findCartridgesWithProviders').resolves(cartridges);
const uploadStub = sinon.stub().resolves(void 0);
command.operations = {...command.operations, getActiveCodeVersion: activeStub, uploadCartridges: uploadStub};
const result = await command.run();
expect(activeStub.getCall(0).args[0]).to.equal(instance);
expect(instanceConfig.codeVersion).to.equal('active');
expect(result.codeVersion).to.equal('active');
});
});