Skip to content

Commit

Permalink
fix: path resolution for master detail fields (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Mar 16, 2022
1 parent 4da80a6 commit 5aa12c1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 36 deletions.
14 changes: 9 additions & 5 deletions __mocks__/fs-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 14 additions & 24 deletions __tests__/unit/lib/service/customObjectHandler.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
})
})
2 changes: 1 addition & 1 deletion src/service/botHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions src/service/customObjectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const readFileOptions = {

class CustomObjectHandler extends StandardHandler {
async handleAddition() {
super.handleAddition()
await super.handleAddition()
if (!this.config.generateDelta) return
await this._handleMasterDetailException()
}

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)
)
Expand Down
4 changes: 2 additions & 2 deletions src/service/inFolderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ''
Expand Down
2 changes: 1 addition & 1 deletion src/service/inResourceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/service/subCustomObjectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5aa12c1

Please sign in to comment.