Skip to content

Commit 48aedd9

Browse files
shetzelsfsholden
authored andcommitted
fix: set an overridden apiVersion on a created connection (#274)
a connection that is created from a username should use the apiVersion passed rather than the default
1 parent 64f86bd commit 48aedd9

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

src/client/metadataTransfer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { AuthInfo, Connection } from '@salesforce/core';
7+
import { AuthInfo, Connection, Logger } from '@salesforce/core';
88
import { EventEmitter } from 'events';
99
import { ComponentSet } from '../collections';
1010
import { MetadataTransferError } from '../errors';
@@ -13,20 +13,25 @@ import { MetadataRequestStatus, RequestStatus, MetadataTransferResult } from './
1313
export interface MetadataTransferOptions {
1414
usernameOrConnection: string | Connection;
1515
components: ComponentSet;
16+
apiVersion?: string;
1617
}
1718

1819
export abstract class MetadataTransfer<
1920
Status extends MetadataRequestStatus,
2021
Result extends MetadataTransferResult
2122
> {
2223
protected components: ComponentSet;
24+
protected logger: Logger;
2325
private signalCancel = false;
2426
private event = new EventEmitter();
2527
private usernameOrConnection: string | Connection;
28+
private apiVersion: string;
2629

27-
constructor({ usernameOrConnection, components }: MetadataTransferOptions) {
30+
constructor({ usernameOrConnection, components, apiVersion }: MetadataTransferOptions) {
2831
this.usernameOrConnection = usernameOrConnection;
2932
this.components = components;
33+
this.apiVersion = apiVersion;
34+
this.logger = Logger.childFromRoot(this.constructor.name);
3035
}
3136

3237
/**
@@ -81,6 +86,10 @@ export abstract class MetadataTransfer<
8186
this.usernameOrConnection = await Connection.create({
8287
authInfo: await AuthInfo.create({ username: this.usernameOrConnection }),
8388
});
89+
if (this.apiVersion) {
90+
this.usernameOrConnection.setApiVersion(this.apiVersion);
91+
this.logger.debug(`Overriding apiVersion to: ${this.apiVersion}`);
92+
}
8493
}
8594
return this.usernameOrConnection;
8695
}

src/collections/componentSet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
152152
const operationOptions = Object.assign({}, options, {
153153
components: this,
154154
registry: this.registry,
155+
apiVersion: this.apiVersion,
155156
});
156157

157158
return new MetadataApiDeploy(operationOptions);
@@ -166,6 +167,7 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
166167
const operationOptions = Object.assign({}, options, {
167168
components: this,
168169
registry: this.registry,
170+
apiVersion: this.apiVersion,
169171
});
170172

171173
return new MetadataApiRetrieve(operationOptions);

test/client/metadataTransfer.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,32 @@ describe('MetadataTransfer', () => {
9696
expect(operation.lifecycle.pre.firstCall.args[0]).to.equal(connection);
9797
});
9898

99+
it('should initialize a Connection with overridden apiVersion if a username is given', async () => {
100+
class TestTransferConnection extends TestTransfer {
101+
protected async pre(): Promise<{ id: string }> {
102+
const connection = await this.getConnection();
103+
return this.lifecycle.pre(connection);
104+
}
105+
}
106+
const apiVersion = '50.0';
107+
const testData = new MockTestOrgData();
108+
$$.setConfigStubContents('AuthInfoConfig', { contents: await testData.getConfig() });
109+
const authInfo = await AuthInfo.create({ username: 'foo@example.com' });
110+
env.stub(AuthInfo, 'create').withArgs({ username: 'foo@example.com' }).resolves(authInfo);
111+
env.stub(Connection, 'create').withArgs({ authInfo }).resolves(connection);
112+
const setApiVersionSpy = env.spy(Connection.prototype, 'setApiVersion');
113+
operation = new TestTransferConnection({
114+
components: new ComponentSet(),
115+
usernameOrConnection: 'foo@example.com',
116+
apiVersion,
117+
});
118+
119+
await operation.start();
120+
121+
expect(operation.lifecycle.pre.firstCall.args[0]).to.equal(connection);
122+
expect(setApiVersionSpy.calledWith(apiVersion)).to.equal(true);
123+
});
124+
99125
describe('Polling and Event Listeners', () => {
100126
let listenerStub: SinonStub;
101127

test/collections/componentSet.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,24 @@ describe('ComponentSet', () => {
422422
expect(result).to.deep.equal(expectedOperation);
423423
});
424424

425+
it('should properly construct a deploy operation with overridden apiVersion', async () => {
426+
const connection = await mockConnection($$);
427+
const apiVersion = '50.0';
428+
const set = ComponentSet.fromSource('.', { registry: mockRegistry, tree });
429+
set.apiVersion = apiVersion;
430+
const operationArgs = { components: set, usernameOrConnection: connection, apiVersion };
431+
const expectedOperation = new MetadataApiDeploy(operationArgs);
432+
const constructorStub = env
433+
.stub()
434+
.withArgs(operationArgs)
435+
.callsFake(() => expectedOperation);
436+
Object.setPrototypeOf(MetadataApiDeploy, constructorStub);
437+
438+
const result = await set.deploy({ usernameOrConnection: connection });
439+
440+
expect(result).to.deep.equal(expectedOperation);
441+
});
442+
425443
it('should throw error if there are no source backed components when deploying', async () => {
426444
const set = await ComponentSet.fromManifestFile('subset.xml', {
427445
registry: mockRegistry,
@@ -461,6 +479,32 @@ describe('ComponentSet', () => {
461479
expect(result).to.deep.equal(expectedOperation);
462480
});
463481

482+
it('should properly construct a retrieve operation with overridden apiVersion', async () => {
483+
const connection = await mockConnection($$);
484+
const apiVersion = '50.0';
485+
const set = ComponentSet.fromSource('.', { registry: mockRegistry, tree });
486+
set.apiVersion = apiVersion;
487+
const operationArgs = {
488+
apiVersion,
489+
components: set,
490+
output: join('test', 'path'),
491+
usernameOrConnection: connection,
492+
};
493+
const expectedOperation = new MetadataApiRetrieve(operationArgs);
494+
const constructorStub = env
495+
.stub()
496+
.withArgs(operationArgs)
497+
.callsFake(() => expectedOperation);
498+
Object.setPrototypeOf(MetadataApiRetrieve, constructorStub);
499+
500+
const result = await set.retrieve({
501+
output: operationArgs.output,
502+
usernameOrConnection: connection,
503+
});
504+
505+
expect(result).to.deep.equal(expectedOperation);
506+
});
507+
464508
it('should properly construct a retrieve operation with packageName', async () => {
465509
const connection = await mockConnection($$);
466510
const set = new ComponentSet([]);

0 commit comments

Comments
 (0)