Skip to content

Commit

Permalink
feat: new default api version definition algorithm (#372)
Browse files Browse the repository at this point in the history
Co-authored-by: Mehdi Cherfaoui <[email protected]>
  • Loading branch information
scolladon and mehdicherf authored Oct 21, 2022
1 parent aed25c5 commit e58d63a
Show file tree
Hide file tree
Showing 24 changed files with 964 additions and 725 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ OPTIONS
-W, --ignore-whitespace ignore git diff whitespace (space,
tab, eol) changes
-a, --api-version=api-version [default: 55] salesforce API version
-a, --api-version=api-version salesforce metadata API version,
default to sfdx-project.json
"sourceApiVersion" attribut or
latest version
-d, --generate-delta generate delta files in [--output]
folder
Expand Down Expand Up @@ -461,7 +464,7 @@ fi

### Use the module in your own node application

If you want to embed sgd in your node application, install it has a dependency for your application
If you want to embed sgd in your node application, install it as a dependency for your application

```sh
yarn add sfdx-git-delta
Expand Down
6 changes: 4 additions & 2 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const path = require('path')
const fs = jest.genMockFromModule('fs')
const fs = jest.createMockFromModule('fs')
const { MASTER_DETAIL_TAG } = require('../src/utils/metadataConstants')

fs.errorMode = false
Expand All @@ -23,7 +23,9 @@ fs.__setMockFiles = newMockFiles => {
}
}

fs.promises = {}
Object.defineProperty(fs, 'promises', {
value: {},
})

fs.promises.stat = jest.fn(
elem =>
Expand Down
15 changes: 13 additions & 2 deletions __tests__/__utils__/testHandlerHelper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
'use strict'
const metadataManager = require('../../src/metadata/metadataManager')
const {
getDefinition,
getLatestSupportedVersion,
} = require('../../src/metadata/metadataManager')

const getMetadata = async () => {
const apiVersion = await getLatestSupportedVersion()
const metadata = await getDefinition('directoryName', apiVersion)
return metadata
}

global.getGlobalMetadata = getMetadata

global.testHandlerHelper = testContext => {
describe(`test if ${testContext.handler.name}`, () => {
let globalMetadata
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
globalMetadata = await getMetadata()
})
describe.each(testContext.testData)(
'handles',
Expand Down
34 changes: 30 additions & 4 deletions __tests__/unit/lib/metadata/metadataManager.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
'use strict'
const metadataManager = require('../../../../src/metadata/metadataManager')
const {
getDefinition,
getLatestSupportedVersion,
isVersionSupported,
} = require('../../../../src/metadata/metadataManager')

describe(`test if metadata`, () => {
test('when grouped per directoryName, have classes', async () => {
const metadata = await metadataManager.getDefinition('directoryName')
const metadata = await getDefinition('directoryName')
expect(metadata.get('classes')).toBeDefined()
})

test('when grouped per directoryName, do not have do not exist', async () => {
let metadata = await metadataManager.getDefinition('directoryName', '48')
metadata = await metadataManager.getDefinition('directoryName', '46')
let metadata = await getDefinition('directoryName', '48')
metadata = await getDefinition('directoryName', '46')
expect(metadata).toBeDefined()
expect(metadata.get('do not exist')).toBeFalsy()
})

test('getLatestSupportedVersion', async () => {
let latestVersion = await getLatestSupportedVersion()
expect(latestVersion).toBeDefined()
expect(latestVersion).toEqual(expect.any(Number))
})

test('isVersionSupported', async () => {
// Arrange
const dataSet = [
[40, false],
[46, true],
[52, true],
[55, true],
]

// Act & Assert
for (const data of dataSet) {
const result = await isVersionSupported(data[0])
expect(result).toEqual(data[1])
}
})
})
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/botHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const BotHandler = require('../../../../src/service/botHandler')
const metadataManager = require('../../../../src/metadata/metadataManager')
jest.mock('fs')

const testContext = {
Expand Down Expand Up @@ -34,7 +33,8 @@ require('fs').__setMockFiles({
describe('test BotHandler', () => {
let globalMetadata
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})

// eslint-disable-next-line no-undef
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/customObjectHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const CustomObjectHandler = require('../../../../src/service/customObjectHandler')
const metadataManager = require('../../../../src/metadata/metadataManager')
const { MASTER_DETAIL_TAG } = require('../../../../src/utils/metadataConstants')
const fse = require('fs-extra')
jest.mock('fs')
Expand Down Expand Up @@ -35,7 +34,8 @@ const testContext = {
describe('customObjectHandler', () => {
let globalMetadata
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})
// eslint-disable-next-line no-undef
describe('test CustomObjectHandler with fields', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/inFileHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { PLUS, MINUS } = require('../../../../src/utils/gitConstants')
const {
LABEL_DIRECTORY_NAME,
} = require('../../../../src/utils/metadataConstants')
const metadataManager = require('../../../../src/metadata/metadataManager')
const { EOL } = require('os')

jest.mock('fs')
Expand Down Expand Up @@ -92,7 +91,8 @@ describe(`test if inFileHandler`, () => {
let work
let globalMetadata
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})
beforeEach(() => {
work = {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/inFolderHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const InFolder = require('../../../../src/service/inFolderHandler')
const metadataManager = require('../../../../src/metadata/metadataManager')
const fs = require('fs')
const fse = require('fs-extra')
jest.mock('fs')
Expand Down Expand Up @@ -62,7 +61,8 @@ describe('InFolderHander', () => {
let globalMetadata
let work
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
work = {
config: { output: '', repo: '.', generateDelta: true },
diffs: { package: new Map(), destructiveChanges: new Map() },
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/inResourceHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const InResource = require('../../../../src/service/inResourceHandler')
const metadataManager = require('../../../../src/metadata/metadataManager')
jest.mock('fs')
jest.mock('fs-extra')
const fs = require('fs')
Expand Down Expand Up @@ -67,7 +66,8 @@ describe('test inResourceHandler', () => {
'force-app/main/default/waveTemplates/WaveTemplateTest/template-info.json':
'{"test":"test"}',
})
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})

// eslint-disable-next-line no-undef
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/lib/service/standardHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
const StandardHandler = require('../../../../src/service/standardHandler')
const { FSE_BIGINT_ERROR } = require('../../../../src/utils/fsHelper')
const metadataManager = require('../../../../src/metadata/metadataManager')
const mc = require('../../../../src/utils/metadataConstants')
const fse = require('fs-extra')
const fs = require('fs')
Expand Down Expand Up @@ -96,7 +95,8 @@ const testContext = {
describe(`standardHandler`, () => {
let globalMetadata
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 54)
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})

beforeEach(() => {
Expand Down
13 changes: 4 additions & 9 deletions __tests__/unit/lib/service/subCustomObjectHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
const metadataManager = require('../../../../src/metadata/metadataManager')
const SubCustomObject = require('../../../../src/service/subCustomObjectHandler')
jest.mock('fs')

Expand Down Expand Up @@ -52,10 +51,8 @@ const testContext = {
testHandlerHelper(testContext)

test('field is not a master detail', async () => {
const globalMetadata = await metadataManager.getDefinition(
'directoryName',
50
)
// eslint-disable-next-line no-undef
const globalMetadata = await getGlobalMetadata()
const handler = new testContext.handler(
`A ${testContext.testData[0][1]}`,
testContext.testData[0][0],
Expand All @@ -73,10 +70,8 @@ test('field is not a master detail', async () => {

test('field does not generate delta', async () => {
testContext.work.config.generateDelta = false
const globalMetadata = await metadataManager.getDefinition(
'directoryName',
50
)
// eslint-disable-next-line no-undef
const globalMetadata = await getGlobalMetadata()
const handler = new testContext.handler(
`A ${testContext.testData[0][1]}`,
testContext.testData[0][0],
Expand Down
7 changes: 2 additions & 5 deletions __tests__/unit/lib/service/typeHandlerFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ const InResource = require('../../../../src/service/inResourceHandler')
const InFolder = require('../../../../src/service/inFolderHandler')
const Standard = require('../../../../src/service/standardHandler')
const TypeHandlerFactory = require('../../../../src/service/typeHandlerFactory')
const metadataManager = require('../../../../src/metadata/metadataManager')

describe('the type handler factory', () => {
let typeHandlerFactory
beforeAll(async () => {
const globalMetadata = await metadataManager.getDefinition(
'directoryName',
50
)
// eslint-disable-next-line no-undef
const globalMetadata = await getGlobalMetadata()
typeHandlerFactory = new TypeHandlerFactory(
{
config: { apiVersion: '46' },
Expand Down
Loading

0 comments on commit e58d63a

Please sign in to comment.