Skip to content
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

refactor: reduce configFile mutation #485

Merged
merged 2 commits into from
Oct 16, 2023
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@
"node": ">=16.0.0"
},
"dependencies": {
"@salesforce/core": "^5.3.3",
"@salesforce/core": "^5.3.9",
"@salesforce/kit": "^3.0.13",
"@salesforce/source-deploy-retrieve": "^9.7.20",
"@salesforce/source-deploy-retrieve": "^9.7.24",
"@salesforce/ts-types": "^2.0.8",
"fast-xml-parser": "^4.2.5",
"graceful-fs": "^4.2.11",
"isomorphic-git": "1.23.0",
"ts-retry-promise": "^0.7.0"
},
"devDependencies": {
"@salesforce/cli-plugins-testkit": "^4.4.5",
"@salesforce/cli-plugins-testkit": "^4.4.7",
"@salesforce/dev-config": "^4.0.1",
"@salesforce/dev-scripts": "^5.10.0",
"@salesforce/dev-scripts": "^5.11.0",
"@salesforce/prettier-config": "^0.0.3",
"@salesforce/ts-sinon": "^1.4.15",
"@types/graceful-fs": "^4.1.6",
"@types/shelljs": "^0.8.11",
"@typescript-eslint/eslint-plugin": "^5.60.0",
"@typescript-eslint/parser": "^5.62.0",
"chai": "^4.3.7",
"chai": "^4.3.8",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-config-salesforce": "^2.0.1",
Expand Down
18 changes: 8 additions & 10 deletions src/shared/remoteSourceTrackingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,17 @@ export class RemoteSourceTrackingService extends ConfigFile<RemoteSourceTracking
throw SfError.wrap(err as Error);
}

const contents = this.getContents();
// Initialize a new maxRevision.json if the file doesn't yet exist.
if (!contents.serverMaxRevisionCounter && !contents.sourceMembers) {
if (!this.has('serverMaxRevisionCounter') && !this.has('sourceMembers')) {
try {
// To find out if the associated org has source tracking enabled, we need to make a query
// for SourceMembers. If a certain error is thrown during the query we won't try to do
// source tracking for this org. Calling querySourceMembersFrom() has the extra benefit
// of caching the query so we don't have to make an identical request in the same process.
await this.querySourceMembersFrom({ fromRevision: 0 });
this.set('sourceMembers', {});
this.set('serverMaxRevisionCounter', 0);

this.logger.debug('Initializing source tracking state');
contents.serverMaxRevisionCounter = 0;
contents.sourceMembers = {};
await this.write();
} catch (e) {
if (
Expand Down Expand Up @@ -483,19 +481,19 @@ export class RemoteSourceTrackingService extends ConfigFile<RemoteSourceTracking
//

private getServerMaxRevision(): number {
return this.getContents().serverMaxRevisionCounter;
return this.get('serverMaxRevisionCounter') ?? 0;
}

private setServerMaxRevision(revision = 0): void {
(this['contents'] as Contents).serverMaxRevisionCounter = revision;
this.set('serverMaxRevisionCounter', revision);
}

private getSourceMembers(): Dictionary<MemberRevision> {
return this.getContents().sourceMembers;
return this.get('sourceMembers');
}

private initSourceMembers(): void {
this.getContents().sourceMembers = {};
this.set('sourceMembers', {});
}

// Return a tracked element as MemberRevision data.
Expand All @@ -511,7 +509,7 @@ export class RemoteSourceTrackingService extends ConfigFile<RemoteSourceTracking
const matchingKey = sourceMembers[key]
? key
: getDecodedKeyIfSourceMembersHas({ sourceMembers, key, logger: this.logger });
this.getContents().sourceMembers[matchingKey] = sourceMember;
this.set('sourceMembers', { ...sourceMembers, [matchingKey]: sourceMember });
}

private calculateTimeout(memberCount: number): Duration {
Expand Down
33 changes: 13 additions & 20 deletions test/unit/remoteSourceTracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,23 @@ describe('remoteSourceTrackingService', () => {

describe('getServerMaxRevision', () => {
it('should return 0 if file does not exist', () => {
// @ts-ignore
// @ts-expect-error it's private
const max = remoteSourceTrackingService.getServerMaxRevision();
expect(max).to.equal(0);
});
});

describe('init', () => {
it('should set initial state of contents', async () => {
$$.SANDBOX.stub(remoteSourceTrackingService, 'getContents').returns({
// @ts-ignore
serverMaxRevisionCounter: null,
// @ts-ignore
sourceMembers: null,
});
// @ts-ignore
// @ts-expect-error it's private
const queryMembersFromSpy = $$.SANDBOX.spy(remoteSourceTrackingService, 'querySourceMembersFrom');
await remoteSourceTrackingService.init();
// @ts-ignore
// @ts-expect-error it's private
expect(remoteSourceTrackingService.getServerMaxRevision()).to.equal(0);
// @ts-ignore
// @ts-expect-error it's private
expect(remoteSourceTrackingService.getSourceMembers()).to.deep.equal({});
expect(queryMembersFromSpy.called).to.equal(true);
// this is run during the beforeEach, but doesn't run again because init already happened
expect(queryMembersFromSpy.called).to.equal(false);
});
});

Expand All @@ -100,11 +95,13 @@ describe('remoteSourceTrackingService', () => {
memberType: 'CustomObject',
serverRevisionCounter: 2,
lastRetrievedFromServer: 3,
isNameObsolete: false,
},
ApexClass__abc: {
memberType: 'ApexClass',
serverRevisionCounter: 2,
lastRetrievedFromServer: 2,
isNameObsolete: false,
},
},
};
Expand Down Expand Up @@ -319,7 +316,7 @@ describe('remoteSourceTrackingService', () => {
describe('setServerMaxRevision', () => {
it('should set the initial serverMaxRevisionCounter to zero during file creation', async () => {
await remoteSourceTrackingService.init();
const contents = await remoteSourceTrackingService.getContents();
const contents = remoteSourceTrackingService.getContents();
expect(contents.serverMaxRevisionCounter).to.equal(0);
expect(contents.sourceMembers).to.eql({});
});
Expand Down Expand Up @@ -470,10 +467,8 @@ describe('remoteSourceTrackingService', () => {
describe('reset', () => {
it('should reset source tracking state to be synced with the max RevisionCounter on the org', async () => {
// Set initial test state of 5 apex classes not yet synced.
remoteSourceTrackingService['contents'] = {
serverMaxRevisionCounter: 5,
sourceMembers: getMemberRevisionEntries(5),
};
remoteSourceTrackingService.set('serverMaxRevisionCounter', 5);
remoteSourceTrackingService.set('sourceMembers', getMemberRevisionEntries(5));

// @ts-ignore
const setMaxSpy = $$.SANDBOX.spy(remoteSourceTrackingService, 'setServerMaxRevision');
Expand All @@ -500,10 +495,8 @@ describe('remoteSourceTrackingService', () => {

it('should reset source tracking state to be synced with the specified revision', async () => {
// Set initial test state of 5 apex classes not yet synced.
remoteSourceTrackingService['contents'] = {
serverMaxRevisionCounter: 5,
sourceMembers: getMemberRevisionEntries(5),
};
remoteSourceTrackingService.set('serverMaxRevisionCounter', 5);
remoteSourceTrackingService.set('sourceMembers', getMemberRevisionEntries(5));

// @ts-ignore
const setMaxSpy = $$.SANDBOX.spy(remoteSourceTrackingService, 'setServerMaxRevision');
Expand Down
Loading