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

fix: disallow undefined xml #757

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions src/utils/previewOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { filePathsFromMetadataComponent } from '@salesforce/source-deploy-retrieve/lib/src/utils/filePathGenerator';

import { SourceTracking } from '@salesforce/source-tracking';
import { isSourceComponent } from './types';
import { isSourceComponentWithXml } from './types';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'previewMessages');
Expand Down Expand Up @@ -64,7 +64,7 @@ const resolvePaths = (filenames: string[]): Array<Pick<PreviewFile, 'type' | 'fu
return undefined;
}
})
.filter(isSourceComponent)
.filter(isSourceComponentWithXml)
.map((sc) => ({ fullName: sc.fullName, type: sc.type.name, path: ensureAbsolutePath(sc.xml) }));
// dedupe by xml path
return Array.from(new Map(sourceComponents.map((sc) => [sc.path, sc])).values());
Expand Down
15 changes: 13 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,19 @@ export type Formatter<T> = {
};

/** validates source component with fullname, type, and xml props */
export const isSourceComponent = (sc: unknown): sc is SourceComponent & { xml: string } =>
isObject(sc) && 'fullName' in sc && 'type' in sc && 'xml' in sc;
export const isSourceComponent = (sc: unknown): sc is SourceComponent =>
isObject(sc) &&
'type' in sc &&
typeof sc.type === 'object' &&
sc.type !== null &&
'name' in sc.type &&
typeof sc.type.name === 'string' &&
'fullName' in sc &&
// (typeof sc.fullName === 'string' || typeof sc.fullName === 'function');
typeof sc.fullName === 'string';

export const isSourceComponentWithXml = (sc: unknown): sc is SourceComponent & { xml: string } =>
isSourceComponent(sc) && 'xml' in sc && typeof sc.xml === 'string';

export const isSdrFailure = (fileResponse: FileResponse): fileResponse is FileResponseFailure =>
fileResponse.state === ComponentStatus.Failed;
Expand Down
72 changes: 72 additions & 0 deletions test/utils/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* 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 { expect, config } from 'chai';
import { SourceComponent, RegistryAccess } from '@salesforce/source-deploy-retrieve';
import { isSourceComponent, isSourceComponentWithXml } from '../../src/utils/types';

config.truncateThreshold = 0;

const reg = new RegistryAccess();
const type = reg.getTypeByName('ApexClass');

describe('isSourceComponent (type guard)', () => {
describe('good', () => {
it('full, correct definition', () => {
expect({ fullName: 'foo', type, xml: 'fooXml', content: 'fooContent' }).to.satisfy(isSourceComponent);
});
it('SC constructed with xml', () => {
expect(new SourceComponent({ name: 'foo', type, xml: 'classes/foo.cls' })).to.satisfy(isSourceComponent);
});
it('SC constructed with no xml', () => {
const sc = new SourceComponent({ name: 'foo', type });
// console.log(sc);
// console.log(typeof sc.fullName);
expect(sc).to.satisfy(isSourceComponent);
});
});
describe('bad', () => {
it('object is undefined', () => {
expect(undefined).to.not.satisfy(isSourceComponent);
});
it('empty object', () => {
expect({}).to.not.satisfy(isSourceComponent);
});

it('object.type is set to undefined', () => {
expect({ fullName: 'foo', type: undefined, xml: 'fooXml' }).to.not.satisfy(isSourceComponent);
});
});
});

describe('isSourceComponentWithXml (type guard)', () => {
describe('good', () => {
it('full, correct definition', () => {
expect({ fullName: 'foo', type, xml: 'fooXml', content: 'fooContent' }).to.satisfy(isSourceComponentWithXml);
});
it('SC constructed with xml', () => {
expect(new SourceComponent({ name: 'foo', type, xml: 'classes/foo.cls' })).to.satisfy(isSourceComponentWithXml);
});
});
describe('bad', () => {
it('object is undefined', () => {
expect(undefined).to.not.satisfy(isSourceComponentWithXml);
});
it('empty object', () => {
expect({}).to.not.satisfy(isSourceComponentWithXml);
});
it('object.xml is undefined', () => {
expect({ fullName: 'foo', type: 'fooType', content: 'fooContent' }).to.not.satisfy(isSourceComponentWithXml);
});
it('object.type is set to undefined', () => {
expect({ fullName: 'foo', type: undefined, xml: 'fooXml' }).to.not.satisfy(isSourceComponentWithXml);
});
it('SC constructed with no xml', () => {
expect(new SourceComponent({ name: 'foo', type })).to.not.satisfy(isSourceComponentWithXml);
});
});
});
17 changes: 2 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1558,12 +1558,7 @@
dependencies:
"@types/node" "*"

"@types/semver@^7.3.12":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a"
integrity sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==

"@types/semver@^7.5.1":
"@types/semver@^7.3.12", "@types/semver@^7.5.1":
version "7.5.2"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.2.tgz#31f6eec1ed7ec23f4f05608d3a2d381df041f564"
integrity sha512-7aqorHYgdNO4DM36stTiGO3DvKoex9TQRwsJU6vMaFGyqpBA1MNZkz+PG3gaNUPpTAOYhT1WR7M1JyA3fbS9Cw==
Expand Down Expand Up @@ -6607,15 +6602,7 @@ pify@^4.0.1:
resolved "https://registry.yarnpkg.com/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231"
integrity sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==

pino-abstract-transport@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.0.0.tgz#cc0d6955fffcadb91b7b49ef220a6cc111d48bb3"
integrity sha512-c7vo5OpW4wIS42hUVcT5REsL8ZljsUfBjqV/e2sFxmFEFZiq1XLUp5EYLtuDH6PEHq9W1egWqRbnLUP5FuZmOA==
dependencies:
readable-stream "^4.0.0"
split2 "^4.0.0"

[email protected]:
pino-abstract-transport@^1.0.0, [email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.1.0.tgz#083d98f966262164504afb989bccd05f665937a8"
integrity sha512-lsleG3/2a/JIWUtf9Q5gUNErBqwIu1tUKTT3dUzaf5DySw9ra1wcqKjJjLX1VTY64Wk1eEOYsVGSaGfCK85ekA==
Expand Down
Loading