Skip to content

fix: set an overridden apiVersion on a created connection #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/client/metadataTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AuthInfo, Connection } from '@salesforce/core';
import { AuthInfo, Connection, Logger } from '@salesforce/core';
import { EventEmitter } from 'events';
import { ComponentSet } from '../collections';
import { MetadataTransferError } from '../errors';
Expand All @@ -13,20 +13,25 @@ import { MetadataRequestStatus, RequestStatus, MetadataTransferResult } from './
export interface MetadataTransferOptions {
usernameOrConnection: string | Connection;
components: ComponentSet;
apiVersion?: string;
}

export abstract class MetadataTransfer<
Status extends MetadataRequestStatus,
Result extends MetadataTransferResult
> {
protected components: ComponentSet;
protected logger: Logger;
private signalCancel = false;
private event = new EventEmitter();
private usernameOrConnection: string | Connection;
private apiVersion: string;

constructor({ usernameOrConnection, components }: MetadataTransferOptions) {
constructor({ usernameOrConnection, components, apiVersion }: MetadataTransferOptions) {
this.usernameOrConnection = usernameOrConnection;
this.components = components;
this.apiVersion = apiVersion;
this.logger = Logger.childFromRoot(this.constructor.name);
}

/**
Expand Down Expand Up @@ -81,6 +86,10 @@ export abstract class MetadataTransfer<
this.usernameOrConnection = await Connection.create({
authInfo: await AuthInfo.create({ username: this.usernameOrConnection }),
});
if (this.apiVersion) {
this.usernameOrConnection.setApiVersion(this.apiVersion);
this.logger.debug(`Overriding apiVersion to: ${this.apiVersion}`);
}
}
return this.usernameOrConnection;
}
Expand Down
2 changes: 2 additions & 0 deletions src/collections/componentSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
const operationOptions = Object.assign({}, options, {
components: this,
registry: this.registry,
apiVersion: this.apiVersion,
});

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

return new MetadataApiRetrieve(operationOptions);
Expand Down
26 changes: 26 additions & 0 deletions test/client/metadataTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ describe('MetadataTransfer', () => {
expect(operation.lifecycle.pre.firstCall.args[0]).to.equal(connection);
});

it('should initialize a Connection with overridden apiVersion if a username is given', async () => {
class TestTransferConnection extends TestTransfer {
protected async pre(): Promise<{ id: string }> {
const connection = await this.getConnection();
return this.lifecycle.pre(connection);
}
}
const apiVersion = '50.0';
const testData = new MockTestOrgData();
$$.setConfigStubContents('AuthInfoConfig', { contents: await testData.getConfig() });
const authInfo = await AuthInfo.create({ username: 'foo@example.com' });
env.stub(AuthInfo, 'create').withArgs({ username: 'foo@example.com' }).resolves(authInfo);
env.stub(Connection, 'create').withArgs({ authInfo }).resolves(connection);
const setApiVersionSpy = env.spy(Connection.prototype, 'setApiVersion');
operation = new TestTransferConnection({
components: new ComponentSet(),
usernameOrConnection: 'foo@example.com',
apiVersion,
});

await operation.start();

expect(operation.lifecycle.pre.firstCall.args[0]).to.equal(connection);
expect(setApiVersionSpy.calledWith(apiVersion)).to.equal(true);
});

describe('Polling and Event Listeners', () => {
let listenerStub: SinonStub;

Expand Down
44 changes: 44 additions & 0 deletions test/collections/componentSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ describe('ComponentSet', () => {
expect(result).to.deep.equal(expectedOperation);
});

it('should properly construct a deploy operation with overridden apiVersion', async () => {
const connection = await mockConnection($$);
const apiVersion = '50.0';
const set = ComponentSet.fromSource('.', { registry: mockRegistry, tree });
set.apiVersion = apiVersion;
const operationArgs = { components: set, usernameOrConnection: connection, apiVersion };
const expectedOperation = new MetadataApiDeploy(operationArgs);
const constructorStub = env
.stub()
.withArgs(operationArgs)
.callsFake(() => expectedOperation);
Object.setPrototypeOf(MetadataApiDeploy, constructorStub);

const result = await set.deploy({ usernameOrConnection: connection });

expect(result).to.deep.equal(expectedOperation);
});

it('should throw error if there are no source backed components when deploying', async () => {
const set = await ComponentSet.fromManifestFile('subset.xml', {
registry: mockRegistry,
Expand Down Expand Up @@ -461,6 +479,32 @@ describe('ComponentSet', () => {
expect(result).to.deep.equal(expectedOperation);
});

it('should properly construct a retrieve operation with overridden apiVersion', async () => {
const connection = await mockConnection($$);
const apiVersion = '50.0';
const set = ComponentSet.fromSource('.', { registry: mockRegistry, tree });
set.apiVersion = apiVersion;
const operationArgs = {
apiVersion,
components: set,
output: join('test', 'path'),
usernameOrConnection: connection,
};
const expectedOperation = new MetadataApiRetrieve(operationArgs);
const constructorStub = env
.stub()
.withArgs(operationArgs)
.callsFake(() => expectedOperation);
Object.setPrototypeOf(MetadataApiRetrieve, constructorStub);

const result = await set.retrieve({
output: operationArgs.output,
usernameOrConnection: connection,
});

expect(result).to.deep.equal(expectedOperation);
});

it('should properly construct a retrieve operation with packageName', async () => {
const connection = await mockConnection($$);
const set = new ComponentSet([]);
Expand Down