Skip to content

Commit

Permalink
fix: ignore duplicate components in server response (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel authored and AnanyaJha committed Jul 28, 2021
1 parent 63d45c0 commit 734b956
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/convert/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { j2xParser } from 'fast-xml-parser';
import { ComponentSet } from '../collections';
import { LibraryError } from '../errors';
import { RegistryAccess } from '../registry';
import { Logger } from '@salesforce/core';
export const pipeline = promisify(cbPipeline);

export class ComponentReader extends Readable {
Expand Down Expand Up @@ -131,10 +132,12 @@ export abstract class ComponentWriter extends Writable {
export class StandardWriter extends ComponentWriter {
public converted: SourceComponent[] = [];
private resolver: MetadataResolver;
private logger: Logger;

constructor(rootDestination: SourcePath, resolver = new MetadataResolver()) {
super(rootDestination);
this.resolver = resolver;
this.logger = Logger.childFromRoot(this.constructor.name);
}

public async _write(
Expand All @@ -152,6 +155,12 @@ export class StandardWriter extends ComponentWriter {
: join(this.rootDestination, info.output);
// if there are children, resolve each file. o/w just pick one of the files to resolve
if (toResolve.length === 0 || chunk.component.type.children) {
// This is a workaround for a server side ListViews bug where
// duplicate components are sent. W-9614275
if (toResolve.includes(fullDest)) {
this.logger.debug(`Ignoring duplicate metadata for: ${fullDest}`);
return;
}
toResolve.push(fullDest);
}
ensureFileExists(fullDest);
Expand Down
43 changes: 43 additions & 0 deletions test/convert/streams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as fs from 'fs';
import * as archiver from 'archiver';
import { Logger } from '@salesforce/core';
import * as streams from '../../src/convert/streams';
import * as fsUtil from '../../src/utils/fileSystemHandler';
import { expect } from 'chai';
Expand Down Expand Up @@ -371,6 +372,48 @@ describe('Streams', () => {
expect(resolverSpy.notCalled).to.be.true;
});
});

it('should skip duplicate components in WriteInfos', async () => {
pipelineStub.resolves();
const loggerStub = env.stub(Logger.prototype, 'debug');

const dupedComponent = SourceComponent.createVirtualComponent(COMPONENT, [
{
dirPath: TYPE_DIRECTORY,
children: XML_NAMES.concat(CONTENT_NAMES),
},
{
dirPath: join(rootDestination, COMPONENT.type.directoryName),
children: [basename(COMPONENT.xml), basename(COMPONENT.content)],
},
{
dirPath: join(absoluteRootDestination, COMPONENT.type.directoryName),
children: [basename(COMPONENT.xml), basename(COMPONENT.content)],
},
]);
dupedComponent.type.children = mockRegistry.getTypeByName('decomposed').children;

const compWriteInfo: WriteInfo = {
output: dupedComponent.getPackageRelativePath(component.xml, 'metadata'),
source: readableMock,
};
const chunkWithDupe: WriterFormat = {
component: dupedComponent,
writeInfos: [compWriteInfo, compWriteInfo],
};

// The chunk has 2 identical components. The dupe should be
// ignored so that it only writes once to compensate for W-9614275
await writer._write(chunkWithDupe, '', (err: Error) => {
expect(err).to.be.undefined;
expect(ensureFile.calledOnce).to.be.true;
expect(pipelineStub.calledOnce).to.be.true;
expect(loggerStub.calledOnce).to.be.true;
const fullDest = join(rootDestination, compWriteInfo.output);
const expectedLogMsg = `Ignoring duplicate metadata for: ${fullDest}`;
expect(loggerStub.firstCall.args[0]).to.equal(expectedLogMsg);
});
});
});

describe('ZipWriter', () => {
Expand Down

0 comments on commit 734b956

Please sign in to comment.