Skip to content

Commit

Permalink
feat: validate manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed May 31, 2023
1 parent 098fb90 commit 1f5863b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/resolve/manifestResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { XMLParser } from 'fast-xml-parser';
import { XMLParser, XMLValidator } from 'fast-xml-parser';
import { ensureArray } from '@salesforce/kit';
import { SfError } from '@salesforce/core';
import { MetadataType, RegistryAccess } from '../registry';
import { NodeFSTreeContainer, TreeContainer } from './treeContainers';
import { MetadataComponent } from './types';
Expand Down Expand Up @@ -52,15 +53,22 @@ export class ManifestResolver {
public async resolve(manifestPath: string): Promise<ResolveManifestResult> {
const components: MetadataComponent[] = [];

const file = await this.tree.readFile(manifestPath);

const file = (await this.tree.readFile(manifestPath)).toString();
const validateResult = XMLValidator.validate(file);
if (validateResult !== true) {
const error = new SfError(
`Invalid manifest file: ${manifestPath}. ${validateResult.err.code}: ${validateResult.err.msg} (Line ${validateResult.err.line} Column ${validateResult.err.col})`,
'InvalidManifest'
);
error.setData(validateResult.err);
throw error;
}
const parser = new XMLParser({
stopNodes: ['version'],
// In order to preserve the .0 on the apiVersion skip parsing it
numberParseOptions: { leadingZeros: false, hex: false, skipLike: /\.0$/ },
});
const parsedManifest: ParsedPackageManifest = (parser.parse(String(file)) as { Package: ParsedPackageManifest })
.Package;
const parsedManifest: ParsedPackageManifest = (parser.parse(file) as { Package: ParsedPackageManifest }).Package;
const packageTypeMembers = ensureArray(parsedManifest.types);
const apiVersion = parsedManifest.version;

Expand Down
29 changes: 28 additions & 1 deletion test/resolve/manifestResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { expect } from 'chai';
import { assert, expect } from 'chai';
import { createSandbox } from 'sinon';
import {
ManifestResolver,
Expand Down Expand Up @@ -80,6 +80,33 @@ describe('ManifestResolver', () => {
expect(result.components).to.deep.equal(expected);
});

it('should throw on invalid xml', async () => {
const badManifest: VirtualFile = {
name: 'bad-package.xml',
data: Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<<types>
<members></members>
<name></name>
</types>
<version>52.0</version>
</Package>\n`),
};
const tree = new VirtualTreeContainer([
{
dirPath: '.',
children: [badManifest],
},
]);
const resolver = new ManifestResolver(tree);
try {
await resolver.resolve(badManifest.name);
expect(true, 'expected invalid types definition error').to.be.false;
} catch (error) {
assert(error instanceof Error);
expect(error.name).to.equal('InvalidManifest');
}
});
it('should throw when type is empty', async () => {
const badManifest: VirtualFile = {
name: 'bad-package.xml',
Expand Down

2 comments on commit 1f5863b

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 1f5863b Previous: 6c3e10b Ratio
eda-componentSetCreate-linux 230 ms 234 ms 0.98
eda-sourceToMdapi-linux 6183 ms 5674 ms 1.09
eda-sourceToZip-linux 5803 ms 4884 ms 1.19
eda-mdapiToSource-linux 4022 ms 4069 ms 0.99
lotsOfClasses-componentSetCreate-linux 464 ms 482 ms 0.96
lotsOfClasses-sourceToMdapi-linux 9751 ms 9166 ms 1.06
lotsOfClasses-sourceToZip-linux 7390 ms 7809 ms 0.95
lotsOfClasses-mdapiToSource-linux 4761 ms 4696 ms 1.01
lotsOfClassesOneDir-componentSetCreate-linux 795 ms 803 ms 0.99
lotsOfClassesOneDir-sourceToMdapi-linux 12633 ms 13246 ms 0.95
lotsOfClassesOneDir-sourceToZip-linux 12546 ms 13280 ms 0.94
lotsOfClassesOneDir-mdapiToSource-linux 8442 ms 8962 ms 0.94

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 1f5863b Previous: 6c3e10b Ratio
eda-componentSetCreate-win32 700 ms 674 ms 1.04
eda-sourceToMdapi-win32 12126 ms 11472 ms 1.06
eda-sourceToZip-win32 9489 ms 8901 ms 1.07
eda-mdapiToSource-win32 12742 ms 12224 ms 1.04
lotsOfClasses-componentSetCreate-win32 1659 ms 1513 ms 1.10
lotsOfClasses-sourceToMdapi-win32 17682 ms 16679 ms 1.06
lotsOfClasses-sourceToZip-win32 11862 ms 11314 ms 1.05
lotsOfClasses-mdapiToSource-win32 14140 ms 13902 ms 1.02
lotsOfClassesOneDir-componentSetCreate-win32 2728 ms 2654 ms 1.03
lotsOfClassesOneDir-sourceToMdapi-win32 29190 ms 28346 ms 1.03
lotsOfClassesOneDir-sourceToZip-win32 19534 ms 18476 ms 1.06
lotsOfClassesOneDir-mdapiToSource-win32 25699 ms 25242 ms 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.