From 5aa12c1573a79aadb3495083d217d0cbb28e24d3 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 16 Mar 2022 16:39:22 +0100 Subject: [PATCH] fix: path resolution for master detail fields (#264) --- __mocks__/fs-extra.js | 14 ++++--- .../lib/service/customObjectHandler.test.js | 38 +++++++------------ src/service/botHandler.js | 2 +- src/service/customObjectHandler.js | 4 +- src/service/inFolderHandler.js | 4 +- src/service/inResourceHandler.js | 2 +- src/service/subCustomObjectHandler.js | 2 +- 7 files changed, 30 insertions(+), 36 deletions(-) diff --git a/__mocks__/fs-extra.js b/__mocks__/fs-extra.js index 7ff79a2f..c464849c 100644 --- a/__mocks__/fs-extra.js +++ b/__mocks__/fs-extra.js @@ -5,12 +5,16 @@ fse.errorMode = false fse.outputFileError = false fse.pathShouldExist = true -fse.pathExists = () => Promise.resolve(fse.pathShouldExist) -fse.copy = () => (fse.errorMode ? Promise.reject() : Promise.resolve()) -fse.copySync = () => { +fse.pathExists = jest.fn(() => Promise.resolve(fse.pathShouldExist)) +fse.copy = jest.fn(() => { + if (fse.errorMode) return Promise.reject() + return Promise.resolve() +}) +fse.copySync = jest.fn(() => { if (fse.errorMode) throw new Error() -} -fse.outputFile = () => +}) +fse.outputFile = jest.fn(() => fse.outputFileError ? Promise.reject() : Promise.resolve() +) module.exports = fse diff --git a/__tests__/unit/lib/service/customObjectHandler.test.js b/__tests__/unit/lib/service/customObjectHandler.test.js index cab208c2..d9e7d88d 100644 --- a/__tests__/unit/lib/service/customObjectHandler.test.js +++ b/__tests__/unit/lib/service/customObjectHandler.test.js @@ -1,8 +1,10 @@ 'use strict' const CustomObjectHandler = require('../../../../src/service/customObjectHandler') -const SubCustomObjectHandler = require('../../../../src/service/subCustomObjectHandler') const metadataManager = require('../../../../src/metadata/metadataManager') +const { MASTER_DETAIL_TAG } = require('../../../../src/utils/metadataConstants') +const { copy } = require('fs-extra') jest.mock('fs') +jest.mock('fs-extra') const testContext = { handler: CustomObjectHandler, @@ -32,58 +34,46 @@ const testContext = { // eslint-disable-next-line no-undef describe('test CustomObjectHandler with fields', () => { let globalMetadata - beforeAll(async () => { + beforeEach(async () => { + jest.clearAllMocks() require('fs').__setMockFiles({ 'force-app/main/default/objects/Account/Account.object-meta.xml': 'test', 'force-app/main/default/objects/Account/fields': '', 'force-app/main/default/objects/Account/fields/test__c.field-meta.xml': - SubCustomObjectHandler.MASTER_DETAIL_TAG, + MASTER_DETAIL_TAG, 'force-app/main/default/objects/Test/Account/Account.object-meta.xml': 'test', - 'force-app/main/default/objects/Test/Account/fields/test__c.field-meta.xml': - SubCustomObjectHandler.MASTER_DETAIL_TAG, + 'force-app/main/default/objects/Test/Account/fields/no': + MASTER_DETAIL_TAG, }) globalMetadata = await metadataManager.getDefinition('directoryName', 50) }) - // eslint-disable-next-line no-undef - testHandlerHelper(testContext) - - test('addition', () => { - testContext.work.config.generateDelta = false + test('addition and masterdetail fields', async () => { const handler = new testContext.handler( 'A force-app/main/default/objects/Account/Account.object-meta.xml', 'objects', testContext.work, globalMetadata ) - handler.handle() + await handler.handle() + expect(copy).toBeCalled() }) + + // eslint-disable-next-line no-undef + testHandlerHelper(testContext) }) // eslint-disable-next-line no-undef describe('test CustomObjectHandler without fields', () => { - let globalMetadata beforeAll(async () => { require('fs').__setMockFiles({ 'force-app/main/default/objects/Account/Account.object-meta.xml': 'test', 'force-app/main/default/objects/Test/Account/Account.object-meta.xml': 'test', }) - globalMetadata = await metadataManager.getDefinition('directoryName', 50) }) // eslint-disable-next-line no-undef testHandlerHelper(testContext) - - test('addition', () => { - testContext.work.config.generateDelta = false - const handler = new testContext.handler( - 'A force-app/main/default/objects/Account/Account.object-meta.xml', - 'objects', - testContext.work, - globalMetadata - ) - handler.handle() - }) }) diff --git a/src/service/botHandler.js b/src/service/botHandler.js index d03bbfb8..48938d06 100644 --- a/src/service/botHandler.js +++ b/src/service/botHandler.js @@ -15,7 +15,7 @@ class BotHandler extends WaveHandler { return [...elementName].join('.') } async handleAddition() { - super.handleAddition() + await super.handleAddition() const botName = this._getParsedPath().dir.split(sep).pop() this._fillPackageWithParameter({ diff --git a/src/service/customObjectHandler.js b/src/service/customObjectHandler.js index a016fb61..537fe070 100644 --- a/src/service/customObjectHandler.js +++ b/src/service/customObjectHandler.js @@ -16,7 +16,7 @@ const readFileOptions = { class CustomObjectHandler extends StandardHandler { async handleAddition() { - super.handleAddition() + await super.handleAddition() if (!this.config.generateDelta) return await this._handleMasterDetailException() } @@ -24,7 +24,7 @@ class CustomObjectHandler extends StandardHandler { async _handleMasterDetailException() { if (this.type !== CustomObjectHandler.OBJECT_TYPE) return - const fieldsFolder = resolve( + const fieldsFolder = join( this.config.repo, join(parse(this.line).dir, FIELD_DIRECTORY_NAME) ) diff --git a/src/service/inFolderHandler.js b/src/service/inFolderHandler.js index d9f16421..fddc4c44 100644 --- a/src/service/inFolderHandler.js +++ b/src/service/inFolderHandler.js @@ -10,8 +10,8 @@ const { join, normalize, sep } = require('path') const INFOLDER_SUFFIX_REGEX = new RegExp(`${INFOLDER_SUFFIX}$`) const EXTENSION_SUFFIX_REGEX = new RegExp(/\.[^/.]+$/) class InFolderHandler extends StandardHandler { - handleAddition() { - super.handleAddition() + async handleAddition() { + await super.handleAddition() if (!this.config.generateDelta) return const regexRepo = this.config.repo !== '.' ? this.config.repo : '' diff --git a/src/service/inResourceHandler.js b/src/service/inResourceHandler.js index 075095fa..4206b9e3 100644 --- a/src/service/inResourceHandler.js +++ b/src/service/inResourceHandler.js @@ -13,7 +13,7 @@ class ResourceHandler extends StandardHandler { super(line, type, work, metadata) } async handleAddition() { - super.handleAddition() + await super.handleAddition() if (!this.config.generateDelta) return const [, srcPath, elementName] = this._parseLine() const [targetPath] = `${join(this.config.output, this.line)}`.match( diff --git a/src/service/subCustomObjectHandler.js b/src/service/subCustomObjectHandler.js index bda1039b..5d7c0fbe 100644 --- a/src/service/subCustomObjectHandler.js +++ b/src/service/subCustomObjectHandler.js @@ -12,7 +12,7 @@ class SubCustomObjectHandler extends StandardHandler { } async handleAddition() { - super.handleAddition() + await super.handleAddition() if (!this.config.generateDelta) return const data = await this._readFile()