From f69f169813fe4c7d017d18d57815f50c765ac311 Mon Sep 17 00:00:00 2001 From: Willie Ruemmele Date: Wed, 9 Nov 2022 12:42:17 -0700 Subject: [PATCH] fix: clone sfdx-project.json deps to avoid writing back to the file (#117) * fix: clonse sfdx-project.json deps to avoid writing back to the file * chore: node 16 way of copying * chore: change to cloneJson from kit * chore: guard against no deps when cloning * fix: clone entry to include unpackagedMetadata entry, add UT * chore: clone at top level only --- package.json | 2 +- src/package/packageVersionCreate.ts | 5 ++-- test/package/packageVersionCreate.test.ts | 35 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4eaac4e18..333d6bd6c 100644 --- a/package.json +++ b/package.json @@ -90,4 +90,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/src/package/packageVersionCreate.ts b/src/package/packageVersionCreate.ts index b7081d596..c5f3365ec 100644 --- a/src/package/packageVersionCreate.ts +++ b/src/package/packageVersionCreate.ts @@ -23,6 +23,7 @@ import { ComponentSetBuilder, ConvertResult, MetadataConverter } from '@salesfor import SettingsGenerator from '@salesforce/core/lib/org/scratchOrgSettingsGenerator'; import * as xml2js from 'xml2js'; import { PackageDirDependency } from '@salesforce/core/lib/sfProject'; +import { cloneJson } from '@salesforce/kit'; import * as pkgUtils from '../utils/packageUtils'; import { MDFolderForArtifactOptions, @@ -327,7 +328,7 @@ export class PackageVersionCreate { // Copy all the metadata from the workspace to a tmp folder const componentSet = await this.generateMDFolderForArtifact(mdOptions); this.verifyHasSource(componentSet); - const packageDescriptorJson = this.packageObject as PackageDescriptorJson; + const packageDescriptorJson = cloneJson(this.packageObject) as PackageDescriptorJson; if (packageDescriptorJson.package) { delete packageDescriptorJson.package; @@ -562,7 +563,7 @@ export class PackageVersionCreate { codeCoverage: boolean ): Promise { // Add the Unpackaged Metadata, if any, to the output directory, only when code coverage is specified - if (codeCoverage && packageDescriptorJson.unpackagedMetadata && packageDescriptorJson.unpackagedMetadata.path) { + if (codeCoverage && packageDescriptorJson.unpackagedMetadata?.path) { const unpackagedPath = path.join(process.cwd(), packageDescriptorJson.unpackagedMetadata.path); if (!fs.existsSync(unpackagedPath)) { throw messages.createError('unpackagedMDDirectoryDoesNotExist', [ diff --git a/test/package/packageVersionCreate.test.ts b/test/package/packageVersionCreate.test.ts index 50fa21db4..8eab514de 100644 --- a/test/package/packageVersionCreate.test.ts +++ b/test/package/packageVersionCreate.test.ts @@ -31,6 +31,14 @@ describe('Package Version Create', () => { project = SfProject.getInstance(); await project.getSfProjectJson().write({ packageDirectories: [ + { + path: 'pkg', + package: 'dep', + versionName: 'ver 0.1', + versionNumber: '0.1.0.NEXT', + default: false, + name: 'pkg', + }, { path: 'force-app', package: 'TEST', @@ -38,11 +46,21 @@ describe('Package Version Create', () => { versionNumber: '0.1.0.NEXT', default: true, ancestorId: 'TEST2', + unpackagedMetadata: { + path: 'unpackaged', + }, + dependencies: [ + { + package: 'DEP@0.1.0-1', + }, + ], }, ], packageAliases: { TEST: packageId, TEST2: '05i3i000000Gmj6XXX', + DEP: '05i3i000000Gmj6XXX', + 'DEP@0.1.0-1': '04t3i000002eyYXXXX', }, }); await fs.promises.mkdir(path.join(project.getPath(), 'force-app')); @@ -117,10 +135,22 @@ describe('Package Version Create', () => { 'SubscriberPackageVersionId', 'Tag' ); + + expect(project.getSfProjectJson().getContents().packageDirectories[1].dependencies).to.deep.equal([ + { + package: 'DEP@0.1.0-1', + }, + ]); }); it('should create the package version create request with codecoverage=true', async () => { const pvc = new PackageVersionCreate({ connection, project, codecoverage: true, packageId }); + // @ts-ignore + const hasUnpackagedMdSpy = $$.SANDBOX.spy(pvc, 'resolveUnpackagedMetadata'); + // @ts-ignore + $$.SANDBOX.stub(pvc, 'generateMDFolderForArtifact').resolves(); + $$.SANDBOX.stub(fs, 'existsSync').returns(true); + $$.SANDBOX.stub(fs.promises, 'readFile').resolves(); const result = await pvc.createPackageVersion(); expect(packageCreateStub.firstCall.args[1].CalculateCodeCoverage).to.equal(true); expect(result).to.have.all.keys( @@ -136,6 +166,11 @@ describe('Package Version Create', () => { 'SubscriberPackageVersionId', 'Tag' ); + const unpackagedMD = (hasUnpackagedMdSpy.firstCall.args.at(0) as { unpackagedMetadata: Record }) + .unpackagedMetadata; + expect(unpackagedMD).to.deep.equal({ + path: 'unpackaged', + }); }); it('should create the package version create request with codecoverage=false', async () => {